The Evaluate
instance is used primarily to test a specific Feature
, for a particular User
,
evaluates to a specific variantKey
(usually 'on'
or 'off'
).
Its main usage expects a boolean
returned, an example being:
if ( featureflow.evaluate(featureKey, user).is(variant) ){
...Do some feature specific code for the targeted variant here...
}
It calculates this value by finding the correct variant
for the feature
specified by the featureKey
and User
provided (optional).
The Evaluate
class can only be instantiated by the FeatureflowClient
calling featureflow.evaluate(key, [user])
.
See Feature
to see the structure of a feature.
During construction, this.evaluatedVariant
should be set to the result of _calculateVariant(feature, defaultFeatureVariant, user)
- Returns: a
string
containing the evaluatedVariant
- Arguments:
feature
the feature to calculatedefaultFeatureVariant
the failover variant specified in theFeatureRegistration
for the Feature keyuser
fromuser
-> should have default properties added to it. SeeUser
for more details
Example implementation is as follows:
# arguments: [feature, defaultFeatureVariant, user]
if (feature is defined) then
if (feature.enabled is false) then
return feature.offVariantKey
endif
foreach (rule in feature.rules) do
# there is always a default rule so a value will always be returned
if (EvaluateHelpers.ruleHasMatched(rule, user) == true) then
hash = EvaluateHelpers.calculateHash(feature.variationSalt, feature.key, user.key)
variantValue = Helpers.getVariantValue(hash)
return EvaluateHelpers.getVariantSplitKey(rule.variantSplits, variantValue)
endif
done
else
if (defaultFeatureVariant is defined) then
return defaultFeatureVariant
else
return 'off'
endif
endif
- Returns: a
boolean
if thefeatureKey
matches thevariant
provided - Arguments:
variant*
The value of the variant you are testing (e.g.'on'
or'off'
)
Example implementation:
# arguments: [variant]
# from new Evaluate(...): [this.featureKey, this.user]
evaluated = this.value() # see .value() below
sendEvaluateEvent(this.featureKey, variant, evaluated, this.user) # Send an event to featureflow.io with the expected and evaluated feature for the user
return value == evaluated
You might also consider implementing some helper functions for the default Variant
cases:
featureflow.evaluate(...).isOn() # equivalent to featureflow.evaluate(...).is('on')
featureflow.evaluate(...).isOff() # equivalent to featureflow.evaluate(...).is('off')
- Returns: a
string
containing the evaluatedVariant
fromthis.evaluatedVaraint
See Operators to see the different operators that need to be implemented