Skip to main content

Class: BitField<Flags>

Type parameters

Type parameter
Flags extends Record<string, number> | Record<string, bigint>

Constructors

new BitField()

new BitField<Flags>(flags: Readonly<Flags>): BitField<Flags>

Parameters

ParameterType
flagsReadonly<Flags>

Returns

BitField<Flags>

Source

lib/BitField.ts:10

Properties

[FlagEntriesSymbol]

private readonly [FlagEntriesSymbol]: readonly [string, Flags[keyof Flags]][]

Source

lib/BitField.ts:8


flags

readonly flags: Flags

Source

lib/BitField.ts:7


mask

readonly mask: PrimitiveType<Flags[keyof Flags]>

Source

lib/BitField.ts:6


type

readonly type: Flags[keyof Flags] extends number ? "number" : "bigint"

Source

lib/BitField.ts:4


zero

readonly zero: Flags[keyof Flags] extends number ? 0 : 0n

Source

lib/BitField.ts:5

Methods

any()

any(field: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>, bits: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>): boolean

Checks whether or not field contains any of the bits from bits.

Parameters

ParameterTypeDescription
fieldMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The bits to compare the bits from.
bitsMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The bits to compare with.

Returns

boolean

Whether or not field has any of bits's bits, also denoted as A ∩ B ≠ ∅.

Source

lib/BitField.ts:85


complement()

complement(field: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>): PrimitiveType<Flags[keyof Flags]>

Makes the complement of field, which is a field of all bits (of U or the union of all Flags bits) that do not belong to A. It is the result of U ∖ A, or difference(U, field).

Parameters

ParameterTypeDescription
fieldMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The bits to get the complement of.

Returns

PrimitiveType<Flags[keyof Flags]>

The complement of field, also denoted Aᶜ or A'.

Example

const bitfield = new BitField({
Read: 0b0001,
Write: 0b0010,
Edit: 0b0100,
Delete: 0b1000
});

bitfield.complement(0b0100);
// 0b1011

Source

lib/BitField.ts:118


difference()

difference(a: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>, b: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>): PrimitiveType<Flags[keyof Flags]>

Removes from a the bits that exist in b.

Parameters

ParameterTypeDescription
aMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The first field.
bMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The bits to remove from a.

Returns

PrimitiveType<Flags[keyof Flags]>

The result of a ∖ b.

Example

bitfield.difference(0b1100, 0b0100);
// 0b1000

bitfield.difference(0b1111, 0b0110);
// 0b1001

Seealso

https://en.wikipedia.org/wiki/Difference_(set_theory)

Source

lib/BitField.ts:187


has()

has(field: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>, bits: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>): boolean

Checks whether or not field is a superset of or equal to bits.

Parameters

ParameterTypeDescription
fieldMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The bits to compare the bits from.
bitsMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The bits to compare with.

Returns

boolean

Whether or not field is a superset of or equal to bits, also denoted as A ⊇ B.

Source

lib/BitField.ts:95


intersection()

intersection(bitfield: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>, ...fields: readonly MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>[]): PrimitiveType<Flags[keyof Flags]>

Makes an intersection of all the bits.

Parameters

ParameterTypeDescription
bitfieldMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The first field.
...fieldsreadonly MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>[]The bits to intersect with bitfield.

Returns

PrimitiveType<Flags[keyof Flags]>

The result of intersecting bitfield with all of the fields, also denoted as A ⋂ fields.

Example

bitfield.intersection(0b0001, 0b0100);
// 0b0000

bitfield.intersection(0b1100, 0b0100);
// 0b0100

bitfield.intersection(0b1101, 0b0101, 0b1100);
// 0b0100

Seealso

https://en.wikipedia.org/wiki/Intersection_(set_theory)

Source

lib/BitField.ts:163


resolve()

resolve(resolvable: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>): PrimitiveType<Flags[keyof Flags]>

Resolves a:

  • string: If it's a property of Flags.
  • number: If the BitField processes number primitives.
  • bigint: If the BitField processes bigint primitives.
  • Array: Resolves recursively.

Parameters

ParameterTypeDescription
resolvableMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The value to resolve.

Returns

PrimitiveType<Flags[keyof Flags]>

The resolved value.

Source

lib/BitField.ts:64


symmetricDifference()

symmetricDifference(a: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>, b: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>): PrimitiveType<Flags[keyof Flags]>

Computes the symmetric difference, denoted as A ⊖ B or A Δ B, which is the disjunctive union, or the set of elements which are in either of the sets, but not in their intersection. As such, this is the result of (A ∖ B) ∪ (B ∖ A), union(difference(a, b), difference(b, a)), or a ⊕ b.

