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/dist/buffer.d.ts CHANGED
@@ -1,5 +1,6 @@
1
+ import type { Mutable } from './objects.js';
1
2
  /**
2
- * A generic ArrayBufferView (typed array) constructor
3
+ * A generic constructor for an `ArrayBufferView`
3
4
  */
4
5
  export interface ArrayBufferViewConstructor {
5
6
  readonly prototype: ArrayBufferView<ArrayBufferLike>;
@@ -8,9 +9,347 @@ export interface ArrayBufferViewConstructor {
8
9
  new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): ArrayBufferView<TArrayBuffer>;
9
10
  new (array: ArrayLike<number> | ArrayBuffer): ArrayBufferView<ArrayBuffer>;
10
11
  }
12
+ /**
13
+ * A generic typed array.
14
+ */
15
+ export interface TypedArray<TArrayBuffer extends ArrayBufferLike = ArrayBuffer, TValue = number | bigint> extends ArrayBufferView<TArrayBuffer> {
16
+ /**
17
+ * The size in bytes of each element in the array.
18
+ */
19
+ readonly BYTES_PER_ELEMENT: number;
20
+ /**
21
+ * Returns the this object after copying a section of the array identified by start and end
22
+ * to the same array starting at position target
23
+ * @param target If target is negative, it is treated as length+target where length is the
24
+ * length of the array.
25
+ * @param start If start is negative, it is treated as length+start. If end is negative, it
26
+ * is treated as length+end.
27
+ * @param end If not specified, length of the this object is used as its default value.
28
+ */
29
+ copyWithin(target: number, start: number, end?: number): this;
30
+ /**
31
+ * Determines whether all the members of an array satisfy the specified test.
32
+ * @param predicate A function that accepts up to three arguments. The every method calls
33
+ * the predicate function for each element in the array until the predicate returns a value
34
+ * which is coercible to the Boolean value false, or until the end of the array.
35
+ * @param thisArg An object to which the this keyword can refer in the predicate function.
36
+ * If thisArg is omitted, undefined is used as the this value.
37
+ */
38
+ every(predicate: (value: TValue, index: number, array: this) => unknown, thisArg?: any): boolean;
39
+ /**
40
+ * Changes all array elements from `start` to `end` index to a static `value` and returns the modified array
41
+ * @param value value to fill array section with
42
+ * @param start index to start filling the array at. If start is negative, it is treated as
43
+ * length+start where length is the length of the array.
44
+ * @param end index to stop filling the array at. If end is negative, it is treated as
45
+ * length+end.
46
+ */
47
+ fill(value: TValue, start?: number, end?: number): this;
48
+ /**
49
+ * Returns the elements of an array that meet the condition specified in a callback function.
50
+ * @param predicate A function that accepts up to three arguments. The filter method calls
51
+ * the predicate function one time for each element in the array.
52
+ * @param thisArg An object to which the this keyword can refer in the predicate function.
53
+ * If thisArg is omitted, undefined is used as the this value.
54
+ */
55
+ filter(predicate: (value: TValue, index: number, array: this) => any, thisArg?: any): TypedArray<TArrayBuffer, TValue>;
56
+ /**
57
+ * Returns the value of the first element in the array where predicate is true, and undefined
58
+ * otherwise.
59
+ * @param predicate find calls predicate once for each element of the array, in ascending
60
+ * order, until it finds one where predicate returns true. If such an element is found, find
61
+ * immediately returns that element value. Otherwise, find returns undefined.
62
+ * @param thisArg If provided, it will be used as the this value for each invocation of
63
+ * predicate. If it is not provided, undefined is used instead.
64
+ */
65
+ find(predicate: (value: TValue, index: number, obj: this) => boolean, thisArg?: any): TValue | undefined;
66
+ /**
67
+ * Returns the index of the first element in the array where predicate is true, and -1
68
+ * otherwise.
69
+ * @param predicate find calls predicate once for each element of the array, in ascending
70
+ * order, until it finds one where predicate returns true. If such an element is found,
71
+ * findIndex immediately returns that element index. Otherwise, findIndex returns -1.
72
+ * @param thisArg If provided, it will be used as the this value for each invocation of
73
+ * predicate. If it is not provided, undefined is used instead.
74
+ */
75
+ findIndex(predicate: (value: TValue, index: number, obj: this) => boolean, thisArg?: any): number;
76
+ /**
77
+ * Performs the specified action for each element in an array.
78
+ * @param callbackfn A function that accepts up to three arguments. forEach calls the
79
+ * callbackfn function one time for each element in the array.
80
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function.
81
+ * If thisArg is omitted, undefined is used as the this value.
82
+ */
83
+ forEach(callbackfn: (value: TValue, index: number, array: this) => void, thisArg?: any): void;
84
+ /**
85
+ * Returns the index of the first occurrence of a value in an array.
86
+ * @param searchElement The value to locate in the array.
87
+ * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the
88
+ * search starts at index 0.
89
+ */
90
+ indexOf(searchElement: TValue, fromIndex?: number): number;
91
+ /**
92
+ * Adds all the elements of an array separated by the specified separator string.
93
+ * @param separator A string used to separate one element of an array from the next in the
94
+ * resulting String. If omitted, the array elements are separated with a comma.
95
+ */
96
+ join(separator?: string): string;
97
+ /**
98
+ * Returns the index of the last occurrence of a value in an array.
99
+ * @param searchElement The value to locate in the array.
100
+ * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the
101
+ * search starts at index 0.
102
+ */
103
+ lastIndexOf(searchElement: TValue, fromIndex?: number): number;
104
+ /**
105
+ * The length of the array.
106
+ */
107
+ readonly length: number;
108
+ /**
109
+ * Calls a defined callback function on each element of an array, and returns an array that
110
+ * contains the results.
111
+ * @param callbackfn A function that accepts up to three arguments. The map method calls the
112
+ * callbackfn function one time for each element in the array.
113
+ * @param thisArg An object to which the this keyword can refer in the callbackfn function.
114
+ * If thisArg is omitted, undefined is used as the this value.
115
+ */
116
+ map(callbackfn: (value: TValue, index: number, array: this) => TValue, thisArg?: any): TypedArray<TArrayBuffer, TValue>;
117
+ /**
118
+ * Calls the specified callback function for all the elements in an array. The return value of
119
+ * the callback function is the accumulated result, and is provided as an argument in the next
120
+ * call to the callback function.
121
+ * @param callbackfn A function that accepts up to four arguments. The reduce method calls the
122
+ * callbackfn function one time for each element in the array.
123
+ * @param initialValue If initialValue is specified, it is used as the initial value to start
124
+ * the accumulation. The first call to the callbackfn function provides this value as an argument
125
+ * instead of an array value.
126
+ */
127
+ reduce(callbackfn: (previousValue: TValue, currentValue: TValue, currentIndex: number, array: this) => number): number;
128
+ reduce(callbackfn: (previousValue: TValue, currentValue: TValue, currentIndex: number, array: this) => number, initialValue: number): number;
129
+ /**
130
+ * Calls the specified callback function for all the elements in an array. The return value of
131
+ * the callback function is the accumulated result, and is provided as an argument in the next
132
+ * call to the callback function.
133
+ * @param callbackfn A function that accepts up to four arguments. The reduce method calls the
134
+ * callbackfn function one time for each element in the array.
135
+ * @param initialValue If initialValue is specified, it is used as the initial value to start
136
+ * the accumulation. The first call to the callbackfn function provides this value as an argument
137
+ * instead of an array value.
138
+ */
139
+ reduce<U>(callbackfn: (previousValue: U, currentValue: TValue, currentIndex: number, array: this) => U, initialValue: U): U;
140
+ /**
141
+ * Calls the specified callback function for all the elements in an array, in descending order.
142
+ * The return value of the callback function is the accumulated result, and is provided as an
143
+ * argument in the next call to the callback function.
144
+ * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls
145
+ * the callbackfn function one time for each element in the array.
146
+ * @param initialValue If initialValue is specified, it is used as the initial value to start
147
+ * the accumulation. The first call to the callbackfn function provides this value as an
148
+ * argument instead of an array value.
149
+ */
150
+ reduceRight(callbackfn: (previousValue: TValue, currentValue: TValue, currentIndex: number, array: this) => number): number;
151
+ reduceRight(callbackfn: (previousValue: TValue, currentValue: TValue, currentIndex: number, array: this) => number, initialValue: number): number;
152
+ /**
153
+ * Calls the specified callback function for all the elements in an array, in descending order.
154
+ * The return value of the callback function is the accumulated result, and is provided as an
155
+ * argument in the next call to the callback function.
156
+ * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls
157
+ * the callbackfn function one time for each element in the array.
158
+ * @param initialValue If initialValue is specified, it is used as the initial value to start
159
+ * the accumulation. The first call to the callbackfn function provides this value as an argument
160
+ * instead of an array value.
161
+ */
162
+ reduceRight<U>(callbackfn: (previousValue: U, currentValue: TValue, currentIndex: TValue, array: this) => U, initialValue: U): U;
163
+ /**
164
+ * Reverses the elements in an Array.
165
+ */
166
+ reverse(): this;
167
+ /**
168
+ * Sets a value or an array of values.
169
+ * @param array A typed or untyped array of values to set.
170
+ * @param offset The index in the current array at which the values are to be written.
171
+ */
172
+ set(array: ArrayLike<TValue>, offset?: number): void;
173
+ /**
174
+ * Returns a section of an array.
175
+ * @param start The beginning of the specified portion of the array.
176
+ * @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
177
+ */
178
+ slice(start?: number, end?: number): TypedArray<TArrayBuffer, TValue>;
179
+ /**
180
+ * Determines whether the specified callback function returns true for any element of an array.
181
+ * @param predicate A function that accepts up to three arguments. The some method calls
182
+ * the predicate function for each element in the array until the predicate returns a value
183
+ * which is coercible to the Boolean value true, or until the end of the array.
184
+ * @param thisArg An object to which the this keyword can refer in the predicate function.
185
+ * If thisArg is omitted, undefined is used as the this value.
186
+ */
187
+ some(predicate: (value: TValue, index: number, array: this) => unknown, thisArg?: any): boolean;
188
+ /**
189
+ * Sorts an array.
190
+ * @param compareFn Function used to determine the order of the elements. It is expected to return
191
+ * a negative value if first argument is less than second argument, zero if they're equal and a positive
192
+ * value otherwise. If omitted, the elements are sorted in ascending order.
193
+ * ```ts
194
+ * [11,2,22,1].sort((a, b) => a - b)
195
+ * ```
196
+ */
197
+ sort(compareFn?: (a: TValue, b: TValue) => number): this;
198
+ /**
199
+ * Gets a new Int8Array view of the ArrayBuffer store for this array, referencing the elements
200
+ * at begin, inclusive, up to end, exclusive.
201
+ * @param begin The index of the beginning of the array.
202
+ * @param end The index of the end of the array.
203
+ */
204
+ subarray(begin?: number, end?: number): TypedArray<TArrayBuffer, TValue>;
205
+ /**
206
+ * Converts a number to a string by using the current locale.
207
+ */
208
+ toLocaleString(): string;
209
+ /**
210
+ * Returns a string representation of an array.
211
+ */
212
+ toString(): string;
213
+ /** Returns the primitive value of the specified object. */
214
+ valueOf(): this;
215
+ [index: number]: TValue;
216
+ }
217
+ export interface TypedArrayConstructor {
218
+ readonly prototype: TypedArray<ArrayBufferLike>;
219
+ new (length: number): TypedArray<ArrayBuffer>;
220
+ new (array: ArrayLike<number>): TypedArray<ArrayBuffer>;
221
+ new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer: TArrayBuffer, byteOffset?: number, length?: number): TypedArray<TArrayBuffer>;
222
+ new (array: ArrayLike<number> | ArrayBuffer): TypedArray<ArrayBuffer>;
223
+ readonly BYTES_PER_ELEMENT: number;
224
+ }
11
225
  /**
12
226
  * Grows a buffer if it isn't large enough
13
227
  * @returns The original buffer if resized successfully, or a newly created buffer
14
228
  */
