Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 1.6 KiB · at 355e324

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:

  • rolloutPercent must 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 op is not one of "eq", "in", "gte"; when op is "in" and value is not an array; or when op is "gte" and value is not a finite number. The first invalid rule fails with { kind: "invalid_rule", index }, its position in rules.

Evaluation of a valid flag:

  1. If enabled is false, the result is defaultValue. (defaultValue is not used otherwise.)
  2. Every rule must match the user. If any rule does not match, the result is false.
  3. If every rule matches (or there are no rules), the result is true when the user's bucket is less than rolloutPercent, else false.

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 (===) to value.
  • in: matches when the attribute is strictly equal to some element of value.
  • gte: matches when the attribute is a number and is greater than or equal to value.