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
Parameter | Type |
---|---|
flags | Readonly <Flags > |
Returns
BitField
<Flags
>
Defined in
Properties
[FlagEntriesSymbol]
private
readonly
[FlagEntriesSymbol]: readonly [string
,Flags
[keyofFlags
]][]
Defined in
flags
readonly
flags:Flags
Defined in
mask
readonly
mask:PrimitiveType
<Flags
[keyofFlags
]>
Defined in
type
readonly
type:Flags
[keyofFlags
] extendsnumber
?"number"
:"bigint"
Defined in
zero
readonly
zero:Flags
[keyofFlags
] extendsnumber
?0
:0n
Defined in
Methods
any()
any(
field
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>,bits
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>):boolean
Checks whether or not field
contains any of the bits from bits
.
Parameters
Parameter | Type | Description |
---|---|---|
field | MaybeArray <keyof Flags | PrimitiveType <Flags [keyof Flags ]>> | The bits to compare the bits from. |
bits | MaybeArray <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 ≠ ∅
.
Defined in
complement()
complement(
field
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>):PrimitiveType
<Flags
[keyofFlags
]>
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
Parameter | Type | Description |
---|---|---|
field | MaybeArray <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
Defined in
difference()
difference(
a
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>,b
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>):PrimitiveType
<Flags
[keyofFlags
]>
Removes from a
the bits that exist in b
.
Parameters
Parameter | Type | Description |
---|---|---|
a | MaybeArray <keyof Flags | PrimitiveType <Flags [keyof Flags ]>> | The first field. |
b | MaybeArray <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)
Defined in
has()
has(
field
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>,bits
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>):boolean
Checks whether or not field
is a superset of or equal to bits
.
Parameters
Parameter | Type | Description |
---|---|---|
field | MaybeArray <keyof Flags | PrimitiveType <Flags [keyof Flags ]>> | The bits to compare the bits from. |
bits | MaybeArray <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
.
Defined in
intersection()
intersection(
bitfield
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>, ...fields
: readonlyMaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>[]):PrimitiveType
<Flags
[keyofFlags
]>
Makes an intersection of all the bits.
Parameters
Parameter | Type | Description |
---|---|---|
bitfield | MaybeArray <keyof Flags | PrimitiveType <Flags [keyof Flags ]>> | The first field. |
...fields | readonly 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)
Defined in
resolve()
resolve(
resolvable
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>):PrimitiveType
<Flags
[keyofFlags
]>
Resolves a:
string
: If it's a property of Flags.number
: If the BitField processesnumber
primitives.bigint
: If the BitField processesbigint
primitives.Array
: Resolves recursively.
Parameters
Parameter | Type | Description |
---|---|---|
resolvable | MaybeArray <keyof Flags | PrimitiveType <Flags [keyof Flags ]>> | The value to resolve. |
Returns
PrimitiveType
<Flags
[keyof Flags
]>
The resolved value.
Defined in
symmetricDifference()
symmetricDifference(
a
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>,b
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>):PrimitiveType
<Flags
[keyofFlags
]>
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
Parameter | Type | Description |
---|---|---|
a | MaybeArray <keyof Flags | PrimitiveType <Flags [keyof Flags ]>> | The first field. |
b | MaybeArray <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
Defined in
toArray()
toArray(
field
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>): keyofFlags
[]
Retrieves an array of the properties from Flags whose values are contained in field
.
Parameters
Parameter | Type | Description |
---|---|---|
field | MaybeArray <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']
Defined in
toEntries()
toEntries(
field
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>):IterableIterator
<[keyofFlags
,PrimitiveType
<Flags
[keyofFlags
]>]>
Retrieves an iterator of the entries from Flags whose values are contained in field
.
Parameters
Parameter | Type | Description |
---|---|---|
field | MaybeArray <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]]
Defined in
toKeys()
toKeys(
field
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>):IterableIterator
<keyofFlags
>
Retrieves an iterator of the properties from Flags whose values are contained in field
.
Parameters
Parameter | Type | Description |
---|---|---|
field | MaybeArray <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']
Defined in
toObject()
toObject(
field
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>):Record
<keyofFlags
,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
Parameter | Type | Description |
---|---|---|
field | MaybeArray <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
// }
Defined in
toValues()
toValues(
field
:MaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>):IterableIterator
<PrimitiveType
<Flags
[keyofFlags
]>>
Retrieves an iterator of the values from Flags whose values are contained in field
.
Parameters
Parameter | Type | Description |
---|---|---|
field | MaybeArray <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]
Defined in
union()
union(...
fields
: readonlyMaybeArray
<keyofFlags
|PrimitiveType
<Flags
[keyofFlags
]>>[]):PrimitiveType
<Flags
[keyofFlags
]>
Makes a union of all the bits.
Parameters
Parameter | Type | Description |
---|---|---|
...fields | readonly 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)