utilium 0.3.0 → 0.3.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.
package/dist/struct.d.ts CHANGED
@@ -1,16 +1,42 @@
1
1
  import { ClassLike } from './types.js';
2
2
  export type PrimitiveType = `${'int' | 'uint'}${8 | 16 | 32 | 64}` | `float${32 | 64}`;
3
3
  export type ValidPrimitiveType = PrimitiveType | Capitalize<PrimitiveType> | 'char';
4
+ /**
5
+ * Options for struct initialization
6
+ */
4
7
  export interface StructOptions {
5
8
  align: number;
6
9
  bigEndian: boolean;
7
10
  }
11
+ /**
12
+ * Gets the size in bytes of a type
13
+ */
8
14
  export declare function sizeof(type: ValidPrimitiveType | ClassLike | object): number;
15
+ /**
16
+ * Aligns a number
17
+ */
9
18
  export declare function align(value: number, alignment: number): number;
19
+ /**
20
+ * Decorates a class as a struct
21
+ */
10
22
  export declare function struct(options?: Partial<StructOptions>): (target: ClassLike, _?: ClassDecoratorContext) => void;
23
+ /**
24
+ * Decorates a class member to be serialized
25
+ */
11
26
  export declare function member(type: ValidPrimitiveType | ClassLike, length?: number): (target: object, context?: ClassMemberDecoratorContext | string | symbol) => void;
27
+ /**
28
+ * Serializes a struct into a Uint8Array
29
+ */
12
30
  export declare function serialize(instance: unknown): Uint8Array;
