Roll Out Feature Flags by Percentage
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write createFlags(config, hash) to decide which features each user gets. config maps flag names to settings: enabled, a boolean, and optionally allow and deny, lists of user ids, match, targeting rules, and rollout, a percentage from 0 to 100 that defaults to 100. hash turns a string into a whole number of 0 or more. Return { isEnabled(name, user), enabledFlags(user) }, where user is an object such as { id: "u1", country: "GB" } and id may be missing. isEnabled checks in this order. An unknown flag, or one whose enabled isn't true, is off for everyone. A user whose id is in deny is off, and then one whose id is in allow is on. Next, every key in match must hold for the user: their property of that name must equal the rule's value or, when the value is an array, be one of its items; if not, it's off. Last comes the rollout: at 100 everyone left is on; below it, a user without an id is off, and a user is on when hash(name + ":" + id) % 100 is less than the rollout, so the same user always gets the same answer and each flag picks its own users. enabledFlags returns the names of the flags that are on for the user, in config order.
Your code must pass
- Returns false for unknown or switched-off flags
- Lets deny and allow lists override the rest
- Requires every targeting rule to match
- Buckets users by hashing the flag name and user id
- Rolls out to about the right share of users
- Lists the flags a user has