utilium 0.7.0 → 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/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.7.0",
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/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++) {