Skip to content

Latest commit

 

History

History
86 lines (68 loc) · 3.24 KB

File metadata and controls

86 lines (68 loc) · 3.24 KB

Evaluate

What is this?

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).

Constructor

new Evaluate(feature, [user])

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)

_calculateVariant(feature, user)

  • Returns: a string containing the evaluated Variant
  • Arguments:
    • feature the feature to calculate
    • defaultFeatureVariant the failover variant specified in the FeatureRegistration for the Feature key
    • user from user -> should have default properties added to it. See User 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

Interface

featureflow.evaluate(...).is(variant)

  • Returns: a boolean if the featureKey matches the variant 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')

featureflow.evaluate(...).value()

  • Returns: a string containing the evaluated Variant from this.evaluatedVaraint

See Operators to see the different operators that need to be implemented