13
- export declare function deserialize(instance: unknown, _buffer: Uint8Array): void;
31
+ /**
32
+ * Deserializes a struct from a Uint8Array
33
+ */
34
+ export declare function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView): void;
35
+ /**
36
+ * Shortcut types
37
+ *
38
+ * Instead of writing `@member(type)` you can write `@types.type`, or `@types.type(length)` for arrays
39
+ */
14
40
  export declare const types: {
15
41
  int8: {
16
42
  (length?: number): (target: object, context?: string | symbol | ClassMemberDecoratorContext) => void;
package/dist/struct.js CHANGED
@@ -22,6 +22,9 @@ function isInstance(arg) {
22
22
  function isStruct(arg) {
23
23
  return isInstance(arg) || isStatic(arg);
24
24
  }
25
+ /**
26
+ * Gets the size in bytes of a type
27
+ */
25
28
  export function sizeof(type) {
26
29
  // primitive
27
30
  if (typeof type == 'string') {
@@ -36,9 +39,15 @@ export function sizeof(type) {
36
39
  const meta = metadata in type ? type[metadata] : type.constructor[metadata];
37
40
  return meta.size;
38
41
  }
42
+ /**
43
+ * Aligns a number
44
+ */
39
45
  export function align(value, alignment) {
40
46
  return Math.ceil(value / alignment) * alignment;
41
47
  }
48
+ /**
49
+ * Decorates a class as a struct
50
+ */
42
51
  export function struct(options = {}) {
43
52
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
44
53
  return function (target, _) {
@@ -61,6 +70,9 @@ export function struct(options = {}) {
61
70
  delete target[init];
62
71
  };
63
72
  }
73
+ /**
74
+ * Decorates a class member to be serialized
75
+ */
64
76
  export function member(type, length) {
65
77
  return function (target, context) {
66
78
  var _a;
@@ -73,6 +85,9 @@ export function member(type, length) {
73
85
  target.constructor[init].push({ name, type, length });
74
86
  };
75
87
  }
88
+ /**
89
+ * Serializes a struct into a Uint8Array
90
+ */
76
91
  export function serialize(instance) {
77
92
  if (!isInstance(instance)) {
78
93
  throw new TypeError('Can not serialize, not a struct instance');
@@ -106,6 +121,9 @@ export function serialize(instance) {
106
121
  }
107
122
  return buffer;
108
123
  }
124
+ /**
125
+ * Deserializes a struct from a Uint8Array
126
+ */
109
127
  export function deserialize(instance, _buffer) {
110
128
  if (!isInstance(instance)) {
111
129
  throw new TypeError('Can not deserialize, not a struct instance');
@@ -154,4 +172,9 @@ function _member(type) {
154
172
  }
155
173
  return _;
156
174
  }
175
+ /**
176
+ * Shortcut types
177
+ *
178
+ * Instead of writing `@member(type)` you can write `@types.type`, or `@types.type(length)` for arrays
179
+ */
157
180
  export const types = Object.fromEntries(validPrimitiveTypes.map(t => [t, _member(t)]));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Typescript utilies",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/struct.ts CHANGED
@@ -30,6 +30,9 @@ interface MemberInit {
30
30
 
31
31
  const init = Symbol('struct_init');
32
32
 
33
+ /**
34
+ * Options for struct initialization
35
+ */
33
36
  export interface StructOptions {
34
37
  align: number;
35
38
  bigEndian: boolean;
@@ -71,6 +74,9 @@ function isStruct(arg: unknown): arg is Instance | Static {
71
74
  return isInstance(arg) || isStatic(arg);
72
75
  }
73
76
 
77
+ /**
78
+ * Gets the size in bytes of a type
79
+ */
74
80
  export function sizeof(type: ValidPrimitiveType | ClassLike | object): number {
75
81
  // primitive
76
82
  if (typeof type == 'string') {
@@ -89,10 +95,16 @@ export function sizeof(type: ValidPrimitiveType | ClassLike | object): number {
89
95
  return meta.size;
90
96
  }
91
97
 
98
+ /**
99
+ * Aligns a number
100
+ */
92
101
  export function align(value: number, alignment: number): number {
93
102
  return Math.ceil(value / alignment) * alignment;
94
103
  }
95
104
 
105
+ /**
106
+ * Decorates a class as a struct
107
+ */
96
108
  export function struct(options: Partial<StructOptions> = {}) {
97
109
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
98
110
  return function (target: ClassLike, _?: ClassDecoratorContext) {
@@ -117,6 +129,9 @@ export function struct(options: Partial<StructOptions> = {}) {
117
129
  };
118
130
  }
119
131
 
132
+ /**
133
+ * Decorates a class member to be serialized
134
+ */
120
135
  export function member(type: ValidPrimitiveType | ClassLike, length?: number) {
121
136
  return function (target: object, context?: ClassMemberDecoratorContext | string | symbol) {
122
137
  let name = typeof context == 'object' ? context.name : context;
@@ -130,6 +145,9 @@ export function member(type: ValidPrimitiveType | ClassLike, length?: number) {
130
145
  };
131
146
  }
132
147
 
148
+ /**
149
+ * Serializes a struct into a Uint8Array
150
+ */
133
151
  export function serialize(instance: unknown): Uint8Array {
134
152
  if (!isInstance(instance)) {
135
153
  throw new TypeError('Can not serialize, not a struct instance');
@@ -172,7 +190,10 @@ export function serialize(instance: unknown): Uint8Array {
172
190
  return buffer;
173
191
  }
174
192
 
175
- export function deserialize(instance: unknown, _buffer: Uint8Array) {
193
+ /**
194
+ * Deserializes a struct from a Uint8Array
195
+ */
196
+ export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView) {
176
197
  if (!isInstance(instance)) {
177
198
  throw new TypeError('Can not deserialize, not a struct instance');
178
199
  }
@@ -235,4 +256,9 @@ function _member<T extends ValidPrimitiveType>(type: T) {
235
256
  return _;
236
257
  }
237
258
 
259
+ /**
260
+ * Shortcut types
261
+ *
262
+ * Instead of writing `@member(type)` you can write `@types.type`, or `@types.type(length)` for arrays
263
+ */
238
264
  export const types = Object.fromEntries(validPrimitiveTypes.map(t => [t, _member(t)])) as { [K in ValidPrimitiveType]: ReturnType<typeof _member<K>> };