utilium 1.0.0 → 1.0.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.
@@ -1,11 +1,13 @@
1
1
  import type { ClassLike } from '../types.js';
2
2
  import type * as primitive from './primitives.js';
3
+ import './struct-symbols.js';
3
4
  export interface MemberInit {
4
5
  name: string;
5
6
  type: string | ClassLike;
6
7
  length?: number;
7
8
  }
8
- export declare const init: unique symbol;
9
+ /** @deprecated */
10
+ export declare const init: typeof Symbol.struct_init;
9
11
  /**
10
12
  * Options for struct initialization
11
13
  */
@@ -23,10 +25,11 @@ export interface Metadata {
23
25
  members: Map<string, Member>;
24
26
  size: number;
25
27
  }
26
- export declare const metadata: unique symbol;
28
+ /** @deprecated */
29
+ export declare const metadata: typeof Symbol.struct_metadata;
27
30
  export interface _DecoratorMetadata<T extends Metadata = Metadata> extends DecoratorMetadata {
28
- [metadata]?: T;
29
- [init]?: MemberInit[];
31
+ [Symbol.struct_metadata]?: T;
32
+ [Symbol.struct_init]?: MemberInit[];
30
33
  }
31
34
  export interface DecoratorContext<T extends Metadata = Metadata> {
32
35
  metadata: _DecoratorMetadata<T>;
@@ -34,7 +37,7 @@ export interface DecoratorContext<T extends Metadata = Metadata> {
34
37
  export type MemberContext = ClassMemberDecoratorContext & DecoratorContext;
35
38
  export interface Static<T extends Metadata = Metadata> {
36
39
  [Symbol.metadata]: DecoratorMetadata & {
37
- [metadata]: T;
40
+ [Symbol.struct_metadata]: T;
38
41
  };
39
42
  new (): Instance<T>;
40
43
  prototype: Instance<T>;
@@ -43,7 +46,7 @@ export interface StaticLike<T extends Metadata = Metadata> extends ClassLike {
43
46
  [Symbol.metadata]?: _DecoratorMetadata<T> | null;
44
47
  }
45
48
  export declare function isValidMetadata<T extends Metadata = Metadata>(arg: unknown): arg is DecoratorMetadata & {
46
- [metadata]: T;
49
+ [Symbol.struct_metadata]: T;
47
50
  };
48
51
  /**
49
52
  * Polyfill context.metadata
@@ -1,7 +1,14 @@
1
- export const init = Symbol('struct_init');
2
- export const metadata = Symbol('struct');
1
+ import './struct-symbols.js';
2
+ // @ts-expect-error 2322
3
+ Symbol.struct_init ||= Symbol('struct_init');
4
+ // @ts-expect-error 2322
5
+ Symbol.struct_metadata ||= Symbol('struct_metadata');
6
+ /** @deprecated */
7
+ export const init = Symbol.struct_init;
8
+ /** @deprecated */
9
+ export const metadata = Symbol.struct_metadata;
3
10
  export function isValidMetadata(arg) {
4
- return arg != null && typeof arg == 'object' && metadata in arg;
11
+ return arg != null && typeof arg == 'object' && Symbol.struct_metadata in arg;
5
12
  }
6
13
  /**
7
14
  * Polyfill Symbol.metadata
package/dist/struct.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as primitive from './internal/primitives.js';
2
- import { checkInstance, checkStruct, init, isStatic, metadata, symbol_metadata } from './internal/struct.js';
2
+ import { checkInstance, checkStruct, isStatic, symbol_metadata } from './internal/struct.js';
3
3
  import { capitalize } from './string.js';
4
4
  export * as Struct from './internal/struct.js';
5
5
  /**
@@ -13,7 +13,7 @@ export function sizeof(type) {
13
13
  }
14
14
  checkStruct(type);
15
15
  const struct = isStatic(type) ? type : type.constructor;
16
- return struct[symbol_metadata(struct)][metadata].size;
16
+ return struct[symbol_metadata(struct)][Symbol.struct_metadata].size;
17
17
  }
18
18
  /**
19
19
  * Aligns a number
@@ -27,10 +27,10 @@ export function align(value, alignment) {
27
27
  export function struct(options = {}) {
28
28
  return function _decorateStruct(target, context) {
29
29
  context.metadata ??= {};
30
- context.metadata[init] ||= [];
30
+ context.metadata[Symbol.struct_init] ||= [];
31
31
  let size = 0;
32
32
  const members = new Map();
33
- for (const _ of context.metadata[init]) {
33
+ for (const _ of context.metadata[Symbol.struct_init]) {
34
34
  const { name, type, length } = _;
35
35
  if (!primitive.isValid(type) && !isStatic(type)) {
36
36
  throw new TypeError('Not a valid type: ' + type);
@@ -43,7 +43,7 @@ export function struct(options = {}) {
43
43
  size += sizeof(type) * (length || 1);
44
44
  size = align(size, options.align || 1);
45
45
  }
46
- context.metadata[metadata] = { options, members, size };
46
+ context.metadata[Symbol.struct_metadata] = { options, members, size };
47
47
  return target;
48
48
  };
49
49
  }
@@ -61,8 +61,8 @@ export function member(type, length) {
61
61
  throw new ReferenceError('Invalid name for struct member');
62
62
  }
63
63
  context.metadata ??= {};
64
- context.metadata[init] ||= [];
65
- context.metadata[init].push({ name, type, length });
64
+ context.metadata[Symbol.struct_init] ||= [];
65
+ context.metadata[Symbol.struct_init].push({ name, type, length });
66
66
  return value;
67
67
  };
68
68
  }
@@ -71,7 +71,7 @@ export function member(type, length) {
71
71
  */
72
72
  export function serialize(instance) {
73
73
  checkInstance(instance);
74
- const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata];
74
+ const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][Symbol.struct_metadata];
75
75
  const buffer = new Uint8Array(sizeof(instance));
76
76
  const view = new DataView(buffer.buffer);
77
77
  for (const [name, { type, length, offset }] of members) {
@@ -106,7 +106,7 @@ export function serialize(instance) {
106
106
  */
107
107
  export function deserialize(instance, _buffer) {
108
108
  checkInstance(instance);
109
- const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata];
109
+ const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][Symbol.struct_metadata];
110
110
  const buffer = _buffer instanceof Uint8Array ? _buffer : new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer);
111
111
  const view = new DataView(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength));
112
112
  for (const [name, { type, offset, length }] of members) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Typescript utilities",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1,4 @@
1
+ interface SymbolConstructor {
2
+ readonly struct_init: unique symbol;
3
+ readonly struct_metadata: unique symbol;
4
+ }
@@ -1,5 +1,12 @@
1
1
  import type { ClassLike } from '../types.js';
2
2
  import type * as primitive from './primitives.js';
3
+ import './struct-symbols.js';
4
+
5
+ // @ts-expect-error 2322
6
+ Symbol.struct_init ||= Symbol('struct_init');
7
+
8
+ // @ts-expect-error 2322
9
+ Symbol.struct_metadata ||= Symbol('struct_metadata');
3
10
 
4
11
  export interface MemberInit {
5
12
  name: string;
@@ -7,7 +14,8 @@ export interface MemberInit {
7
14
  length?: number;
8
15
  }
9
16
 
10
- export const init: unique symbol = Symbol('struct_init');
17
+ /** @deprecated */
18
+ export const init: typeof Symbol.struct_init = Symbol.struct_init;
11
19
 
12
20
  /**
13
21
  * Options for struct initialization
@@ -29,11 +37,12 @@ export interface Metadata {
29
37
  size: number;
30
38
  }
31
39
 
32
- export const metadata: unique symbol = Symbol('struct');
40
+ /** @deprecated */
41
+ export const metadata: typeof Symbol.struct_metadata = Symbol.struct_metadata;
33
42
 
34
43
  export interface _DecoratorMetadata<T extends Metadata = Metadata> extends DecoratorMetadata {
35
- [metadata]?: T;
36
- [init]?: MemberInit[];
44
+ [Symbol.struct_metadata]?: T;
45
+ [Symbol.struct_init]?: MemberInit[];
37
46
  }
38
47
 
39
48
  export interface DecoratorContext<T extends Metadata = Metadata> {
@@ -44,7 +53,7 @@ export type MemberContext = ClassMemberDecoratorContext & DecoratorContext;
44
53
 
45
54
  export interface Static<T extends Metadata = Metadata> {
46
55
  [Symbol.metadata]: DecoratorMetadata & {
47
- [metadata]: T;
56
+ [Symbol.struct_metadata]: T;
48
57
  };
49
58
  new (): Instance<T>;
50
59
  prototype: Instance<T>;
@@ -57,9 +66,9 @@ export interface StaticLike<T extends Metadata = Metadata> extends ClassLike {
57
66
  export function isValidMetadata<T extends Metadata = Metadata>(
58
67
  arg: unknown
59
68
  ): arg is DecoratorMetadata & {
60
- [metadata]: T;
69
+ [Symbol.struct_metadata]: T;
61
70
  } {
62
- return arg != null && typeof arg == 'object' && metadata in arg;
71
+ return arg != null && typeof arg == 'object' && Symbol.struct_metadata in arg;
63
72
  }
64
73
 
65
74
  /**
package/src/struct.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as primitive from './internal/primitives.js';
2
2
  import type { DecoratorContext, InstanceLike, Member, MemberInit, Metadata, Options, Size, StaticLike } from './internal/struct.js';
3
- import { checkInstance, checkStruct, init, isStatic, metadata, symbol_metadata, type MemberContext } from './internal/struct.js';
3
+ import { checkInstance, checkStruct, isStatic, symbol_metadata, type MemberContext } from './internal/struct.js';
4
4
  import { capitalize } from './string.js';
5
5
  import type { ClassLike } from './types.js';
6
6
  export * as Struct from './internal/struct.js';
@@ -20,7 +20,7 @@ export function sizeof<T extends primitive.Valid | StaticLike | InstanceLike>(ty
20
20
 
21
21
  const struct = isStatic(type) ? type : type.constructor;
22
22
 
23
- return struct[symbol_metadata(struct)][metadata].size as Size<T>;
23
+ return struct[symbol_metadata(struct)][Symbol.struct_metadata].size as Size<T>;
24
24
  }
25
25
 
26
26
  /**
@@ -36,10 +36,10 @@ export function align(value: number, alignment: number): number {
36
36
  export function struct(options: Partial<Options> = {}) {
37
37
  return function _decorateStruct<const T extends StaticLike>(target: T, context: ClassDecoratorContext & DecoratorContext): T {
38
38
  context.metadata ??= {};
39
- context.metadata[init] ||= [];
39
+ context.metadata[Symbol.struct_init] ||= [];
40
40
  let size = 0;
41
41
  const members = new Map<string, Member>();
42
- for (const _ of context.metadata[init]) {
42
+ for (const _ of context.metadata[Symbol.struct_init]!) {
43
43
  const { name, type, length } = _;
44
44
  if (!primitive.isValid(type) && !isStatic(type)) {
45
45
  throw new TypeError('Not a valid type: ' + type);
@@ -53,7 +53,7 @@ export function struct(options: Partial<Options> = {}) {
53
53
  size = align(size, options.align || 1);
54
54
  }
55
55
 
56
- context.metadata[metadata] = { options, members, size } satisfies Metadata;
56
+ context.metadata[Symbol.struct_metadata] = { options, members, size } satisfies Metadata;
57
57
  return target;
58
58
  };
59
59
  }
@@ -74,8 +74,8 @@ export function member(type: primitive.Valid | ClassLike, length?: number) {
74
74
  }
75
75
 
76
76
  context.metadata ??= {};
77
- context.metadata[init] ||= [];
78
- context.metadata[init].push({ name, type, length } satisfies MemberInit);
77
+ context.metadata[Symbol.struct_init] ||= [];
78
+ context.metadata[Symbol.struct_init]!.push({ name, type, length } satisfies MemberInit);
79
79
  return value;
80
80
  };
81
81
  }
@@ -85,7 +85,7 @@ export function member(type: primitive.Valid | ClassLike, length?: number) {
85
85
  */
86
86
  export function serialize(instance: unknown): Uint8Array {
87
87
  checkInstance(instance);
88
- const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata];
88
+ const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][Symbol.struct_metadata];
89
89
 
90
90
  const buffer = new Uint8Array(sizeof(instance));
91
91
  const view = new DataView(buffer.buffer);
@@ -129,7 +129,7 @@ export function serialize(instance: unknown): Uint8Array {
129
129
  */
130
130
  export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView) {
131
131
  checkInstance(instance);
132
- const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata];
132
+ const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][Symbol.struct_metadata];
133
133
 
134
134
  const buffer = _buffer instanceof Uint8Array ? _buffer : new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer);
135
135