Type-safe bitwise flag operations for permissions, features, and options
npm install @philiprehberger/bit-flagsType-safe bitwise flag operations for permissions, features, and options.
npm install @philiprehberger/bit-flags
import { defineFlags } from '@philiprehberger/bit-flags';
const permissions = defineFlags({
read: 1,
write: 2,
execute: 4,
});
let flags = permissions.set(0, 'read', 'write'); // 3
permissions.has(flags, 'read'); // true
permissions.has(flags, 'execute'); // false
flags = permissions.toggle(flags, 'execute'); // 7
permissions.toArray(flags); // ['read', 'write', 'execute']
defineFlags<F>(definition: F): FlagSet<F>Creates a type-safe flag set from a record of flag names to bit values.
FlagSet<F>has(flags, flag) — Check if a flag is setset(flags, ...flagNames) — Set one or more flagsunset(flags, ...flagNames) — Unset one or more flagstoggle(flags, flag) — Toggle a flag on/offtoArray(flags) — Get active flag names as an arrayfromArray(flagNames) — Combine flag names into a numberall — All flags combinednone — Zero valuenpm install
npm run build
npm test
If you find this project useful: