Skip to content

bitflag-js v1.0.0

Choose a tag to compare

@IanMitchell IanMitchell released this 09 Aug 21:21
· 7 commits to main since this release
85fae6f

This package provides a class for working with bitflag fields. These structures are very useful as a way to condense a list of boolean values into a single number. They are perfect matches for things like user flags, permissions, and more.

bitflag-js is written in TypeScript and uses bigints instead of numbers.

Usage

import { BitField } from "bitflags-js";

const UserBadges = {
	VERIFIED: 1n << 0n,
	CONTRIBUTOR: 1n << 1n,
	SUPPORTER: 1n << 2n,
	VIP: 1n << 3n,
};

const bitfield = new BitField();
bitfield.add(UserBadges.VERIFIED);
bitfield.add(UserBadges.CONTRIBUTOR);

if (bitfield.has(UserBadges.VERIFIED)) {
	console.log("This user is verified!");
}

console.log(bitfield.value);