Parameters

ParameterTypeDescription
aMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The first field.
bMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The second field.

Returns

PrimitiveType<Flags[keyof Flags]>

The result of computing a Δ b.

Remarks

The empty set () is neutral, as such, A Δ ∅ = A and A Δ A = ∅

Example

bitfield.symmetricDifference(0b1100, 0b0011);
// 0b1111

bitfield.symmetricDifference(0b1101, 0b1011);
// 0b0110

Seealso

https://en.wikipedia.org/wiki/Symmetric_difference

Source

lib/BitField.ts:209


toArray()

toArray(field: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>): keyof Flags[]

Retrieves an array of the properties from Flags whose values are contained in field.

Parameters

ParameterTypeDescription
fieldMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The field to convert to an array.

Returns

keyof Flags[]

The names of the BitField's flag properties whose value are contained in field.

Example

const bitfield = new BitField({
Read: 0b0001,
Write: 0b0010,
Edit: 0b0100,
Delete: 0b1000
});

bitfield.toArray(0b0101);
// ['Read', 'Edit']

Source

lib/BitField.ts:230


toEntries()

toEntries(field: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>): IterableIterator <[keyof Flags, PrimitiveType<Flags[keyof Flags]>]>

Retrieves an iterator of the entries from Flags whose values are contained in field.

Parameters

ParameterTypeDescription
fieldMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The field to convert to an iterator.

Returns

IterableIterator <[keyof Flags, PrimitiveType<Flags[keyof Flags]>]>

An iterator with the entries of the BitField's flag properties whose value are contained in field.

Example

const bitfield = new BitField({
Read: 0b0001,
Write: 0b0010,
Edit: 0b0100,
Delete: 0b1000
});

[...bitfield.toEntries(0b0101)];
// [['Read', 0b0001], ['Edit', 0b0100]]

Source

lib/BitField.ts:301


toKeys()

toKeys(field: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>): IterableIterator<keyof Flags>

Retrieves an iterator of the properties from Flags whose values are contained in field.

Parameters

ParameterTypeDescription
fieldMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The field to convert to an iterator.

Returns

IterableIterator<keyof Flags>

An iterator with the keys of the BitField's flag properties whose value are contained in field.

Example

const bitfield = new BitField({
Read: 0b0001,
Write: 0b0010,
Edit: 0b0100,
Delete: 0b1000
});

[...bitfield.toKeys(0b0101)];
// ['Read', 'Edit']

Source

lib/BitField.ts:251


toObject()

toObject(field: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>): Record<keyof Flags, boolean>

Retrieves an object with the properties from Flags whose values are boolean denoting whether or not the flag's bit is contained in field.

Parameters

ParameterTypeDescription
fieldMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The field to convert to an object.

Returns

Record<keyof Flags, boolean>

An object with the properties of Flags which values are boolean.

Example

const bitfield = new BitField({
Read: 0b0001,
Write: 0b0010,
Edit: 0b0100,
Delete: 0b1000
});

bitfield.toObject(0b0101);
// {
// Read: true,
// Write: false,
// Edit: true,
// Delete: false
// }

Source

lib/BitField.ts:332


toValues()

toValues(field: MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>): IterableIterator <PrimitiveType<Flags[keyof Flags]>>

Retrieves an iterator of the values from Flags whose values are contained in field.

Parameters

ParameterTypeDescription
fieldMaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>The field to convert to an iterator.

Returns

IterableIterator <PrimitiveType<Flags[keyof Flags]>>

An iterator with the values of the BitField's flag properties whose value are contained in field.

Example

const bitfield = new BitField({
Read: 0b0001,
Write: 0b0010,
Edit: 0b0100,
Delete: 0b1000
});

[...bitfield.toValues(0b0101)];
// [0b0001, 0b0100]

Source

lib/BitField.ts:276


union()

union(...fields: readonly MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>[]): PrimitiveType<Flags[keyof Flags]>

Makes a union of all the bits.

Parameters

ParameterTypeDescription
...fieldsreadonly MaybeArray<keyof Flags | PrimitiveType<Flags[keyof Flags]>>[]The bits to create a union of.

Returns

PrimitiveType<Flags[keyof Flags]>

The result of combining all bits together, also denoted as ∅ ⋃ fields.

Example

bitfield.union(0b0001, 0b0100);
// 0b0101

bitfield.union(0b1100, 0b0001, 0b0010);
// 0b1111

Seealso

https://en.wikipedia.org/wiki/Union_(set_theory)

Source

lib/BitField.ts:136