utilium 1.7.2 → 1.7.4

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
@@ -21,7 +21,7 @@ export declare function struct(options?: Partial<Options>): <const T extends Sta
21
21
  /**
22
22
  * Decorates a class member to be serialized
23
23
  */
24
- export declare function member<I extends {}>(type: primitive.Valid | ClassLike<I>, length?: number | (keyof I & string)): <V>(value: V, context: MemberContext) => V;
24
+ export declare function member(type: primitive.Valid | ClassLike, length?: number | string): <V>(value: V, context: MemberContext) => V;
25
25
  /**
26
26
  * Serializes a struct into a Uint8Array
27
27
  */
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 1;
47
+ return 0;
48
48
  if (typeof length != 'string')
49
49
  return Number.isSafeInteger(length) && length >= 0
50
50
  ? length
@@ -97,7 +97,6 @@ export function struct(options = {}) {
97
97
  /**
98
98
  * Decorates a class member to be serialized
99
99
  */
100
- // eslint-disable-next-line @typescript-eslint/no-empty-object-type
101
100
  export function member(type, length) {
102
101
  return function (value, context) {
103
102
  let name = context.name;
@@ -125,7 +124,7 @@ export function serialize(instance) {
125
124
  // for unions we should write members in ascending last modified order, but we don't have that info.
126
125
  for (const [name, { type, length: rawLength, offset }] of members) {
127
126
  const length = _memberLength(instance.constructor, rawLength, name);
128
- for (let i = 0; i < length; i++) {
127
+ for (let i = 0; i < (length || 1); i++) {
129
128
  const iOff = offset + sizeof(type) * i;
130
129
  let value = length > 0 ? instance[name][i] : instance[name];
131
130
  if (typeof value == 'string') {
@@ -176,7 +175,7 @@ export function deserialize(instance, _buffer) {
176
175
  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
177
176
  for (const [name, { type, offset, length: rawLength }] of members) {
178
177
  const length = _memberLength(instance.constructor, rawLength, name);
179
- for (let i = 0; i < length; i++) {
178
+ for (let i = 0; i < (length || 1); i++) {
180
179
  let object = length > 0 ? instance[name] : instance;
181
180
  const key = length > 0 ? i : name, iOff = offset + sizeof(type) * i;
182
181
  if (typeof instance[name] == 'string') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.7.2",
3
+ "version": "1.7.4",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
package/src/struct.ts CHANGED
@@ -70,7 +70,7 @@ function _memberLength<T extends Metadata>(
70
70
  length: string | number | undefined,
71
71
  name: string
72
72
  ): number {
73
- if (length === undefined) return 1;
73
+ if (length === undefined) return 0;
74
74
  if (typeof length != 'string')
75
75
  return Number.isSafeInteger(length) && length >= 0
76
76
  ? length
@@ -138,8 +138,7 @@ export function struct(options: Partial<Options> = {}) {
138
138
  /**
139
139
  * Decorates a class member to be serialized
140
140
  */
141
- // eslint-disable-next-line @typescript-eslint/no-empty-object-type
142
- export function member<I extends {}>(type: primitive.Valid | ClassLike<I>, length?: number | (keyof I & string)) {
141
+ export function member(type: primitive.Valid | ClassLike, length?: number | string) {
143
142
  return function <V>(value: V, context: MemberContext): V {
144
143
  let name = context.name;
145
144
  if (typeof name == 'symbol') {
@@ -170,7 +169,7 @@ export function serialize(instance: unknown): Uint8Array {
170
169
  // for unions we should write members in ascending last modified order, but we don't have that info.
171
170
  for (const [name, { type, length: rawLength, offset }] of members) {
172
171
  const length = _memberLength(instance.constructor, rawLength, name);
173
- for (let i = 0; i < length; i++) {
172
+ for (let i = 0; i < (length || 1); i++) {
174
173
  const iOff = offset + sizeof(type) * i;
175
174
 
176
175
  let value = length > 0 ? instance[name][i] : instance[name];
@@ -235,7 +234,7 @@ export function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayB
235
234
 
236
235
  for (const [name, { type, offset, length: rawLength }] of members) {
237
236
  const length = _memberLength(instance.constructor, rawLength, name);
238
- for (let i = 0; i < length; i++) {
237
+ for (let i = 0; i < (length || 1); i++) {
239
238
  let object = length > 0 ? instance[name] : instance;
240
239
  const key = length > 0 ? i : name,
241
240
  iOff = offset + sizeof(type) * i;