@yume-chan/struct 0.0.17 → 0.0.18

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.
Files changed (61) hide show
  1. package/CHANGELOG.json +12 -0
  2. package/CHANGELOG.md +8 -1
  3. package/LICENSE +1 -1
  4. package/esm/basic/definition.d.ts +4 -4
  5. package/esm/basic/definition.d.ts.map +1 -1
  6. package/esm/basic/definition.js.map +1 -1
  7. package/esm/basic/field-value.d.ts +7 -7
  8. package/esm/basic/field-value.d.ts.map +1 -1
  9. package/esm/basic/field-value.js +2 -2
  10. package/esm/basic/field-value.js.map +1 -1
  11. package/esm/basic/stream.d.ts +1 -1
  12. package/esm/basic/stream.d.ts.map +1 -1
  13. package/esm/basic/struct-value.d.ts +2 -2
  14. package/esm/basic/struct-value.d.ts.map +1 -1
  15. package/esm/basic/struct-value.js +10 -3
  16. package/esm/basic/struct-value.js.map +1 -1
  17. package/esm/struct.d.ts +19 -19
  18. package/esm/struct.d.ts.map +1 -1
  19. package/esm/struct.js +10 -8
  20. package/esm/struct.js.map +1 -1
  21. package/esm/sync-promise.d.ts.map +1 -1
  22. package/esm/sync-promise.js +4 -4
  23. package/esm/sync-promise.js.map +1 -1
  24. package/esm/types/bigint.d.ts +4 -4
  25. package/esm/types/bigint.d.ts.map +1 -1
  26. package/esm/types/bigint.js +6 -6
  27. package/esm/types/bigint.js.map +1 -1
  28. package/esm/types/buffer/base.d.ts +4 -4
  29. package/esm/types/buffer/base.d.ts.map +1 -1
  30. package/esm/types/buffer/base.js +5 -6
  31. package/esm/types/buffer/base.js.map +1 -1
  32. package/esm/types/buffer/fixed-length.d.ts.map +1 -1
  33. package/esm/types/buffer/fixed-length.js +0 -1
  34. package/esm/types/buffer/fixed-length.js.map +1 -1
  35. package/esm/types/buffer/variable-length.d.ts +7 -7
  36. package/esm/types/buffer/variable-length.d.ts.map +1 -1
  37. package/esm/types/buffer/variable-length.js +4 -4
  38. package/esm/types/buffer/variable-length.js.map +1 -1
  39. package/esm/types/number.d.ts +16 -22
  40. package/esm/types/number.d.ts.map +1 -1
  41. package/esm/types/number.js +89 -40
  42. package/esm/types/number.js.map +1 -1
  43. package/esm/utils.d.ts +6 -6
  44. package/esm/utils.d.ts.map +1 -1
  45. package/esm/utils.js +1 -8
  46. package/esm/utils.js.map +1 -1
  47. package/package.json +12 -9
  48. package/src/basic/definition.ts +12 -9
  49. package/src/basic/field-value.ts +18 -15
  50. package/src/basic/stream.ts +1 -1
  51. package/src/basic/struct-value.ts +17 -11
  52. package/src/struct.ts +207 -201
  53. package/src/sync-promise.ts +32 -12
  54. package/src/types/bigint.ts +58 -31
  55. package/src/types/buffer/base.ts +53 -35
  56. package/src/types/buffer/fixed-length.ts +3 -8
  57. package/src/types/buffer/variable-length.ts +41 -24
  58. package/src/types/number.ts +131 -79
  59. package/src/utils.ts +30 -8
  60. package/tsconfig.build.json +1 -1
  61. package/tsconfig.build.tsbuildinfo +1 -1
@@ -1,6 +1,6 @@
1
- import type { StructFieldDefinition } from "./definition.js";
2
- import type { StructOptions } from "./options.js";
3
- import type { StructValue } from "./struct-value.js";
1
+ import { type StructFieldDefinition } from "./definition.js";
2
+ import { type StructOptions } from "./options.js";
3
+ import { type StructValue } from "./struct-value.js";
4
4
 
5
5
  /**
6
6
  * A field value defines how to serialize a field.
@@ -9,8 +9,12 @@ import type { StructValue } from "./struct-value.js";
9
9
  * helpful for the serialization process.
10
10
  */
11
11
  export abstract class StructFieldValue<