15
229
  export declare function extendBuffer<T extends ArrayBufferLike | ArrayBufferView>(buffer: T, newByteLength: number): T;
16
230
  export declare function toUint8Array(buffer: ArrayBufferLike | ArrayBufferView): Uint8Array;
231
+ export declare function initView<T extends ArrayBufferLike = ArrayBuffer>(view: Mutable<ArrayBufferView<T> & {
232
+ BYTES_PER_ELEMENT?: number;
233
+ }>, buffer?: T | ArrayBufferView<T> | ArrayLike<number> | number, byteOffset?: number, byteLength?: number): void;
234
+ /** Creates a view of an array buffer */
235
+ export declare class BufferView<T extends ArrayBufferLike = ArrayBufferLike> implements ArrayBufferView<T> {
236
+ readonly buffer: T;
237
+ readonly byteOffset: number;
238
+ readonly byteLength: number;
239
+ constructor(buffer?: T | ArrayBufferView<T> | ArrayLike<number>, byteOffset?: number, byteLength?: number);
240
+ }
241
+ /** Creates an array of a buffer view type */
242
+ export declare function BufferViewArray<T extends ArrayBufferViewConstructor>(element: T, size: number): {
243
+ new <TArrayBuffer extends ArrayBufferLike = ArrayBuffer>(buffer?: TArrayBuffer | ArrayBufferView<TArrayBuffer> | ArrayLike<number>, byteOffset?: number, byteLength?: number): {
244
+ [n: number]: any;
245
+ readonly BYTES_PER_ELEMENT: number;
246
+ readonly buffer: TArrayBuffer;
247
+ readonly byteOffset: number;
248
+ readonly byteLength: number;
249
+ length: number;
250
+ toString(): string;
251
+ toLocaleString(): string;
252
+ toLocaleString(locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string;
253
+ pop(): any;
254
+ push(...items: any[]): number;
255
+ concat(...items: ConcatArray<any>[]): any[];
256
+ concat(...items: any[]): any[];
257
+ join(separator?: string): string;
258
+ reverse(): any[];
259
+ shift(): any;
260
+ slice(start?: number, end?: number): any[];
261
+ sort(compareFn?: ((a: any, b: any) => number) | undefined): /*elided*/ any;
262
+ splice(start: number, deleteCount?: number): any[];
263
+ splice(start: number, deleteCount: number, ...items: any[]): any[];
264
+ unshift(...items: any[]): number;
265
+ indexOf(searchElement: any, fromIndex?: number): number;
266
+ lastIndexOf(searchElement: any, fromIndex?: number): number;
267
+ every<S extends any>(predicate: (value: any, index: number, array: any[]) => value is S, thisArg?: any): this is S[];
268
+ every(predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any): boolean;
269
+ some(predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any): boolean;
270
+ forEach(callbackfn: (value: any, index: number, array: any[]) => void, thisArg?: any): void;
271
+ map<U>(callbackfn: (value: any, index: number, array: any[]) => U, thisArg?: any): U[];
272
+ filter<S extends any>(predicate: (value: any, index: number, array: any[]) => value is S, thisArg?: any): S[];
273
+ filter(predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any): any[];
274
+ reduce(callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any;
275
+ reduce(callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any;
276
+ reduce<U>(callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U;
277
+ reduceRight(callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any): any;
278
+ reduceRight(callbackfn: (previousValue: any, currentValue: any, currentIndex: number, array: any[]) => any, initialValue: any): any;
279
+ reduceRight<U>(callbackfn: (previousValue: U, currentValue: any, currentIndex: number, array: any[]) => U, initialValue: U): U;
280
+ find<S extends any>(predicate: (value: any, index: number, obj: any[]) => value is S, thisArg?: any): S | undefined;
281
+ find(predicate: (value: any, index: number, obj: any[]) => unknown, thisArg?: any): any;
282
+ findIndex(predicate: (value: any, index: number, obj: any[]) => unknown, thisArg?: any): number;
283
+ fill(value: any, start?: number, end?: number): /*elided*/ any;
284
+ copyWithin(target: number, start: number, end?: number): /*elided*/ any;
285
+ entries(): ArrayIterator<[number, any]>;
286
+ keys(): ArrayIterator<number>;
287
+ values(): ArrayIterator<any>;
288
+ includes(searchElement: any, fromIndex?: number): boolean;
289
+ flatMap<U, This = undefined>(callback: (this: This, value: any, index: number, array: any[]) => U | readonly U[], thisArg?: This | undefined): U[];
290
+ flat<A, D extends number = 1>(this: A, depth?: D | undefined): FlatArray<A, D>[];
291
+ at(index: number): any;
292
+ findLast<S extends any>(predicate: (value: any, index: number, array: any[]) => value is S, thisArg?: any): S | undefined;
293
+ findLast(predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any): any;
294
+ findLastIndex(predicate: (value: any, index: number, array: any[]) => unknown, thisArg?: any): number;
295
+ toReversed(): any[];
296
+ toSorted(compareFn?: ((a: any, b: any) => number) | undefined): any[];
297
+ toSpliced(start: number, deleteCount: number, ...items: any[]): any[];
298
+ toSpliced(start: number, deleteCount?: number): any[];
299
+ with(index: number, value: any): any[];
300
+ [Symbol.iterator](): ArrayIterator<any>;
301
+ readonly [Symbol.unscopables]: {
302
+ [x: number]: boolean | undefined;
303
+ length?: boolean | undefined;
304
+ toString?: boolean | undefined;
305
+ toLocaleString?: boolean | undefined;
306
+ pop?: boolean | undefined;
307
+ push?: boolean | undefined;
308
+ concat?: boolean | undefined;
309
+ join?: boolean | undefined;
310
+ reverse?: boolean | undefined;
311
+ shift?: boolean | undefined;
312
+ slice?: boolean | undefined;
313
+ sort?: boolean | undefined;
314
+ splice?: boolean | undefined;
315
+ unshift?: boolean | undefined;
316
+ indexOf?: boolean | undefined;
317
+ lastIndexOf?: boolean | undefined;
318
+ every?: boolean | undefined;
319
+ some?: boolean | undefined;
320
+ forEach?: boolean | undefined;
321
+ map?: boolean | undefined;
322
+ filter?: boolean | undefined;
323
+ reduce?: boolean | undefined;
324
+ reduceRight?: boolean | undefined;
325
+ find?: boolean | undefined;
326
+ findIndex?: boolean | undefined;
327
+ fill?: boolean | undefined;
328
+ copyWithin?: boolean | undefined;
329
+ entries?: boolean | undefined;
330
+ keys?: boolean | undefined;
331
+ values?: boolean | undefined;
332
+ includes?: boolean | undefined;
333
+ flatMap?: boolean | undefined;
334
+ flat?: boolean | undefined;
335
+ at?: boolean | undefined;
336
+ findLast?: boolean | undefined;
337
+ findLastIndex?: boolean | undefined;
338
+ toReversed?: boolean | undefined;
339
+ toSorted?: boolean | undefined;
340
+ toSpliced?: boolean | undefined;
341
+ with?: boolean | undefined;
342
+ [Symbol.iterator]?: boolean | undefined;
343
+ readonly [Symbol.unscopables]?: boolean | undefined;
344
+ };
345
+ };
346
+ isArray(arg: any): arg is any[];
347
+ from<T_1>(arrayLike: ArrayLike<T_1>): T_1[];
348
+ from<T_1, U>(arrayLike: ArrayLike<T_1>, mapfn: (v: T_1, k: number) => U, thisArg?: any): U[];
349
+ from<T_1>(iterable: Iterable<T_1> | ArrayLike<T_1>): T_1[];
350
+ from<T_1, U>(iterable: Iterable<T_1> | ArrayLike<T_1>, mapfn: (v: T_1, k: number) => U, thisArg?: any): U[];
351
+ of<T_1>(...items: T_1[]): T_1[];
352
+ fromAsync<T_1>(iterableOrArrayLike: AsyncIterable<T_1> | Iterable<T_1 | PromiseLike<T_1>> | ArrayLike<T_1 | PromiseLike<T_1>>): Promise<T_1[]>;
353
+ fromAsync<T_1, U>(iterableOrArrayLike: AsyncIterable<T_1> | Iterable<T_1> | ArrayLike<T_1>, mapFn: (value: Awaited<T_1>, index: number) => U, thisArg?: any): Promise<Awaited<U>[]>;
354
+ readonly [Symbol.species]: ArrayConstructor;
355
+ };
package/dist/buffer.js CHANGED
@@ -1,4 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-expressions */
2
+ import { _throw } from './misc.js';
2
3
  /**
3
4
  * Grows a buffer if it isn't large enough
4
5
  * @returns The original buffer if resized successfully, or a newly created buffer
@@ -37,3 +38,60 @@ export function toUint8Array(buffer) {
37
38
  return new Uint8Array(buffer);
38
39
  return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
39
40
  }
41
+ export function initView(view, buffer, byteOffset, byteLength) {
42
+ if (typeof buffer == 'number') {
43
+ const per = view.BYTES_PER_ELEMENT ?? _throw('BYTES_PER_ELEMENT is not set');
44
+ view.buffer = new ArrayBuffer(buffer * per);
45
+ view.byteOffset = 0;
46
+ view.byteLength = buffer * per;
47
+ return;
48
+ }
49
+ if (!buffer
50
+ || buffer instanceof ArrayBuffer
51
+ || (globalThis.SharedArrayBuffer && buffer instanceof globalThis.SharedArrayBuffer)) {
52
+ const { staticSize = 0 } = view.constructor?.[Symbol.metadata]?.struct ?? {};
53
+ view.buffer = buffer ?? new ArrayBuffer(staticSize);
54
+ view.byteOffset = byteOffset ?? 0;
55
+ view.byteLength = byteLength ?? staticSize;
56
+ return;
57
+ }
58
+ if (ArrayBuffer.isView(buffer)) {
59
+ view.buffer = buffer.buffer;
60
+ view.byteOffset = buffer.byteOffset;
61
+ view.byteLength = buffer.byteLength;
62
+ return;
63
+ }
64
+ const array = buffer;
65
+ view.buffer = new ArrayBuffer(array.length);
66
+ view.byteOffset = 0;
67
+ view.byteLength = array.length;
68
+ const data = new Uint8Array(view.buffer);
69
+ for (let i = 0; i < array.length; i++) {
70
+ data[i] = array[i];
71
+ }
72
+ }
73
+ /** Creates a view of an array buffer */
74
+ export class BufferView {
75
+ constructor(buffer, byteOffset, byteLength) {
76
+ initView(this, buffer, byteOffset, byteLength);
77
+ }
78
+ }
79
+ BufferView;
80
+ /** Creates an array of a buffer view type */
81
+ export function BufferViewArray(element, size) {
82
+ return class BufferViewArray extends Array {
83
+ BYTES_PER_ELEMENT = size;
84
+ constructor(buffer, byteOffset, byteLength) {
85
+ const t_size = size;
86
+ const length = (byteLength ?? t_size) / t_size;
87
+ if (!Number.isSafeInteger(length))
88
+ throw new Error('Invalid array length: ' + length);
89
+ super(length);
90
+ initView(this, buffer, byteOffset, byteLength);
91
+ for (let i = 0; i < length; i++) {
92
+ this[i] = new element(this.buffer, this.byteOffset + i * t_size, t_size);
93
+ }
94
+ }
95
+ };
96
+ }
97
+ BufferView;
@@ -1,26 +1,99 @@
1
- type BitsToBytes = {
2
- '8': 1;
3
- '16': 2;
4
- '32': 4;
5
- '64': 8;
6
- '128': 16;
1
+ import type { ArrayBufferViewConstructor } from '../buffer.js';
2
+ import type { UnionToTuple } from '../types.js';
3
+ /** A definition for a primitive type */
4
+ export interface Type<T = any> {
5
+ readonly name: string;
6
+ readonly size: number;
7
+ readonly array: ArrayBufferViewConstructor;
8
+ get(this: void, view: DataView, offset: number, littleEndian: boolean): T;
9
+ set(this: void, view: DataView, offset: number, littleEndian: boolean, value: T): void;
10
+ }
11
+ export declare function isType<T = any>(type: unknown): type is Type<T>;
12
+ export declare const types: {
13
+ readonly int8: {
14
+ readonly name: "int8";
15
+ readonly size: 1;
16
+ readonly array: Int8ArrayConstructor;
17
+ readonly get: (this: void, view: DataView<ArrayBufferLike>, offset: number) => number;
18
+ readonly set: (this: void, view: DataView<ArrayBufferLike>, offset: number, _le: boolean, value: any) => void;
19
+ };
20
+ readonly uint8: {
21
+ readonly name: "uint8";
22
+ readonly size: 1;
23
+ readonly array: Uint8ArrayConstructor;
24
+ readonly get: (this: void, view: DataView<ArrayBufferLike>, offset: number) => number;
25
+ readonly set: (this: void, view: DataView<ArrayBufferLike>, offset: number, _le: boolean, value: any) => void;
26
+ };
27
+ readonly int16: {
28
+ readonly name: "int16";
29
+ readonly size: 2;
30
+ readonly array: Int16ArrayConstructor;
31
+ readonly get: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean) => number;
32
+ readonly set: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean, value: any) => void;
33
+ };
34
+ readonly uint16: {
35
+ readonly name: "uint16";
36
+ readonly size: 2;
37
+ readonly array: Uint16ArrayConstructor;
38
+ readonly get: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean) => number;
39
+ readonly set: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean, value: any) => void;
40
+ };
41
+ readonly int32: {
42
+ readonly name: "int32";
43
+ readonly size: 4;
44
+ readonly array: Int32ArrayConstructor;
45
+ readonly get: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean) => number;
46
+ readonly set: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean, value: any) => void;
47
+ };
48
+ readonly uint32: {
49
+ readonly name: "uint32";
50
+ readonly size: 4;
51
+ readonly array: Uint32ArrayConstructor;
52
+ readonly get: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean) => number;
53
+ readonly set: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean, value: any) => void;
54
+ };
55
+ readonly int64: {
56
+ readonly name: "int64";
57
+ readonly size: 8;
58
+ readonly array: BigInt64ArrayConstructor;
59
+ readonly get: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean) => bigint;
60
+ readonly set: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean, value: any) => void;
61
+ };
62
+ readonly uint64: {
63
+ readonly name: "uint64";
64
+ readonly size: 8;
65
+ readonly array: BigUint64ArrayConstructor;
66
+ readonly get: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean) => bigint;
67
+ readonly set: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean, value: any) => void;
68
+ };
69
+ readonly float32: {
70
+ readonly name: "float32";
71
+ readonly size: 4;
72
+ readonly array: Float32ArrayConstructor;
73
+ readonly get: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean) => number;
74
+ readonly set: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean, value: any) => void;
75
+ };
76
+ readonly float64: {
77
+ readonly name: "float64";
78
+ readonly size: 8;
79
+ readonly array: Float64ArrayConstructor;
80
+ readonly get: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean) => number;
81
+ readonly set: (this: void, view: DataView<ArrayBufferLike>, offset: number, le: boolean, value: any) => void;
82
+ };
7
83
  };
