Class: BitField<Flags>
@sapphire/bitfield.BitField
Type parameters
Name | Type |
---|---|
Flags | extends Record <string , number > | Record <string , bigint > |
Constructors
constructor
• new BitField<Flags
>(flags
)
Type parameters
Name | Type |
---|---|
Flags | extends Record <string , number > | Record <string , bigint > |
Parameters
Name | Type |
---|---|
flags | Readonly <Flags > |
Defined in
Properties
[FlagEntriesSymbol]
• Private
Readonly
[FlagEntriesSymbol]: readonly [string
, Flags
[keyof Flags
]][]
Defined in
flags
• Readonly
flags: Flags
Defined in
mask
• Readonly
mask: PrimitiveType
<Flags
[keyof Flags
]>
Defined in
type
• Readonly
type: Flags
[keyof Flags
] extends number
? "number"
: "bigint"
Defined in
zero
• Readonly
zero: Flags
[keyof Flags
] extends number
? 0
: 0n
Defined in
Methods
any
▸ any(field
, bits
): boolean
Checks whether or not field
contains any of the bits from bits
.
Parameters
Name | 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
): 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
Name | 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
, b
): PrimitiveType
<Flags
[keyof Flags
]>
Removes from a
the bits that exist in b
.
Parameters
Name | 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
, bits
): boolean
Checks whether or not field
is a superset of or equal to bits
.
Parameters
Name | 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
, ...fields
): PrimitiveType
<Flags
[keyof Flags
]>
Makes an intersection of all the bits.
Parameters
Name | 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
): PrimitiveType
<Flags
[keyof Flags
]>
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
Name | 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
, b
): 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
Name | 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
): keyof Flags
[]
Retrieves an array of the properties from Flags whose values are contained in field
.
Parameters
Name | 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
): IterableIterator
<[key: keyof Flags, value: PrimitiveType<Flags[keyof Flags]>]>
Retrieves an iterator of the entries from Flags whose values are contained in field
.
Parameters
Name | Type | Description |
---|---|---|
field | MaybeArray <keyof Flags | PrimitiveType <Flags [keyof Flags ]>> | The field to convert to an iterator. |
Returns
IterableIterator
<[key: keyof Flags, value: 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
): IterableIterator
<keyof Flags
>
Retrieves an iterator of the properties from Flags whose values are contained in field
.
Parameters
Name | 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
): 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
Name | 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
): IterableIterator
<PrimitiveType
<Flags
[keyof Flags
]>>
Retrieves an iterator of the values from Flags whose values are contained in field
.
Parameters
Name | 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
): PrimitiveType
<Flags
[keyof Flags
]>
Makes a union of all the bits.
Parameters
Name | 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)