Percentage Rollout Feature Flag Evaluator
Enable features for a stable percentage of users by hashing flag key and user ID into buckets, with allowlist overrides.
Code
export interface FlagContext {
readonly userId: string;
readonly allowlist?: ReadonlySet<string>;
}
export interface FlagDecision {
readonly enabled: boolean;
readonly bucket: number;
readonly reason: "allowlist" | "percentage" | "off";
}
export function evaluateFlag(flagKey: string, percentage: number, context: FlagContext): FlagDecision {
if (typeof flagKey !== "string" || flagKey.trim() === "") {
throw new TypeError("flagKey must be a non-empty string");
}
if (typeof context.userId !== "string" || context.userId === "") {
throw new TypeError("userId must be a non-empty string");
}
if (!Number.isFinite(percentage) || percentage < 0 || percentage > 100) {
throw new RangeError("percentage must be between 0 and 100");
}
if (context.allowlist?.has(context.userId) === true) {
return { enabled: true, bucket: 0, reason: "allowlist" };
}
if (percentage === 0) return { enabled: false, bucket: 100, reason: "off" };
const bucket = bucketFor(`${flagKey}:${context.userId}`) / 100;
return { enabled: bucket < percentage, bucket, reason: "percentage" };
}
function bucketFor(value: string): number {
let hash = 0x811c9dc5;
for (let index = 0; index < value.length; index += 1) {
hash ^= value.charCodeAt(index);
hash = Math.imul(hash, 0x01000193);
}
return (hash >>> 0) % 10000;
}
const allowlist = new Set(["internal-1"]);
console.log(evaluateFlag("new-checkout", 25, { userId: "user-42", allowlist }));
console.log(evaluateFlag("new-checkout", 25, { userId: "internal-1", allowlist }));
console.log(evaluateFlag("new-checkout", 0, { userId: "user-42" }));Output
Captured from a sandboxed run of this snippet in the build pipeline.
{ enabled: false, bucket: 99.26, reason: 'percentage' }
{ enabled: true, bucket: 0, reason: 'allowlist' }
{ enabled: false, bucket: 100, reason: 'off' }What the code does
Percentage Rollout Feature Flag Evaluator reduces to the functions evaluateFlag and bucketFor.
for (let index = 0; index < value.length; index += 1) is the loop that carries the per-item work. Invalid states are rejected up front: it throws TypeError and RangeError instead of continuing with bad data.
The result comes back as an object carrying true.
What to watch for
- The guard
typeof context.userId !== "string" || context.userId === ""short-circuits the flow with an earlythrowwhen its condition holds.
Applying it
Entry signature: export function evaluateFlag(flagKey: string, percentage: number, context: FlagContext)
evaluateFlag is the public entry point; everything else in the file exists to support it.
The call returns true.
Statements that matter
if (percentage === 0) return { enabled: false, bucket: 100, reason: "off" };
return { enabled: bucket < percentage, bucket, reason: "percentage" };
throw new RangeError("percentage must be between 0 and 100");
Interactive Tools for This Topic
FAQ
- Does `evaluateFlag` modify its input?
- No. The code builds and returns a new value and leaves the arguments untouched, so it is safe to call on shared or frozen data structures.
- What happens on invalid input?
- `TypeError` and `RangeError` is raised at the guard (`typeof context.userId !== "string" || context.userId === ""`), so invalid state never reaches the main logic.
- What has to be installed first?
- Nothing — the runtime itself provides everything the code touches. The file defines `evaluateFlag`, `bucketFor` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.