utilium 0.6.3 → 0.7.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.
@@ -18,4 +18,7 @@ export declare function isType(type: {
18
18
  export declare function isValid(type: {
19
19
  toString(): string;
20
20
  }): type is Valid;
21
+ export declare function checkValid(type: {
22
+ toString(): string;
23
+ }): asserts type is Valid;
21
24
  export {};
@@ -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;
@@ -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.d.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import { EventEmitter } from 'eventemitter3';
2
- export declare class List<T> extends EventEmitter<'update'> implements Set<T>, RelativeIndexable<T> {
2
+ export declare class List<T> extends EventEmitter<'update'> implements RelativeIndexable<T> {
3
3
  readonly [Symbol.toStringTag] = "List";
4
4
  constructor(values?: readonly T[] | Iterable<T> | null);
5
5
  protected data: Set<T>;
6
- array(): T[];
7
- json(): string;
6
+ toSet(): Set<T>;
7
+ toArray(): T[];
8
+ toJSON(): string;
8
9
  toString(): string;
9
10
  set(index: number, value: T): void;
10
11
  deleteAt(index: number): void;
@@ -16,14 +17,6 @@ export declare class List<T> extends EventEmitter<'update'> implements Set<T>, R
16
17
  add(value: T): this;
17
18
  clear(): void;
18
19
  delete(value: T): boolean;
19
- union<U>(other: ReadonlySetLike<U>): List<T | U>;
20
- intersection<U>(other: ReadonlySetLike<U>): List<T & U>;
21
- difference<U>(other: ReadonlySetLike<U>): List<T>;
22
- symmetricDifference<U>(other: ReadonlySetLike<U>): List<T | U>;
23
- isSubsetOf(other: ReadonlySetLike<unknown>): boolean;
24
- isSupersetOf(other: ReadonlySetLike<unknown>): boolean;
25
- isDisjointFrom(other: ReadonlySetLike<unknown>): boolean;
26
- forEach(callbackfn: (value: T, value2: T, list: List<T>) => void, thisArg?: any): void;
27
20
  has(value: T): boolean;
28
21
  get size(): number;
29
22
  entries(): IterableIterator<[T, T]>;
package/dist/list.js CHANGED
@@ -8,10 +8,13 @@ export class List extends EventEmitter {
8
8
  }
9
9
  }
10
10
  data = new Set();
11
- array() {
11
+ toSet() {
12
+ return new Set(this.data);
13
+ }
14
+ toArray() {
12
15
  return [...this.data];
13
16
  }
14
- json() {
17
+ toJSON() {
15
18
  return JSON.stringify([...this.data]);
16
19
  }
17
20
  toString() {
@@ -80,31 +83,6 @@ export class List extends EventEmitter {
80
83
  this.emit('update');
81
84
  return success;
82
85
  }
83
- union(other) {
84
- return new List(this.data.union(other));
85
- }
86
- intersection(other) {
87
- return new List(this.data.intersection(other));
88
- }
89
- difference(other) {
90
- return new List(this.data.difference(other));
91
- }
92
- symmetricDifference(other) {
93
- return new List(this.data.symmetricDifference(other));
94
- }
95
- isSubsetOf(other) {
96
- return this.data.isSubsetOf(other);
97
- }
98
- isSupersetOf(other) {
99
- return this.data.isSupersetOf(other);
100
- }
101
- isDisjointFrom(other) {
102
- return this.data.isDisjointFrom(other);
103
- }
104
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
105
- forEach(callbackfn, thisArg) {
106
- this.data.forEach((v1, v2) => callbackfn.call(thisArg, v1, v2, this));
107
- }
108
86
  has(value) {
109
87
  return this.data.has(value);
110
88
  }
package/dist/struct.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as primitive from './internal/primitives.js';
2
- import { symbol_metadata, init, isInstance, isStatic, isStruct, metadata, } from './internal/struct.js';
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
- if (!primitive.isValid(type)) {
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
- if (!isStruct(type)) {
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 __decorateStruct(target, context) {
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
- if (!isInstance(instance)) {
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
- if (!isInstance(instance)) {
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "0.6.3",
3
+ "version": "0.7.1",
4
4
  "description": "Typescript utilies",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -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
+ }
@@ -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
@@ -1,6 +1,6 @@
1
1
  import { EventEmitter } from 'eventemitter3';
2
2
 
3
- export class List<T> extends EventEmitter<'update'> implements Set<T>, RelativeIndexable<T> {
3
+ export class List<T> extends EventEmitter<'update'> implements RelativeIndexable<T> {
4
4
  public readonly [Symbol.toStringTag] = 'List';
5
5
 
6
6
  public constructor(values?: readonly T[] | Iterable<T> | null) {
@@ -12,11 +12,15 @@ export class List<T> extends EventEmitter<'update'> implements Set<T>, RelativeI
12
12
 
13
13
  protected data = new Set<T>();
14
14
 
15
- public array(): T[] {
15
+ public toSet(): Set<T> {
16
+ return new Set(this.data);
17
+ }
18
+
19
+ public toArray(): T[] {
16
20
  return [...this.data];
17
21
  }
18
22
 
19
- public json() {
23
+ public toJSON() {
20
24
  return JSON.stringify([...this.data]);
21
25
  }
22
26
 
@@ -103,39 +107,6 @@ export class List<T> extends EventEmitter<'update'> implements Set<T>, RelativeI
103
107
  return success;
104
108
  }
105
109
 
106
- public union<U>(other: ReadonlySetLike<U>): List<T | U> {
107
- return new List(this.data.union(other));
108
- }
109
-
110
- public intersection<U>(other: ReadonlySetLike<U>): List<T & U> {
111
- return new List(this.data.intersection(other));
112
- }
113
-
114
- public difference<U>(other: ReadonlySetLike<U>): List<T> {
115
- return new List(this.data.difference(other));
116
- }
117
-
118
- public symmetricDifference<U>(other: ReadonlySetLike<U>): List<T | U> {
119
- return new List(this.data.symmetricDifference(other));
120
- }
121
-
122
- public isSubsetOf(other: ReadonlySetLike<unknown>): boolean {
123
- return this.data.isSubsetOf(other);
124
- }
125
-
126
- public isSupersetOf(other: ReadonlySetLike<unknown>): boolean {
127
- return this.data.isSupersetOf(other);
128
- }
129
-
130
- public isDisjointFrom(other: ReadonlySetLike<unknown>): boolean {
131
- return this.data.isDisjointFrom(other);
132
- }
133
-
134
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
135
- public forEach(callbackfn: (value: T, value2: T, list: List<T>) => void, thisArg?: any): void {
136
- this.data.forEach((v1, v2) => callbackfn.call(thisArg, v1, v2, this));
137
- }
138
-
139
110
  public has(value: T): boolean {
140
111
  return this.data.has(value);
141
112
  }
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
- if (!primitive.isValid(type)) {
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
- if (!isStruct(type)) {
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 __decorateStruct<const T extends StaticLike>(target: T, context: ClassDecoratorContext & DecoratorContext): T {
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
- if (!isInstance(instance)) {
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
- if (!isInstance(instance)) {
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++) {