utilium 0.7.0 → 0.7.2
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/internal/primitives.d.ts +3 -0
- package/dist/internal/primitives.js +6 -1
- package/dist/internal/struct.d.ts +2 -0
- package/dist/internal/struct.js +10 -0
- package/dist/list.js +8 -8
- package/dist/struct.js +8 -16
- package/package.json +1 -1
- package/src/internal/primitives.ts +7 -1
- package/src/internal/struct.ts +12 -0
- package/src/list.ts +8 -8
- package/src/struct.ts +12 -20
@@ -1,7 +1,7 @@
|
|
1
1
|
import { capitalize } from '../string.js';
|
2
2
|
export const types = ['int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'float32', 'float64'];
|
3
3
|
export const valids = [...types, ...types.map(t => capitalize(t)), 'char'];
|
4
|
-
export const regex = /^(u?int)(8|16|32|64)
|
4
|
+
export const regex = /^(u?int|float)(8|16|32|64)$/i;
|
5
5
|
export function normalize(type) {
|
6
6
|
return (type == 'char' ? 'uint8' : type.toLowerCase());
|
7
7
|
}
|
@@ -11,3 +11,8 @@ export function isType(type) {
|
|
11
11
|
export function isValid(type) {
|
12
12
|
return type == 'char' || regex.test(type.toString().toLowerCase());
|
13
13
|
}
|
14
|
+
export function checkValid(type) {
|
15
|
+
if (!isValid(type)) {
|
16
|
+
throw new TypeError('Not a valid primitive type: ' + type);
|
17
|
+
}
|
18
|
+
}
|
@@ -57,6 +57,8 @@ export interface InstanceLike<T extends Metadata = Metadata> {
|
|
57
57
|
constructor: StaticLike<T>;
|
58
58
|
}
|
59
59
|
export declare function isInstance<T extends Metadata = Metadata>(arg: unknown): arg is Instance<T>;
|
60
|
+
export declare function checkInstance<T extends Metadata = Metadata>(arg: unknown): asserts arg is Instance<T>;
|
60
61
|
export declare function isStruct<T extends Metadata = Metadata>(arg: unknown): arg is Instance<T> | Static<T>;
|
62
|
+
export declare function checkStruct<T extends Metadata = Metadata>(arg: unknown): asserts arg is Instance<T> | Static<T>;
|
61
63
|
export type Like<T extends Metadata = Metadata> = InstanceLike<T> | StaticLike<T>;
|
62
64
|
export type Size<T extends primitive.Valid | StaticLike | InstanceLike> = T extends primitive.Valid ? primitive.Size<T> : T extends Like<infer M> ? M['size'] : number;
|
package/dist/internal/struct.js
CHANGED
@@ -19,6 +19,16 @@ export function isStatic(arg) {
|
|
19
19
|
export function isInstance(arg) {
|
20
20
|
return arg != null && typeof arg == 'object' && isStatic(arg.constructor);
|
21
21
|
}
|
22
|
+
export function checkInstance(arg) {
|
23
|
+
if (!isInstance(arg)) {
|
24
|
+
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance');
|
25
|
+
}
|
26
|
+
}
|
22
27
|
export function isStruct(arg) {
|
23
28
|
return isInstance(arg) || isStatic(arg);
|
24
29
|
}
|
30
|
+
export function checkStruct(arg) {
|
31
|
+
if (!isStruct(arg)) {
|
32
|
+
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct');
|
33
|
+
}
|
34
|
+
}
|
package/dist/list.js
CHANGED
@@ -12,10 +12,10 @@ export class List extends EventEmitter {
|
|
12
12
|
return new Set(this.data);
|
13
13
|
}
|
14
14
|
toArray() {
|
15
|
-
return
|
15
|
+
return Array.from(this.data);
|
16
16
|
}
|
17
17
|
toJSON() {
|
18
|
-
return JSON.stringify(
|
18
|
+
return JSON.stringify(Array.from(this.data));
|
19
19
|
}
|
20
20
|
toString() {
|
21
21
|
return this.join(',');
|
@@ -24,7 +24,7 @@ export class List extends EventEmitter {
|
|
24
24
|
if (Math.abs(index) > this.data.size) {
|
25
25
|
throw new ReferenceError('Can not set an element outside the bounds of the list');
|
26
26
|
}
|
27
|
-
const data =
|
27
|
+
const data = Array.from(this.data);
|
28
28
|
data.splice(index, 1, value);
|
29
29
|
this.data = new Set(data);
|
30
30
|
this.emit('update');
|
@@ -33,17 +33,17 @@ export class List extends EventEmitter {
|
|
33
33
|
if (Math.abs(index) > this.data.size) {
|
34
34
|
throw new ReferenceError('Can not delete an element outside the bounds of the list');
|
35
35
|
}
|
36
|
-
this.delete(
|
36
|
+
this.delete(Array.from(this.data).at(index));
|
37
37
|
}
|
38
38
|
// Array methods
|
39
39
|
at(index) {
|
40
40
|
if (Math.abs(index) > this.data.size) {
|
41
41
|
throw new ReferenceError('Can not access an element outside the bounds of the list');
|
42
42
|
}
|
43
|
-
return
|
43
|
+
return Array.from(this.data).at(index);
|
44
44
|
}
|
45
45
|
pop() {
|
46
|
-
const item =
|
46
|
+
const item = Array.from(this.data).pop();
|
47
47
|
if (item !== undefined) {
|
48
48
|
this.delete(item);
|
49
49
|
}
|
@@ -56,13 +56,13 @@ export class List extends EventEmitter {
|
|
56
56
|
return this.data.size;
|
57
57
|
}
|
58
58
|
join(separator) {
|
59
|
-
return
|
59
|
+
return Array.from(this.data).join(separator);
|
60
60
|
}
|
61
61
|
splice(start, deleteCount, ...items) {
|
62
62
|
if (Math.abs(start) > this.data.size) {
|
63
63
|
throw new ReferenceError('Can not splice elements outside the bounds of the list');
|
64
64
|
}
|
65
|
-
const data =
|
65
|
+
const data = Array.from(this.data);
|
66
66
|
const deleted = data.splice(start, deleteCount, ...items);
|
67
67
|
this.data = new Set(data);
|
68
68
|
this.emit('update');
|
package/dist/struct.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import * as primitive from './internal/primitives.js';
|
2
|
-
import {
|
2
|
+
import { checkInstance, checkStruct, init, isStatic, metadata, symbol_metadata, } from './internal/struct.js';
|
3
3
|
import { capitalize } from './string.js';
|
4
4
|
export * as Struct from './internal/struct.js';
|
5
5
|
/**
|
@@ -8,14 +8,10 @@ export * as Struct from './internal/struct.js';
|
|
8
8
|
export function sizeof(type) {
|
9
9
|
// primitive
|
10
10
|
if (typeof type == 'string') {
|
11
|
-
|
12
|
-
throw new TypeError('Invalid primitive type: ' + type);
|
13
|
-
}
|
11
|
+
primitive.checkValid(type);
|
14
12
|
return (+primitive.normalize(type).match(primitive.regex)[2] / 8);
|
15
13
|
}
|
16
|
-
|
17
|
-
throw new TypeError('Not a struct');
|
18
|
-
}
|
14
|
+
checkStruct(type);
|
19
15
|
const struct = isStatic(type) ? type : type.constructor;
|
20
16
|
return struct[symbol_metadata(struct)][metadata].size;
|
21
17
|
}
|
@@ -30,7 +26,7 @@ export function align(value, alignment) {
|
|
30
26
|
*/
|
31
27
|
export function struct(options = {}) {
|
32
28
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
33
|
-
return function
|
29
|
+
return function _decorateStruct(target, context) {
|
34
30
|
context.metadata ??= {};
|
35
31
|
context.metadata[init] ||= [];
|
36
32
|
let size = 0;
|
@@ -75,9 +71,7 @@ export function member(type, length) {
|
|
75
71
|
* Serializes a struct into a Uint8Array
|
76
72
|
*/
|
77
73
|
export function serialize(instance) {
|
78
|
-
|
79
|
-
throw new TypeError('Can not serialize, not a struct instance');
|
80
|
-
}
|
74
|
+
checkInstance(instance);
|
81
75
|
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata];
|
82
76
|
const buffer = new Uint8Array(sizeof(instance));
|
83
77
|
const view = new DataView(buffer.buffer);
|
@@ -112,12 +106,10 @@ export function serialize(instance) {
|
|
112
106
|
* Deserializes a struct from a Uint8Array
|
113
107
|
*/
|
114
108
|
export function deserialize(instance, _buffer) {
|
115
|
-
|
116
|
-
throw new TypeError('Can not deserialize, not a struct instance');
|
117
|
-
}
|
109
|
+
checkInstance(instance);
|
118
110
|
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata];
|
119
|
-
const buffer = new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer);
|
120
|
-
const view = new DataView(buffer.buffer);
|
111
|
+
const buffer = _buffer instanceof Uint8Array ? _buffer : new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer);
|
112
|
+
const view = new DataView(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
|
121
113
|
for (const [name, { type, offset, length }] of members) {
|
122
114
|
for (let i = 0; i < (length || 1); i++) {
|
123
115
|
// @ts-expect-error 7053
|
package/package.json
CHANGED
@@ -15,7 +15,7 @@ export const types = ['int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'in
|
|
15
15
|
|
16
16
|
export const valids = [...types, ...types.map(t => capitalize(t)), 'char'] satisfies Valid[];
|
17
17
|
|
18
|
-
export const regex = /^(u?int)(8|16|32|64)
|
18
|
+
export const regex = /^(u?int|float)(8|16|32|64)$/i;
|
19
19
|
|
20
20
|
export type Normalize<T extends Valid> = T extends 'char' ? 'uint8' : Uncapitalize<T>;
|
21
21
|
|
@@ -30,3 +30,9 @@ export function isType(type: { toString(): string }): type is Type {
|
|
30
30
|
export function isValid(type: { toString(): string }): type is Valid {
|
31
31
|
return type == 'char' || regex.test(type.toString().toLowerCase());
|
32
32
|
}
|
33
|
+
|
34
|
+
export function checkValid(type: { toString(): string }): asserts type is Valid {
|
35
|
+
if (!isValid(type)) {
|
36
|
+
throw new TypeError('Not a valid primitive type: ' + type);
|
37
|
+
}
|
38
|
+
}
|
package/src/internal/struct.ts
CHANGED
@@ -90,10 +90,22 @@ export function isInstance<T extends Metadata = Metadata>(arg: unknown): arg is
|
|
90
90
|
return arg != null && typeof arg == 'object' && isStatic(arg.constructor);
|
91
91
|
}
|
92
92
|
|
93
|
+
export function checkInstance<T extends Metadata = Metadata>(arg: unknown): asserts arg is Instance<T> {
|
94
|
+
if (!isInstance(arg)) {
|
95
|
+
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance');
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
93
99
|
export function isStruct<T extends Metadata = Metadata>(arg: unknown): arg is Instance<T> | Static<T> {
|
94
100
|
return isInstance(arg) || isStatic(arg);
|
95
101
|
}
|
96
102
|
|
103
|
+
export function checkStruct<T extends Metadata = Metadata>(arg: unknown): asserts arg is Instance<T> | Static<T> {
|
104
|
+
if (!isStruct(arg)) {
|
105
|
+
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct');
|
106
|
+
}
|
107
|
+
}
|
108
|
+
|
97
109
|
export type Like<T extends Metadata = Metadata> = InstanceLike<T> | StaticLike<T>;
|
98
110
|
|
99
111
|
export type Size<T extends primitive.Valid | StaticLike | InstanceLike> = T extends primitive.Valid ? primitive.Size<T> : T extends Like<infer M> ? M['size'] : number;
|
package/src/list.ts
CHANGED
@@ -17,11 +17,11 @@ export class List<T> extends EventEmitter<'update'> implements RelativeIndexable
|
|
17
17
|
}
|
18
18
|
|
19
19
|
public toArray(): T[] {
|
20
|
-
return
|
20
|
+
return Array.from(this.data);
|
21
21
|
}
|
22
22
|
|
23
23
|
public toJSON() {
|
24
|
-
return JSON.stringify(
|
24
|
+
return JSON.stringify(Array.from(this.data));
|
25
25
|
}
|
26
26
|
|
27
27
|
public toString() {
|
@@ -33,7 +33,7 @@ export class List<T> extends EventEmitter<'update'> implements RelativeIndexable
|
|
33
33
|
throw new ReferenceError('Can not set an element outside the bounds of the list');
|
34
34
|
}
|
35
35
|
|
36
|
-
const data =
|
36
|
+
const data = Array.from(this.data);
|
37
37
|
data.splice(index, 1, value);
|
38
38
|
this.data = new Set<T>(data);
|
39
39
|
this.emit('update');
|
@@ -44,7 +44,7 @@ export class List<T> extends EventEmitter<'update'> implements RelativeIndexable
|
|
44
44
|
throw new ReferenceError('Can not delete an element outside the bounds of the list');
|
45
45
|
}
|
46
46
|
|
47
|
-
this.delete(
|
47
|
+
this.delete(Array.from(this.data).at(index)!);
|
48
48
|
}
|
49
49
|
|
50
50
|
// Array methods
|
@@ -54,11 +54,11 @@ export class List<T> extends EventEmitter<'update'> implements RelativeIndexable
|
|
54
54
|
throw new ReferenceError('Can not access an element outside the bounds of the list');
|
55
55
|
}
|
56
56
|
|
57
|
-
return
|
57
|
+
return Array.from(this.data).at(index)!;
|
58
58
|
}
|
59
59
|
|
60
60
|
public pop(): T | undefined {
|
61
|
-
const item =
|
61
|
+
const item = Array.from(this.data).pop();
|
62
62
|
if (item !== undefined) {
|
63
63
|
this.delete(item);
|
64
64
|
}
|
@@ -73,7 +73,7 @@ export class List<T> extends EventEmitter<'update'> implements RelativeIndexable
|
|
73
73
|
}
|
74
74
|
|
75
75
|
public join(separator?: string): string {
|
76
|
-
return
|
76
|
+
return Array.from(this.data).join(separator);
|
77
77
|
}
|
78
78
|
|
79
79
|
public splice(start: number, deleteCount: number, ...items: T[]): T[] {
|
@@ -81,7 +81,7 @@ export class List<T> extends EventEmitter<'update'> implements RelativeIndexable
|
|
81
81
|
throw new ReferenceError('Can not splice elements outside the bounds of the list');
|
82
82
|
}
|
83
83
|
|
84
|
-
const data =
|
84
|
+
const data = Array.from(this.data);
|
85
85
|
const deleted = data.splice(start, deleteCount, ...items);
|
86
86
|
this.data = new Set<T>(data);
|
87
87
|
this.emit('update');
|
package/src/struct.ts
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
import * as primitive from './internal/primitives.js';
|
2
2
|
import {
|
3
|
+
checkInstance,
|
4
|
+
checkStruct,
|
3
5
|
DecoratorContext,
|
6
|
+
init,
|
4
7
|
InstanceLike,
|
8
|
+
isStatic,
|
5
9
|
MemberInit,
|
6
10
|
Metadata,
|
11
|
+
metadata,
|
7
12
|
Options,
|
8
13
|
Size,
|
9
14
|
StaticLike,
|
10
15
|
symbol_metadata,
|
11
|
-
init,
|
12
|
-
isInstance,
|
13
|
-
isStatic,
|
14
|
-
isStruct,
|
15
|
-
metadata,
|
16
16
|
type MemberContext,
|
17
17
|
} from './internal/struct.js';
|
18
18
|
import { capitalize } from './string.js';
|
@@ -25,16 +25,12 @@ export * as Struct from './internal/struct.js';
|
|
25
25
|
export function sizeof<T extends primitive.Valid | StaticLike | InstanceLike>(type: T): Size<T> {
|
26
26
|
// primitive
|
27
27
|
if (typeof type == 'string') {
|
28
|
-
|
29
|
-
throw new TypeError('Invalid primitive type: ' + type);
|
30
|
-
}
|
28
|
+
primitive.checkValid(type);
|
31
29
|
|
32
30
|
return (+primitive.normalize(type).match(primitive.regex)![2] / 8) as Size<T>;
|
33
31
|
}
|
34
32
|
|
35
|
-
|
36
|
-
throw new TypeError('Not a struct');
|
37
|
-
}
|
33
|
+
checkStruct(type);
|
38
34
|
|
39
35
|
const struct = isStatic(type) ? type : type.constructor;
|
40
36
|
|
@@ -53,7 +49,7 @@ export function align(value: number, alignment: number): number {
|
|
53
49
|
*/
|
54
50
|
export function struct(options: Partial<Options> = {}) {
|
55
51
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
56
|
-
return function
|
52
|
+
return function _decorateStruct<const T extends StaticLike>(target: T, context: ClassDecoratorContext & DecoratorContext): T {
|
57
53
|
context.metadata ??= {};
|
58
54
|
context.metadata[init] ||= [];
|
59
55
|
let size = 0;
|
@@ -103,9 +99,7 @@ export function member(type: primitive.Valid | ClassLike, length?: number) {
|
|
103
99
|
* Serializes a struct into a Uint8Array
|
104
100
|
*/
|
105
101
|
export function serialize(instance: unknown): Uint8Array {
|
106
|
-
|
107
|
-
throw new TypeError('Can not serialize, not a struct instance');
|
108
|
-
}
|
102
|
+
checkInstance(instance);
|
109
103
|
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata];
|
110
104
|
|
111
105
|
const buffer = new Uint8Array(sizeof(instance));
|
@@ -149,14 +143,12 @@ export function serialize(instance: unknown): Uint8Array {
|
|
149
143
|
* Deserializes a struct from a Uint8Array
|
150
144
|
*/
|
151
145
|
export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView) {
|
152
|
-
|
153
|
-
throw new TypeError('Can not deserialize, not a struct instance');
|
154
|
-
}
|
146
|
+
checkInstance(instance);
|
155
147
|
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata];
|
156
148
|
|
157
|
-
const buffer = new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer);
|
149
|
+
const buffer = _buffer instanceof Uint8Array ? _buffer : new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer);
|
158
150
|
|
159
|
-
const view = new DataView(buffer.buffer);
|
151
|
+
const view = new DataView(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
|
160
152
|
|
161
153
|
for (const [name, { type, offset, length }] of members) {
|
162
154
|
for (let i = 0; i < (length || 1); i++) {
|