-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbit-field-types.ts
122 lines (121 loc) · 3.64 KB
/
bit-field-types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
export interface BitFieldStructure<
K extends PropertyKey,
N extends number | bigint = number,
> {
value: N;
/**
* Iterates over numbers stored in the instance.
*/
[Symbol.iterator](): Generator<number>;
/**
* Returns the value of a given field.
*
* @param field the name of the field
* @return value of the field
*/
get(field: K): number;
/**
* Checks whether the instance has all the specified fields set to 1. Useful for bit flags.
*
* @param fields names of the fields to check
* @return whether all the specified fields are set in the instance
*/
has(...fields: Array<K>): boolean;
/**
* Checks if the instance contains all the key-value pairs listed in matcher.
* Use `BigBitField.getMatcher` to get an array of precomputed values
* that you can use to efficiently compare multiple instances
* to the same key-value pairs as shown in the examples below.
*
* @param matcher an object with key-value pairs, or an array of precomputed matcher values
* @return whether the instance matches with the provided fields
*/
match(matcher: Partial<Record<K, number>> | [N, N]): boolean;
/**
* Stores a given value in a field.
*
* @param field name of the field
* @param value value of the field
* @return the instance
*/
set(field: K, value?: number): this;
/**
* Returns the bigint value of an instance.
*/
toJSON(): N;
/**
* Returns the object representation of the instance,
* with field names as properties with corresponding values.
*/
toObject(): Record<K, number>;
/**
* Returns a string representing the value of the instance.
*/
toString(): string;
/**
* Returns the bigint value of an instance.
*/
valueOf(): N;
}
export interface BitFieldConstructor<
K extends PropertyKey,
N extends number | bigint = number,
> {
schema: Record<K, N>;
fields: Array<K>;
masks: Record<K, N>;
offsets: Record<K, N>;
mask: N;
size: N;
/**
* @param data a single numerical value of the bitfield,
* a bitfield, or a map of field names with their respective values
*/
new (
data?: N | BitFieldStructure<K, N> | Array<number> | Record<K, number>,
): BitFieldStructure<K, N>;
/**
* Decodes a numeric value into its object representation according to the schema.
*
* @param data a numeric value
* @return object representation
*/
decode(data: N): Record<K, number>;
/**
* Encodes a given list of numbers or map of fields and their respective values
* into a single numeric value according to the schema.
*
* @param data a list of numbers or an object to encode
* @return encoded numeric value
*/
encode(data: Array<number> | Record<K, number>): N;
/**
* Creates a tuple of values to be used as a matcher
* to efficiently match against multiple instances.
*
* @param matcher an object containing field names and their values
* @return a tuple of precomputed values
*/
getMatcher(matcher: Partial<Record<K, number>>): [N, N];
/**
* Returns the minimum amount of bits necessary to hold a given number.
*
* @param number
* @return the amount of bits
*/
getMinSize(value: number): number;
/**
* Checks if a given set of values are valid according to the schema.
*
* @param data a map of field names and their values to check
* @return whether all pairs are valid
*/
isValid(data: Partial<Record<K, number>>): boolean;
/**
* The static version of `BitField#match`, matches a given value against a precomputed matcher.
*
* @param value a value to check
* @param matcher a precomputed set of values
*/
match(value: N, matcher: [N, N]): boolean;
}