Write evaluateFlag(flag, user, hash), which decides whether a feature flag is on for a user. The flag comes from a JSON config file, so its contents are not guaranteed to match the declared types.
hash(s) is injected; it returns an integer from 0 to 99. The rollout bucket for a user is hash(flag.key + ":" + user.id).
Validation happens first, on every call, even when the flag is disabled:
rolloutPercentmust be a finite number from 0 to 100 inclusive, else fail with{ kind: "invalid_rollout" }.- Then each rule is checked in order. A rule is invalid when its
opis not one of"eq","in","gte"; whenopis"in"andvalueis not an array; or whenopis"gte"andvalueis not a finite number. The first invalid rule fails with{ kind: "invalid_rule", index }, its position inrules.
Evaluation of a valid flag:
- If
enabledisfalse, the result isdefaultValue. (defaultValueis not used otherwise.) - Every rule must match the user. If any rule does not match, the result is
false. - If every rule matches (or there are no rules), the result is
truewhen the user's bucket is less thanrolloutPercent, elsefalse.
A rule reads user.attributes[rule.attribute]. If that attribute is missing (not present, null or undefined), the rule does not match, whatever its op and value. Otherwise:
eq: matches when the attribute is strictly equal (===) tovalue.in: matches when the attribute is strictly equal to some element ofvalue.gte: matches when the attribute is a number and is greater than or equal tovalue.