12
- TDefinition extends StructFieldDefinition<any, any, any> = StructFieldDefinition<any, any, any>
13
- > {
12
+ TDefinition extends StructFieldDefinition<
13
+ any,
14
+ any,
15
+ any
16
+ > = StructFieldDefinition<any, any, any>
17
+ > {
14
18
  /** Gets the definition associated with this runtime value */
15
19
  public readonly definition: TDefinition;
16
20
 
@@ -21,17 +25,19 @@ export abstract class StructFieldValue<
21
25
  public readonly struct: StructValue;
22
26
 
23
27
  public get hasCustomAccessors(): boolean {
24
- return this.get !== StructFieldValue.prototype.get ||
25
- this.set !== StructFieldValue.prototype.set;
28
+ return (
29
+ this.get !== StructFieldValue.prototype.get ||
30
+ this.set !== StructFieldValue.prototype.set
31
+ );
26
32
  }
27
33
 
28
- protected value: TDefinition['TValue'];
34
+ protected value: TDefinition["TValue"];
29
35
 
30
36
  public constructor(
31
37
  definition: TDefinition,
32
38
  options: Readonly<StructOptions>,
33
39
  struct: StructValue,
34
- value: TDefinition['TValue'],
40
+ value: TDefinition["TValue"]
35
41
  ) {
36
42
  this.definition = definition;
37
43
  this.options = options;
@@ -51,22 +57,19 @@ export abstract class StructFieldValue<
51
57
  /**
52
58
  * When implemented in derived classes, reads current field's value.
53
59
  */
54
- public get(): TDefinition['TValue'] {
60
+ public get(): TDefinition["TValue"] {
55
61
  return this.value;
56
62
  }
57
63
 
58
64
  /**
59
65
  * When implemented in derived classes, updates current field's value.
60
66
  */
61
- public set(value: TDefinition['TValue']): void {
67
+ public set(value: TDefinition["TValue"]): void {
62
68
  this.value = value;
63
69
  }
64
70
 
65
71
  /**
66
72
  * When implemented in derived classes, serializes this field into `dataView` at `offset`
67
73
  */
68
- public abstract serialize(
69
- dataView: DataView,
70
- offset: number,
71
- ): void;
74
+ public abstract serialize(dataView: DataView, offset: number): void;
72
75
  }
@@ -1,4 +1,4 @@
1
- import type { ValueOrPromise } from "../utils.js";
1
+ import { type ValueOrPromise } from "../utils.js";
2
2
 
3
3
  // TODO: allow over reading (returning a `Uint8Array`, an `offset` and a `length`) to avoid copying
4
4
 
@@ -1,4 +1,4 @@
1
- import type { StructFieldValue } from "./field-value.js";
1
+ import { type StructFieldValue } from "./field-value.js";
2
2
 
3
3
  export const STRUCT_VALUE_SYMBOL = Symbol("struct-value");
4
4
 
@@ -6,25 +6,27 @@ export const STRUCT_VALUE_SYMBOL = Symbol("struct-value");
6
6
  * A struct value is a map between keys in a struct and their field values.
7
7
  */
8
8
  export class StructValue {
9
- /** @internal */ readonly fieldValues: Record<PropertyKey, StructFieldValue> = {};
9
+ /** @internal */ readonly fieldValues: Record<
10
+ PropertyKey,
11
+ StructFieldValue
12
+ > = {};
10
13
 
11
14
  /**
12
15
  * Gets the result struct value object
13
16
  */
14
17
  public readonly value: Record<PropertyKey, unknown>;
15
18
 
16
- public constructor(prototype: any) {
19
+ public constructor(prototype: object) {
17
20
  // PERF: `Object.create(extra)` is 50% faster
18
21
  // than `Object.defineProperties(this.value, extra)`
19
- this.value = Object.create(prototype);
22
+ this.value = Object.create(prototype) as Record<PropertyKey, unknown>;
20
23
 
21
24
  // PERF: `Object.defineProperty` is slow
22
25
  // but we need it to be non-enumerable
23
- Object.defineProperty(
24
- this.value,
25
- STRUCT_VALUE_SYMBOL,
26
- { enumerable: false, value: this }
27
- );
26
+ Object.defineProperty(this.value, STRUCT_VALUE_SYMBOL, {
27
+ enumerable: false,
28
+ value: this,
29
+ });
28
30
  }
29
31
 
30
32
  /**
@@ -42,8 +44,12 @@ export class StructValue {
42
44
  Object.defineProperty(this.value, name, {
43
45
  configurable: true,
44
46
  enumerable: true,
45
- get() { return fieldValue.get(); },
46
- set(v) { fieldValue.set(v); },
47
+ get() {
48
+ return fieldValue.get();
49
+ },
50
+ set(v) {
51
+ fieldValue.set(v);
52
+ },
47
53
  });
48
54
  } else {
49
55
  this.value[name] = fieldValue.get();