8
- export type Size<T extends string> = T extends `${'int' | 'uint' | 'float'}${infer bits}` ? bits extends keyof BitsToBytes ? BitsToBytes[bits] : never : never;
9
- export declare const typeNames: readonly ["int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64", "int128", "uint128", "float32", "float64", "float128"];
10
- export type Typename = (typeof typeNames)[number];
11
- export type Valid = Typename | Capitalize<Typename> | 'char';
12
- export declare const validNames: ("int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "int64" | "uint64" | "int128" | "uint128" | "float32" | "float64" | "float128" | "Int8" | "Uint8" | "Int16" | "Uint16" | "Int32" | "Uint32" | "Int64" | "Uint64" | "Int128" | "Uint128" | "Float32" | "Float64" | "Float128" | "char")[];
13
- export declare const regex: RegExp;
14
- export type Normalize<T extends Valid> = T extends 'char' ? 'uint8' : Uncapitalize<T>;
15
- export declare function normalize<T extends Valid>(type: T): Normalize<T>;
16
- export declare function isType(type: {
84
+ export type TypeName = keyof typeof types;
85
+ export declare const typeNames: UnionToTuple<TypeName>;
86
+ export declare function isTypeName(type: {
17
87
  toString(): string;
18
- }): type is Typename;
88
+ }): type is TypeName;
89
+ export type Valid = TypeName | Capitalize<TypeName> | 'char';
90
+ export declare const validNames: ("int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "int64" | "uint64" | "float32" | "float64" | "Int8" | "Uint8" | "Int16" | "Uint16" | "Int32" | "Uint32" | "Int64" | "Uint64" | "Float32" | "Float64" | "char")[];
19
91
  export declare function isValid(type: {
20
92
  toString(): string;
21
93
  }): type is Valid;
22
94
  export declare function checkValid(type: {
23
95
  toString(): string;
24
96
  }): asserts type is Valid;
25
- export declare const mask64: bigint;
26
- export {};
97
+ export type Normalize<T extends Valid> = (T extends 'char' ? 'uint8' : Uncapitalize<T>) & TypeName;
98
+ export declare function normalize<T extends Valid>(type: T): Normalize<T>;
99
+ export type Size<T extends Valid | Type> = (T extends Valid ? (typeof types)[Normalize<T>] : T)['size'];
@@ -1,33 +1,98 @@
1
1
  import { capitalize } from '../string.js';
2
- export const typeNames = [
3
- 'int8',
4
- 'uint8',
5
- 'int16',
6
- 'uint16',
7
- 'int32',
8
- 'uint32',
9
- 'int64',
10
- 'uint64',
11
- 'int128',
12
- 'uint128',
13
- 'float32',
14
- 'float64',
15
- 'float128',
16
- ];
17
- export const validNames = [...typeNames, ...typeNames.map(t => capitalize(t)), 'char'];
18
- export const regex = /^(u?int|float)(8|16|32|64|128)$/i;
19
- export function normalize(type) {
20
- return (type == 'char' ? 'uint8' : type.toLowerCase());
21
- }
22
2
  export function isType(type) {
23
- return regex.test(type.toString());
3
+ return (typeof type == 'object'
4
+ && type != null
5
+ && 'size' in type
6
+ && 'get' in type
7
+ && 'set' in type
8
+ && typeof type.size == 'number'
9
+ && typeof type.get == 'function'
10
+ && typeof type.set == 'function');
11
+ }
12
+ export const types = {
13
+ int8: {
14
+ name: 'int8',
15
+ size: 1,
16
+ array: Int8Array,
17
+ get: (view, offset) => view.getInt8(offset),
18
+ set: (view, offset, _le, value) => view.setInt8(offset, value),
19
+ },
20
+ uint8: {
21
+ name: 'uint8',
22
+ size: 1,
23
+ array: Uint8Array,
24
+ get: (view, offset) => view.getUint8(offset),
25
+ set: (view, offset, _le, value) => view.setUint8(offset, value),
26
+ },
27
+ int16: {
28
+ name: 'int16',
29
+ size: 2,
30
+ array: Int16Array,
31
+ get: (view, offset, le) => view.getInt16(offset, le),
32
+ set: (view, offset, le, value) => view.setInt16(offset, value, le),
33
+ },
34
+ uint16: {
35
+ name: 'uint16',
36
+ size: 2,
37
+ array: Uint16Array,
38
+ get: (view, offset, le) => view.getUint16(offset, le),
39
+ set: (view, offset, le, value) => view.setUint16(offset, value, le),
40
+ },
41
+ int32: {
42
+ name: 'int32',
43
+ size: 4,
44
+ array: Int32Array,
45
+ get: (view, offset, le) => view.getInt32(offset, le),
46
+ set: (view, offset, le, value) => view.setInt32(offset, value, le),
47
+ },
48
+ uint32: {
49
+ name: 'uint32',
50
+ size: 4,
51
+ array: Uint32Array,
52
+ get: (view, offset, le) => view.getUint32(offset, le),
53
+ set: (view, offset, le, value) => view.setUint32(offset, value, le),
54
+ },
55
+ int64: {
56
+ name: 'int64',
57
+ size: 8,
58
+ array: BigInt64Array,
59
+ get: (view, offset, le) => view.getBigInt64(offset, le),
60
+ set: (view, offset, le, value) => view.setBigInt64(offset, value, le),
61
+ },
62
+ uint64: {
63
+ name: 'uint64',
64
+ size: 8,
65
+ array: BigUint64Array,
66
+ get: (view, offset, le) => view.getBigUint64(offset, le),
67
+ set: (view, offset, le, value) => view.setBigUint64(offset, value, le),
68
+ },
69
+ float32: {
70
+ name: 'float32',
71
+ size: 4,
72
+ array: Float32Array,
73
+ get: (view, offset, le) => view.getFloat32(offset, le),
74
+ set: (view, offset, le, value) => view.setFloat32(offset, value, le),
75
+ },
76
+ float64: {
77
+ name: 'float64',
78
+ size: 8,
79
+ array: Float64Array,
80
+ get: (view, offset, le) => view.getFloat64(offset, le),
81
+ set: (view, offset, le, value) => view.setFloat64(offset, value, le),
82
+ },
83
+ };
84
+ export const typeNames = Object.keys(types);
85
+ export function isTypeName(type) {
86
+ return typeNames.includes(type.toString());
24
87
  }
88
+ export const validNames = [...typeNames, ...typeNames.map(t => capitalize(t)), 'char'];
25
89
  export function isValid(type) {
26
- return type == 'char' || regex.test(type.toString().toLowerCase());
90
+ return validNames.includes(type.toString());
27
91
  }
28
92
  export function checkValid(type) {
29
- if (!isValid(type)) {
93
+ if (!isValid(type))
30
94
  throw new TypeError('Not a valid primitive type: ' + type);
31
- }
32
95
  }
33
- export const mask64 = BigInt('0xffffffffffffffff');
96
+ export function normalize(type) {
97
+ return (type == 'char' ? 'uint8' : type.toLowerCase());
98
+ }