utilium 1.7.17 → 1.9.0

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.
@@ -2,11 +2,8 @@ import type { ClassLike } from '../types.js';
2
2
  import type * as primitive from './primitives.js';
3
3
  declare global {
4
4
  interface SymbolConstructor {
5
- /** User-defined size */
6
5
  readonly size: unique symbol;
7
- /** User-defined serialization */
8
6
  readonly serialize: unique symbol;
9
- /** User-defined deserialization */
10
7
  readonly deserialize: unique symbol;
11
8
  }
12
9
  }
@@ -68,6 +65,7 @@ export interface Static<T extends Metadata = Metadata> {
68
65
  }
69
66
  export interface StaticLike<T extends Metadata = Metadata> extends ClassLike {
70
67
  [Symbol.metadata]?: _DecoratorMetadata<T> | null;
68
+ new (): unknown;
71
69
  }
72
70
  export declare function isValidMetadata<T extends Metadata = Metadata>(arg: unknown): arg is DecoratorMetadata & {
73
71
  struct: T;
package/dist/string.d.ts CHANGED
@@ -15,3 +15,5 @@ export declare function encodeUTF8(input: string): Uint8Array;
15
15
  * Decodes a UTF-8 string from a buffer
16
16
  */
17
17
  export declare function decodeUTF8(input?: Uint8Array): string;
18
+ export declare function encodeASCII(input: string): Uint8Array;
19
+ export declare function decodeASCII(input: Uint8Array): string;
package/dist/string.js CHANGED
@@ -18,3 +18,17 @@ const decoder = new TextDecoder();
18
18
  export function decodeUTF8(input) {
19
19
  return decoder.decode(input);
20
20
  }
21
+ export function encodeASCII(input) {
22
+ const data = new Uint8Array(input.length);
23
+ for (let i = 0; i < input.length; i++) {
24
+ data[i] = input.charCodeAt(i);
25
+ }
26
+ return data;
27
+ }
28
+ export function decodeASCII(input) {
29
+ let output = '';
30
+ for (let i = 0; i < input.length; i++) {
31
+ output += String.fromCharCode(input[i]);
32
+ }
33
+ return output;
34
+ }
package/dist/struct.js CHANGED
@@ -229,8 +229,10 @@ export function deserialize(instance, _buffer) {
229
229
  for (let i = 0; i < Math.abs(length); i++) {
230
230
  let object = length != -1 ? instance[member.name] : instance;
231
231
  const key = length != -1 ? i : member.name;
232
+ const isNullish = object[key] === null || object[key] === undefined;
233
+ const needsAllocation = isNullish && isStatic(member.type) && member.type[Symbol.metadata].struct.isDynamic;
232
234
  offset = nextOffset;
233
- if (!isInstance(object[key]))
235
+ if (!isInstance(object[key]) && !needsAllocation)
234
236
  nextOffset += sizeof(member.type);
235
237
  if (typeof instance[member.name] == 'string') {
236
238
  instance[member.name] =
@@ -240,7 +242,9 @@ export function deserialize(instance, _buffer) {
240
242
  continue;
241
243
  }
242
244
  if (!primitive.isType(member.type)) {
243
- if (object[key] === null || object[key] === undefined)
245
+ if (needsAllocation && isStatic(member.type))
246
+ object[key] ??= new member.type();
247
+ else if (isNullish)
244
248
  continue;
245
249
  deserialize(object[key], new Uint8Array(buffer.subarray(offset)));
246
250
  nextOffset += sizeof(object[key]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.7.17",
3
+ "version": "1.9.0",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
@@ -3,13 +3,8 @@ import type * as primitive from './primitives.js';
3
3
 
4
4
  declare global {
5
5
  interface SymbolConstructor {
6
- /** User-defined size */
7
6
  readonly size: unique symbol;
8
-
9
- /** User-defined serialization */
10
7
  readonly serialize: unique symbol;
11
-
12
- /** User-defined deserialization */
13
8
  readonly deserialize: unique symbol;
14
9
  }
15
10
  }
@@ -102,6 +97,7 @@ export interface Static<T extends Metadata = Metadata> {
102
97
 
103
98
  export interface StaticLike<T extends Metadata = Metadata> extends ClassLike {
104
99
  [Symbol.metadata]?: _DecoratorMetadata<T> | null;
100
+ new (): unknown;
105
101
  }
106
102
 
107
103
  export function isValidMetadata<T extends Metadata = Metadata>(
package/src/string.ts CHANGED
@@ -38,3 +38,19 @@ const decoder = new TextDecoder();
38
38
  export function decodeUTF8(input?: Uint8Array): string {
39
39
  return decoder.decode(input);
40
40
  }
41
+
42
+ export function encodeASCII(input: string): Uint8Array {
43
+ const data = new Uint8Array(input.length);
44
+ for (let i = 0; i < input.length; i++) {
45
+ data[i] = input.charCodeAt(i);
46
+ }
47
+ return data;
48
+ }
49
+
50
+ export function decodeASCII(input: Uint8Array): string {
51
+ let output = '';
52
+ for (let i = 0; i < input.length; i++) {
53
+ output += String.fromCharCode(input[i]);
54
+ }
55
+ return output;
56
+ }
package/src/struct.ts CHANGED
@@ -318,8 +318,12 @@ export function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayB
318
318
  let object = length != -1 ? instance[member.name] : instance;
319
319
  const key = length != -1 ? i : member.name;
320
320
 
321
+ const isNullish = object[key] === null || object[key] === undefined;
322
+
323
+ const needsAllocation = isNullish && isStatic(member.type) && member.type[Symbol.metadata].struct.isDynamic;
324
+
321
325
  offset = nextOffset;
322
- if (!isInstance(object[key])) nextOffset += sizeof(member.type);
326
+ if (!isInstance(object[key]) && !needsAllocation) nextOffset += sizeof(member.type);
323
327
 
324
328
  if (typeof instance[member.name] == 'string') {
325
329
  instance[member.name] =
@@ -330,7 +334,8 @@ export function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayB
330
334
  }
331
335
 
332
336
  if (!primitive.isType(member.type)) {
333
- if (object[key] === null || object[key] === undefined) continue;
337
+ if (needsAllocation && isStatic(member.type)) object[key] ??= new member.type();
338
+ else if (isNullish) continue;
334
339
 
335
340
  deserialize(object[key], new Uint8Array(buffer.subarray(offset)));
336
341
  nextOffset += sizeof(object[key]);