utilium 1.10.0 → 2.0.0-pre.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/buffer.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-expressions */
2
2
 
3
+ import { _throw } from './misc.js';
4
+ import type { Mutable } from './objects.js';
5
+
3
6
  /**
4
- * A generic ArrayBufferView (typed array) constructor
7
+ * A generic constructor for an `ArrayBufferView`
5
8
  */
6
9
  export interface ArrayBufferViewConstructor {
7
10
  readonly prototype: ArrayBufferView<ArrayBufferLike>;
@@ -15,6 +18,274 @@ export interface ArrayBufferViewConstructor {
15
18
  new (array: ArrayLike<number> | ArrayBuffer): ArrayBufferView<ArrayBuffer>;
16
19
  }
17
20
 
21
+ /**
22
+ * A generic typed array.
23
+ */
24
+ export interface TypedArray<TArrayBuffer extends ArrayBufferLike = ArrayBuffer, TValue = number | bigint>
25
+ extends ArrayBufferView<TArrayBuffer> {
26
+ /**
27
+ * The size in bytes of each element in the array.
28
+ */
29
+ readonly BYTES_PER_ELEMENT: number;
30
+
31
+ /**
32
+ * Returns the this object after copying a section of the array identified by start and end
33
+ * to the same array starting at position target
34
+ * @param target If target is negative, it is treated as length+target where length is the
35
+ * length of the array.
36
+ * @param start If start is negative, it is treated as length+start. If end is negative, it
37
+ * is treated as length+end.
38
+ * @param end If not specified, length of the this object is used as its default value.
39
+ */
40
+ copyWithin(target: number, start: number, end?: number): this;
41
+
42
+ /**
43
+ * Determines whether all the members of an array satisfy the specified test.
44
+ * @param predicate A function that accepts up to three arguments. The every method calls
45
+ * the predicate function for each element in the array until the predicate returns a value
46
+ * which is coercible to the Boolean value false, or until the end of the array.
47
+ * @param thisArg An object to which the this keyword can refer in the predicate function.
48
+ * If thisArg is omitted, undefined is used as the this value.
49
+ */
50
+ every(predicate: (value: TValue, index: number, array: this) => unknown, thisArg?: any): boolean;
51
+
52
+ /**
53
+ * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array
54
+ * @param value value to fill array section with
55
+ * @param start index to start filling the array at. If start is negative, it is treated as
56
+ * length+start where length is the length of the array.
57
+ * @param end index to stop filling the array at. If end is negative, it is treated as
58
+ * length+end.
59
+ */
60
+ fill(value: TValue, start?: number, end?: number): this;
61
+
62
+ /**
63
+ * Returns the elements of an array that meet the condition specified in a callback function.
64
+ * @param predicate A function that accepts up to three arguments. The filter method calls
65
+ * the predicate function one time for each element in the array.
66
+ * @param thisArg An object to which the this keyword can refer in the predicate function.
67
+ * If thisArg is omitted, undefined is used as the this value.
68
+ */
69
+ filter(
70
+ predicate: (value: TValue, index: number, array: this) => any,
71
+ thisArg?: any
72
+ ): TypedArray<TArrayBuffer, TValue>;
73
+
74
+ /**
75
+ * Returns the value of the first element in the array where predicate is true, and undefined
76
+ * otherwise.
77
+ * @param predicate find calls predicate once for each element of the array, in ascending
78
+ * order, until it finds one where predicate returns true. If such an element is found, find
79
+ * immediately returns that element value. Otherwise, find returns undefined.
80
+ * @param thisArg If provided, it will be used as the this value for each invocation of
81
+ * predicate. If it is not provided, undefined is used instead.
82
+ */
83
+ find(predicate: (value: TValue, index: number, obj: this) => boolean, thisArg?: any): TValue | undefined;
84
+
85
+ /**
86
+ * Returns the index of the first element in the array where predicate is true, and -1
87
+ * otherwise.
88
+ * @param predicate find calls predicate once for each element of the array, in ascending
89
+ * order, until it finds one where predicate returns true. If such an element is found,
90
+ * findIndex immediately returns that element index. Otherwise, findIndex returns -1.
91
+ * @param thisArg If provided, it will be used as the this value for each invocation of
92
+ * predicate. If it is not provided, undefined is used instead.
93
+ */
94
+ findIndex(predicate: (value: TValue, index: number, obj: this) => boolean, thisArg?: any): number;
95
+
96
+ /**
97
+ * Performs the specified action for each element in an array.
98
+ * @param callbackfn A function that accepts up to three arguments. forEach calls the
99
+ * callbackfn function one time for each element in the array.
100
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function.
101
+ * If thisArg is omitted, undefined is used as the this value.
102
+ */
103
+ forEach(callbackfn: (value: TValue, index: number, array: this) => void, thisArg?: any): void;
104
+
105
+ /**
106
+ * Returns the index of the first occurrence of a value in an array.
107
+ * @param searchElement The value to locate in the array.
108
+ * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the
109
+ * search starts at index 0.
110
+ */
111
+ indexOf(searchElement: TValue, fromIndex?: number): number;
112
+
113
+ /**
114
+ * Adds all the elements of an array separated by the specified separator string.
115
+ * @param separator A string used to separate one element of an array from the next in the
116
+ * resulting String. If omitted, the array elements are separated with a comma.
117
+ */
118
+ join(separator?: string): string;
119
+
120
+ /**
121
+ * Returns the index of the last occurrence of a value in an array.
122
+ * @param searchElement The value to locate in the array.
123
+ * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the
124
+ * search starts at index 0.
125
+ */
126
+ lastIndexOf(searchElement: TValue, fromIndex?: number): number;
127
+
128
+ /**
129
+ * The length of the array.
130
+ */
131
+ readonly length: number;
132
+
133
+ /**
134
+ * Calls a defined callback function on each element of an array, and returns an array that
135
+ * contains the results.
136
+ * @param callbackfn A function that accepts up to three arguments. The map method calls the
137
+ * callbackfn function one time for each element in the array.
138
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function.
139
+ * If thisArg is omitted, undefined is used as the this value.
140
+ */
141
+ map(
142
+ callbackfn: (value: TValue, index: number, array: this) => TValue,
143
+ thisArg?: any
144
+ ): TypedArray<TArrayBuffer, TValue>;
145
+
146
+ /**
147
+ * Calls the specified callback function for all the elements in an array. The return value of
148
+ * the callback function is the accumulated result, and is provided as an argument in the next
149
+ * call to the callback function.
150
+ * @param callbackfn A function that accepts up to four arguments. The reduce method calls the
151
+ * callbackfn function one time for each element in the array.
152
+ * @param initialValue If initialValue is specified, it is used as the initial value to start
153
+ * the accumulation. The first call to the callbackfn function provides this value as an argument
154
+ * instead of an array value.
155
+ */
156
+ reduce(
157
+ callbackfn: (previousValue: TValue, currentValue: TValue, currentIndex: number, array: this) => number
158
+ ): number;
159
+ reduce(
160
+ callbackfn: (previousValue: TValue, currentValue: TValue, currentIndex: number, array: this) => number,
161
+ initialValue: number
162
+ ): number;
163
+
164
+ /**
165
+ * Calls the specified callback function for all the elements in an array. The return value of
166
+ * the callback function is the accumulated result, and is provided as an argument in the next
167
+ * call to the callback function.
168
+ * @param callbackfn A function that accepts up to four arguments. The reduce method calls the
169
+ * callbackfn function one time for each element in the array.
170
+ * @param initialValue If initialValue is specified, it is used as the initial value to start
171
+ * the accumulation. The first call to the callbackfn function provides this value as an argument
172
+ * instead of an array value.
173
+ */
174
+ reduce<U>(
175
+ callbackfn: (previousValue: U, currentValue: TValue, currentIndex: number, array: this) => U,
176
+ initialValue: U
177
+ ): U;
178
+
179
+ /**
180
+ * Calls the specified callback function for all the elements in an array, in descending order.
181
+ * The return value of the callback function is the accumulated result, and is provided as an
182
+ * argument in the next call to the callback function.
183
+ * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls
184
+ * the callbackfn function one time for each element in the array.
185
+ * @param initialValue If initialValue is specified, it is used as the initial value to start
186
+ * the accumulation. The first call to the callbackfn function provides this value as an
187
+ * argument instead of an array value.
188
+ */
189
+ reduceRight(
190
+ callbackfn: (previousValue: TValue, currentValue: TValue, currentIndex: number, array: this) => number
191
+ ): number;
192
+ reduceRight(
193
+ callbackfn: (previousValue: TValue, currentValue: TValue, currentIndex: number, array: this) => number,
194
+ initialValue: number
195
+ ): number;
196
+
197
+ /**
198
+ * Calls the specified callback function for all the elements in an array, in descending order.
199
+ * The return value of the callback function is the accumulated result, and is provided as an
200
+ * argument in the next call to the callback function.
201
+ * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls
202
+ * the callbackfn function one time for each element in the array.
203
+ * @param initialValue If initialValue is specified, it is used as the initial value to start
204
+ * the accumulation. The first call to the callbackfn function provides this value as an argument
205
+ * instead of an array value.
206
+ */
207
+ reduceRight<U>(
208
+ callbackfn: (previousValue: U, currentValue: TValue, currentIndex: TValue, array: this) => U,
209
+ initialValue: U
210
+ ): U;
211
+
212
+ /**
213
+ * Reverses the elements in an Array.
214
+ */
215
+ reverse(): this;
216
+
217
+ /**
218
+ * Sets a value or an array of values.
219
+ * @param array A typed or untyped array of values to set.
220
+ * @param offset The index in the current array at which the values are to be written.
221
+ */
222
+ set(array: ArrayLike<TValue>, offset?: number): void;
223
+
224
+ /**
225
+ * Returns a section of an array.
226
+ * @param start The beginning of the specified portion of the array.
227
+ * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
228
+ */
229
+ slice(start?: number, end?: number): TypedArray<TArrayBuffer, TValue>;
230
+
231
+ /**
232
+ * Determines whether the specified callback function returns true for any element of an array.
233
+ * @param predicate A function that accepts up to three arguments. The some method calls
234
+ * the predicate function for each element in the array until the predicate returns a value
235
+ * which is coercible to the Boolean value true, or until the end of the array.
236
+ * @param thisArg An object to which the this keyword can refer in the predicate function.
237
+ * If thisArg is omitted, undefined is used as the this value.
238
+ */
239
+ some(predicate: (value: TValue, index: number, array: this) => unknown, thisArg?: any): boolean;
240
+
241
+ /**
242
+ * Sorts an array.
243
+ * @param compareFn Function used to determine the order of the elements. It is expected to return
244
+ * a negative value if first argument is less than second argument, zero if they're equal and a positive
245
+ * value otherwise. If omitted, the elements are sorted in ascending order.
246
+ * ```ts
247
+ * [11,2,22,1].sort((a, b) => a - b)
248
+ * ```
249
+ */
250
+ sort(compareFn?: (a: TValue, b: TValue) => number): this;
251
+
252
+ /**
253
+ * Gets a new Int8Array view of the ArrayBuffer store for this array, referencing the elements
254
+ * at begin, inclusive, up to end, exclusive.
255
+ * @param begin The index of the beginning of the array.
256
+ * @param end The index of the end of the array.
257
+ */
258
+ subarray(begin?: number, end?: number): TypedArray<TArrayBuffer, TValue>;
259
+
260
+ /**
261
+ * Converts a number to a string by using the current locale.
262
+ */
263
+ toLocaleString(): string;
264
+
265
+ /**
266
+ * Returns a string representation of an array.
267
+ */
268
+ toString(): string;
269
+
270
+ /** Returns the primitive value of the specified object. */
271
+ valueOf(): this;
272
+
273
+ [index: number]: TValue;
274
+ }
275
+
276
+ export interface TypedArrayConstructor {
277
+ readonly prototype: TypedArray<ArrayBufferLike>;
278
+ new (length: number): TypedArray<ArrayBuffer>;
279
+ new (array: ArrayLike<number>): TypedArray<ArrayBuffer>;
280
+ new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(
281
+ buffer: TArrayBuffer,
282
+ byteOffset?: number,
283
+ length?: number
284
+ ): TypedArray<TArrayBuffer>;
285
+ new (array: ArrayLike<number> | ArrayBuffer): TypedArray<ArrayBuffer>;
286
+ readonly BYTES_PER_ELEMENT: number;
287
+ }
288
+
18
289
  /**
19
290
  * Grows a buffer if it isn't large enough
20
291
  * @returns The original buffer if resized successfully, or a newly created buffer
@@ -55,3 +326,94 @@ export function toUint8Array(buffer: ArrayBufferLike | ArrayBufferView): Uint8Ar
55
326
  if (!ArrayBuffer.isView(buffer)) return new Uint8Array(buffer);
56
327
  return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
57
328
  }
329
+
330
+ export function initView<T extends ArrayBufferLike = ArrayBuffer>(
331
+ view: Mutable<ArrayBufferView<T> & { BYTES_PER_ELEMENT?: number }>,
332
+ buffer?: T | ArrayBufferView<T> | ArrayLike<number> | number,
333
+ byteOffset?: number,
334
+ byteLength?: number
335
+ ) {
336
+ if (typeof buffer == 'number') {
337
+ const per = view.BYTES_PER_ELEMENT ?? _throw('BYTES_PER_ELEMENT is not set');
338
+ view.buffer = new ArrayBuffer(buffer * per) as T;
339
+ view.byteOffset = 0;
340
+ view.byteLength = buffer * per;
341
+ return;
342
+ }
343
+
344
+ if (
345
+ !buffer
346
+ || buffer instanceof ArrayBuffer
347
+ || (globalThis.SharedArrayBuffer && buffer instanceof globalThis.SharedArrayBuffer)
348
+ ) {
349
+ const { staticSize = 0 } = (view.constructor as any)?.[Symbol.metadata]?.struct ?? {};
350
+ view.buffer = buffer ?? (new ArrayBuffer(staticSize) as T);
351
+ view.byteOffset = byteOffset ?? 0;
352
+ view.byteLength = byteLength ?? staticSize;
353
+ return;
354
+ }
355
+
356
+ if (ArrayBuffer.isView(buffer)) {
357
+ view.buffer = buffer.buffer;
358
+ view.byteOffset = buffer.byteOffset;
359
+ view.byteLength = buffer.byteLength;
360
+ return;
361
+ }
362
+
363
+ const array = buffer as ArrayLike<number>;
364
+
365
+ view.buffer = new ArrayBuffer(array.length) as T;
366
+ view.byteOffset = 0;
367
+ view.byteLength = array.length;
368
+
369
+ const data = new Uint8Array(view.buffer);
370
+ for (let i = 0; i < array.length; i++) {
371
+ data[i] = array[i];
372
+ }
373
+ }
374
+
375
+ /** Creates a view of an array buffer */
376
+ export class BufferView<T extends ArrayBufferLike = ArrayBufferLike> implements ArrayBufferView<T> {
377
+ declare public readonly buffer: T;
378
+ declare public readonly byteOffset: number;
379
+ declare public readonly byteLength: number;
380
+
381
+ public constructor(buffer?: T | ArrayBufferView<T> | ArrayLike<number>, byteOffset?: number, byteLength?: number) {
382
+ initView<T>(this, buffer, byteOffset, byteLength);
383
+ }
384
+ }
385
+
386
+ BufferView satisfies ArrayBufferViewConstructor;
387
+
388
+ /** Creates an array of a buffer view type */
389
+ export function BufferViewArray<T extends ArrayBufferViewConstructor>(element: T, size: number) {
390
+ return class BufferViewArray<TArrayBuffer extends ArrayBufferLike = ArrayBuffer> extends Array {
391
+ public readonly BYTES_PER_ELEMENT: number = size;
392
+
393
+ declare public readonly buffer: TArrayBuffer;
394
+ declare public readonly byteOffset: number;
395
+ declare public readonly byteLength: number;
396
+
397
+ public constructor(
398
+ buffer?: TArrayBuffer | ArrayBufferView<TArrayBuffer> | ArrayLike<number>,
399
+ byteOffset?: number,
400
+ byteLength?: number
401
+ ) {
402
+ const t_size = size;
403
+
404
+ const length = (byteLength ?? t_size) / t_size;
405
+
406
+ if (!Number.isSafeInteger(length)) throw new Error('Invalid array length: ' + length);
407
+
408
+ super(length);
409
+
410
+ initView(this, buffer, byteOffset, byteLength);
411
+
412
+ for (let i = 0; i < length; i++) {
413
+ this[i] = new element(this.buffer, this.byteOffset + i * t_size, t_size);
414
+ }
415
+ }
416
+ };
417
+ }
418
+
419
+ BufferView satisfies ArrayBufferViewConstructor;
@@ -1,61 +1,135 @@
1
+ import type { ArrayBufferViewConstructor } from '../buffer.js';
1
2
  import { capitalize } from '../string.js';
3
+ import type { UnionToTuple } from '../types.js';
2
4
 
3
- type BitsToBytes = {
4
- '8': 1;
5
- '16': 2;
6
- '32': 4;
7
- '64': 8;
8
- '128': 16;
9
- };
10
-
11
- export type Size<T extends string> = T extends `${'int' | 'uint' | 'float'}${infer bits}`
12
- ? bits extends keyof BitsToBytes
13
- ? BitsToBytes[bits]
14
- : never
15
- : never;
16
-
17
- export const typeNames = [
18
- 'int8',
19
- 'uint8',
20
- 'int16',
21
- 'uint16',
22
- 'int32',
23
- 'uint32',
24
- 'int64',
25
- 'uint64',
26
- 'int128',
27
- 'uint128',
28
- 'float32',
29
- 'float64',
30
- 'float128',
31
- ] as const;
32
-
33
- export type Typename = (typeof typeNames)[number];
34
-
35
- export type Valid = Typename | Capitalize<Typename> | 'char';
5
+ /** A definition for a primitive type */
6
+ export interface Type<T = any> {
7
+ readonly name: string;
8
+ readonly size: number;
9
+ readonly array: ArrayBufferViewConstructor;
10
+ get(this: void, view: DataView, offset: number, littleEndian: boolean): T;
11
+ set(this: void, view: DataView, offset: number, littleEndian: boolean, value: T): void;
12
+ }
36
13
 
37
- export const validNames = [...typeNames, ...typeNames.map(t => capitalize(t)), 'char'] satisfies Valid[];
14
+ export function isType<T = any>(type: unknown): type is Type<T> {
15
+ return (
16
+ typeof type == 'object'
17
+ && type != null
18
+ && 'size' in type
19
+ && 'get' in type
20
+ && 'set' in type
21
+ && typeof type.size == 'number'
22
+ && typeof type.get == 'function'
23
+ && typeof type.set == 'function'
24
+ );
25
+ }
38
26
 
39
- export const regex = /^(u?int|float)(8|16|32|64|128)$/i;
27
+ export const types = {
28
+ int8: {
29
+ name: 'int8',
30
+ size: 1,
31
+ array: Int8Array,
32
+ get: (view, offset) => view.getInt8(offset),
33
+ set: (view, offset, _le, value) => view.setInt8(offset, value),
34
+ },
40
35
 
41
- export type Normalize<T extends Valid> = T extends 'char' ? 'uint8' : Uncapitalize<T>;
36
+ uint8: {
37
+ name: 'uint8',
38
+ size: 1,
39
+ array: Uint8Array,
40
+ get: (view, offset) => view.getUint8(offset),
41
+ set: (view, offset, _le, value) => view.setUint8(offset, value),
42
+ },
42
43
 
43
- export function normalize<T extends Valid>(type: T): Normalize<T> {
44
- return (type == 'char' ? 'uint8' : type.toLowerCase()) as Normalize<T>;
45
- }
44
+ int16: {
45
+ name: 'int16',
46
+ size: 2,
47
+ array: Int16Array,
48
+ get: (view, offset, le) => view.getInt16(offset, le),
49
+ set: (view, offset, le, value) => view.setInt16(offset, value, le),
50
+ },
51
+
52
+ uint16: {
53
+ name: 'uint16',
54
+ size: 2,
55
+ array: Uint16Array,
56
+ get: (view, offset, le) => view.getUint16(offset, le),
57
+ set: (view, offset, le, value) => view.setUint16(offset, value, le),
58
+ },
59
+
60
+ int32: {
61
+ name: 'int32',
62
+ size: 4,
63
+ array: Int32Array,
64
+ get: (view, offset, le) => view.getInt32(offset, le),
65
+ set: (view, offset, le, value) => view.setInt32(offset, value, le),
66
+ },
67
+
68
+ uint32: {
69
+ name: 'uint32',
70
+ size: 4,
71
+ array: Uint32Array,
72
+ get: (view, offset, le) => view.getUint32(offset, le),
73
+ set: (view, offset, le, value) => view.setUint32(offset, value, le),
74
+ },
75
+
76
+ int64: {
77
+ name: 'int64',
78
+ size: 8,
79
+ array: BigInt64Array,
80
+ get: (view, offset, le) => view.getBigInt64(offset, le),
81
+ set: (view, offset, le, value) => view.setBigInt64(offset, value, le),
82
+ },
83
+
84
+ uint64: {
85
+ name: 'uint64',
86
+ size: 8,
87
+ array: BigUint64Array,
88
+ get: (view, offset, le) => view.getBigUint64(offset, le),
89
+ set: (view, offset, le, value) => view.setBigUint64(offset, value, le),
90
+ },
91
+
92
+ float32: {
93
+ name: 'float32',
94
+ size: 4,
95
+ array: Float32Array,
96
+ get: (view, offset, le) => view.getFloat32(offset, le),
97
+ set: (view, offset, le, value) => view.setFloat32(offset, value, le),
98
+ },
46
99
 
47
- export function isType(type: { toString(): string }): type is Typename {
48
- return regex.test(type.toString());
100
+ float64: {
101
+ name: 'float64',
102
+ size: 8,
103
+ array: Float64Array,
104
+ get: (view, offset, le) => view.getFloat64(offset, le),
105
+ set: (view, offset, le, value) => view.setFloat64(offset, value, le),
106
+ },
107
+ } as const satisfies Record<string, Type>;
108
+
109
+ export type TypeName = keyof typeof types;
110
+
111
+ export const typeNames = Object.keys(types) as UnionToTuple<TypeName>;
112
+
113
+ export function isTypeName(type: { toString(): string }): type is TypeName {
114
+ return typeNames.includes(type.toString() as TypeName);
49
115
  }
50
116
 
117
+ export type Valid = TypeName | Capitalize<TypeName> | 'char';
118
+
119
+ export const validNames = [...typeNames, ...typeNames.map(t => capitalize(t)), 'char'] satisfies Valid[];
120
+
51
121
  export function isValid(type: { toString(): string }): type is Valid {
52
- return type == 'char' || regex.test(type.toString().toLowerCase());
122
+ return validNames.includes(type.toString() as Valid);
53
123
  }
54
124
 
55
125
  export function checkValid(type: { toString(): string }): asserts type is Valid {
56
- if (!isValid(type)) {
57
- throw new TypeError('Not a valid primitive type: ' + type);
58
- }
126
+ if (!isValid(type)) throw new TypeError('Not a valid primitive type: ' + type);
127
+ }
128
+
129
+ export type Normalize<T extends Valid> = (T extends 'char' ? 'uint8' : Uncapitalize<T>) & TypeName;
130
+
131
+ export function normalize<T extends Valid>(type: T): Normalize<T> {
132
+ return (type == 'char' ? 'uint8' : type.toLowerCase()) as Normalize<T>;
59
133
  }
60
134
 
61
- export const mask64 = BigInt('0xffffffffffffffff');
135
+ export type Size<T extends Valid | Type> = (T extends Valid ? (typeof types)[Normalize<T>] : T)['size'];