```ts class GroupPermissions extends bitfields.Bitfield<typeof GroupPermissions['Flags']> { static Flags = { 'create:post_content': bitfields.bit(0), // (1n << 0n) -> 0n, 'read:post_content': bitfields.bit(1), // (1n << 1n) -> 1n,
npm install @isluny/bitfieldsts
class GroupPermissions extends bitfields.Bitfield {
static Flags = {
'create:post_content': bitfields.bit(0), // (1n << 0n) -> 0n,
'read:post_content': bitfields.bit(1), // (1n << 1n) -> 1n,
'update:members': bitfields.bit(2), // (1n << 2n) -> 2n,
'update:group': bitfields.bit(3), // (1n << 3n) -> 4n,
} static defaultBits = GroupPermissions.resolve(
'create:post_content',
'read:post_content'
)
}
`Using Bitfield
`ts
const permissions = new GroupPermissions()
`Methods
$3
Checks if a specific bit is set.`ts
permissions.has('create:post_content') // true or false
`$3
Adds one or more bits to the bitfield.`ts
permissions.add('update:members')
`$3
Removes one or more bits from the bitfield.`ts
permissions.remove('create:post_content')
`$3
Returns an object representing the bitfield flags as key-value pairs.`ts
console.log(permissions.serialize())
`$3
Returns an array of all enabled flags.`ts
console.log(permissions.toArray())
`$3
Returns the numerical representation of the bitfield.`ts
console.log(permissions.toJSON())
`$3
You can iterate over a bitfield instance to get all active flags.`ts
for (const flag of permissions) {
console.log(flag)
}
`Conclusion
@isluny/bitfields` provides an efficient and flexible way to manage permissions and feature flags using bitfields. Whether you're handling user roles, access permissions, or other flag-based systems, this package offers an intuitive API with TypeScript support. 🚀