Bit Flag Utilities for Permission Sets

Represent up to 30 permissions as one 32-bit mask with helpers to build, test, add, remove, toggle and list enabled flags.

JavascriptSecurity Boundaries

Code

javascriptsnippet
50 lines
export function createFlagSet(names) {
  if (!Array.isArray(names) || names.length === 0) {
    throw new TypeError("names must be a non-empty array");
  }
  if (names.length > 30) throw new RangeError("at most 30 flags are supported");
  const bits = new Map();
  const all = [];
  for (const name of names) {
    if (typeof name !== "string" || name.trim() === "") {
      throw new TypeError("flag names must be non-empty strings");
    }
    const normalized = name.trim();
    if (bits.has(normalized)) throw new Error(`duplicate flag "${normalized}"`);
    bits.set(normalized, 1 << bits.size);
    all.push(normalized);
  }
  const resolve = (name) => {
    const bit = bits.get(name);
    if (bit === undefined) throw new Error(`unknown flag "${name}"`);
    return bit;
  };
  return {
    names: () => [...all],
    mask(...selected) {
      return selected.reduce((mask, name) => mask | resolve(name), 0);
    },
    has(mask, name) {
      return (mask & resolve(name)) !== 0;
    },
    add(mask, name) {
      return mask | resolve(name);
    },
    remove(mask, name) {
      return mask & ~resolve(name);
    },
    toggle(mask, name) {
      return mask ^ resolve(name);
    },
    toNames(mask) {
      return all.filter((name) => (mask & resolve(name)) !== 0);
    },
  };
}

const permissions = createFlagSet(["read", "write", "admin"]);
let mask = permissions.mask("read", "write");
console.log(mask, permissions.toNames(mask));
mask = permissions.remove(mask, "write");
mask = permissions.toggle(mask, "admin");
console.log(permissions.has(mask, "write"), permissions.toNames(mask));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

3 [ 'read', 'write' ]
false [ 'read', 'admin' ]

How it works

Count the functions resolve and createFlagSet as the entire surface area of Bit Flag Utilities for Permission Sets.

Iteration happens inside for (const name of names) — everything outside it runs once. A guard precedes the TypeError, RangeError and Error: bad state stops the flow there instead of continuing.

The result is a fresh construction, so callers can keep their originals.

Points of care

  • The guard names.length > 30 short-circuits the flow with an early throw when its condition holds.

Applying it

The main signature reads export function createFlagSet(names)

The surface worth importing is createFlagSet; the helpers around it are private to the snippet.

Statements that matter

return selected.reduce((mask, name) => mask | resolve(name), 0);
return all.filter((name) => (mask & resolve(name)) !== 0);
if (names.length > 30) throw new RangeError("at most 30 flags are supported");

Interactive Tools for This Topic

FAQ

Does `createFlagSet` modify its input?
No — a new value is constructed for the result and the arguments are never written to.
How does the code behave when input is bad?
It throws `TypeError`, `RangeError` and `Error` before any real work runs, via the guard `names.length > 30` — handle it at the call site or let it propagate deliberately.
Does this work in a bare project with no dependencies file?
Yes — a bare project runs it as-is, since nothing needs declaring. The file defines `resolve`, `createFlagSet` at load time.