utilium 1.7.5 → 1.7.7

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.js CHANGED
@@ -44,7 +44,7 @@ export function align(value, alignment) {
44
44
  */
45
45
  function _memberLength(struct, length, name) {
46
46
  if (length === undefined)
47
- return 0;
47
+ return -1;
48
48
  if (typeof length != 'string')
49
49
  return Number.isSafeInteger(length) && length >= 0
50
50
  ? length
@@ -119,6 +119,8 @@ export function member(type, length) {
119
119
  * Serializes a struct into a Uint8Array
120
120
  */
121
121
  export function serialize(instance) {
122
+ if (isCustom(instance))
123
+ return instance[Symbol.serialize]();
122
124
  checkInstance(instance);
123
125
  const { options, members } = instance.constructor[symbol_metadata(instance.constructor)].struct;
124
126
  const buffer = new Uint8Array(sizeof(instance));
@@ -126,9 +128,9 @@ export function serialize(instance) {
126
128
  // for unions we should write members in ascending last modified order, but we don't have that info.
127
129
  for (const [name, { type, length: rawLength, offset }] of members) {
128
130
  const length = _memberLength(instance, rawLength, name);
129
- for (let i = 0; i < (length || 1); i++) {
131
+ for (let i = 0; i < Math.abs(length); i++) {
130
132
  const iOff = offset + sizeof(type) * i;
131
- let value = length > 0 ? instance[name][i] : instance[name];
133
+ let value = length != -1 ? instance[name][i] : instance[name];
132
134
  if (typeof value == 'string') {
133
135
  value = value.charCodeAt(0);
134
136
  }
@@ -177,9 +179,9 @@ export function deserialize(instance, _buffer) {
177
179
  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
178
180
  for (const [name, { type, offset, length: rawLength }] of members) {
179
181
  const length = _memberLength(instance, rawLength, name);
180
- for (let i = 0; i < (length || 1); i++) {
181
- let object = length > 0 ? instance[name] : instance;
182
- const key = length > 0 ? i : name, iOff = offset + sizeof(type) * i;
182
+ for (let i = 0; i < Math.abs(length); i++) {
183
+ let object = length != -1 ? instance[name][i] : instance[name];
184
+ const key = length != -1 ? i : name, iOff = offset + sizeof(type) * i;
183
185
  if (typeof instance[name] == 'string') {
184
186
  instance[name] =
185
187
  instance[name].slice(0, i) + String.fromCharCode(view.getUint8(iOff)) + instance[name].slice(i + 1);
@@ -192,7 +194,7 @@ export function deserialize(instance, _buffer) {
192
194
  deserialize(object[key], new Uint8Array(buffer.subarray(iOff, iOff + sizeof(type))));
193
195
  continue;
194
196
  }
195
- if (length)
197
+ if (length && length != -1)
196
198
  object ||= [];
197
199
  const fn = `get${capitalize(type)}`;
198
200
  if (fn == 'getInt64') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.7.5",
3
+ "version": "1.7.7",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
package/src/struct.ts CHANGED
@@ -71,7 +71,7 @@ function _memberLength<T extends Metadata>(
71
71
  length: string | number | undefined,
72
72
  name: string
73
73
  ): number {
74
- if (length === undefined) return 0;
74
+ if (length === undefined) return -1;
75
75
  if (typeof length != 'string')
76
76
  return Number.isSafeInteger(length) && length >= 0
77
77
  ? length
@@ -162,6 +162,8 @@ export function member(type: primitive.Valid | ClassLike, length?: number | stri
162
162
  * Serializes a struct into a Uint8Array
163
163
  */
164
164
  export function serialize(instance: unknown): Uint8Array {
165
+ if (isCustom(instance)) return instance[Symbol.serialize]!();
166
+
165
167
  checkInstance(instance);
166
168
  const { options, members } = instance.constructor[symbol_metadata(instance.constructor)].struct;
167
169
 
@@ -171,10 +173,10 @@ export function serialize(instance: unknown): Uint8Array {
171
173
  // for unions we should write members in ascending last modified order, but we don't have that info.
172
174
  for (const [name, { type, length: rawLength, offset }] of members) {
173
175
  const length = _memberLength(instance, rawLength, name);
174
- for (let i = 0; i < (length || 1); i++) {
176
+ for (let i = 0; i < Math.abs(length); i++) {
175
177
  const iOff = offset + sizeof(type) * i;
176
178
 
177
- let value = length > 0 ? instance[name][i] : instance[name];
179
+ let value = length != -1 ? instance[name][i] : instance[name];
178
180
  if (typeof value == 'string') {
179
181
  value = value.charCodeAt(0);
180
182
  }
@@ -236,9 +238,9 @@ export function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayB
236
238
 
237
239
  for (const [name, { type, offset, length: rawLength }] of members) {
238
240
  const length = _memberLength(instance, rawLength, name);
239
- for (let i = 0; i < (length || 1); i++) {
240
- let object = length > 0 ? instance[name] : instance;
241
- const key = length > 0 ? i : name,
241
+ for (let i = 0; i < Math.abs(length); i++) {
242
+ let object = length != -1 ? instance[name][i] : instance[name];
243
+ const key = length != -1 ? i : name,
242
244
  iOff = offset + sizeof(type) * i;
243
245
 
244
246
  if (typeof instance[name] == 'string') {
@@ -255,7 +257,7 @@ export function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayB
255
257
  continue;
256
258
  }
257
259
 
258
- if (length) object ||= [];
260
+ if (length && length != -1) object ||= [];
259
261
 
260
262
  const fn = `get${capitalize(type)}` as const;
261
263
  if (fn == 'getInt64') {