Manage bitewise flags wisely
Built with β€οΈβπ₯ by @dubisdev
FlaggyJS is a lightweight TypeScript library for managing bitwise flags.
Powerful and efficient binary flags managing for permission systems, feature toggles, or any use-case.
// 1. Import the libraryimport{useFlags}from"flaggyjs";// 2. Define your flagsconst{ flags, FlagsContainer }=useFlags(["AMAZING","SIMPLE","BORING"]asconst);// 3. Create as many containers as you needconstflaggy=newFlagsContainer();constdocumentation=newFlagsContainer([flags.SIMPLE,flags.BORING]);// Initialize with flags π// 4. Start working with fully Typed flagsflaggy.addFlag(flags.AMAZING);// Add one flag π©flaggy.addFlags(flags.SIMPLE,flags.BORING);// or many flags at once πflaggy.removeFlag(flags.BORING);// Flaggy is not boring! π// π FlaggyJS is type safe// v Argument of type "Too Long" is not assignable... // Fails type checkingdocumentation.addFlag("Too Long")// β οΈ Error! => Invalid flag provided // Also on runtime// 5. Evaluate your flags when neededconsole.log(flaggy.hasFlag(flags.AMAZING));// trueconsole.log(documentation.hasFlag(flags.SIMPLE));// trueconsole.log(flaggy.hasFlag(flags.BORING));// falseconsole.log(flags)// {// AMAZING: "AMAZING",// SIMPLE: "SIMPLE",// BORING: "BORING"// }- π Type-safe flag managementwithout Enums.
- π€ Zero dependencies - No extra packages are used.
- π§ββοΈ Bitwise operations: Efficiently manage flags with bitwise operations under the hood.
- π Validation: Ensures invalid flags are caught early.
- π Immutable flag definitions: Prevent accidental changes to the flag definitions.
npm install flaggyjshasFlag(flag: FlagId): booleanChecks if a flag is currently set.addFlag(flag: FlagId): voidAdds a flag to the current set.removeFlag(flag: FlagId): voidRemoves a flag from the current set.addFlags(...flags: FlagId[]): voidAdds multiple flags at once.
The package uses Vitest for testing. To run the tests:
bun run testThe package uses tsc for building TypeScript into JavaScript:
bun run build