@rotu/structview 0.12.0 → 0.13.0

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/README.md CHANGED
@@ -75,7 +75,7 @@ class Menu extends defineArray({
75
75
  length: 3,
76
76
  }) {}
77
77
 
78
- const myMenu = new Menu({ byteLength: 48 })
78
+ const myMenu = Menu.alloc({ byteLength: 48 })
79
79
  Object.assign(myMenu.item(0), { name: "garden salad", price: 4 })
80
80
  Object.assign(myMenu.item(1), { name: "soup du jour", price: 2.5 })
81
81
  Object.assign(myMenu.item(2), { name: "fries", price: 2.25 })
package/esm/core.d.ts CHANGED
@@ -24,6 +24,19 @@ export declare function structBytes(struct: AnyStruct, start?: number, end?: num
24
24
  */
25
25
  export declare class Struct {
26
26
  [dataViewSymbol]: DataView;
27
+ /**
28
+ * Create a new instance of this Struct in a newly allocated buffer
29
+ * @param args options for allocation. Note that if the class does not have a static BYTE_LENGTH property, you *must* provide a byteLength here.
30
+ * @returns
31
+ */
32
+ static alloc<T extends Struct>(this: {
33
+ new ({ buffer }: {
34
+ readonly buffer: ArrayBufferLike;
35
+ }): T;
36
+ }, args?: {
37
+ byteLength?: number;
38
+ maxByteLength?: number;
39
+ }): T;
27
40
  get [Symbol.toStringTag](): string;
28
41
  static toDataView(o: Struct): DataView;
29
42
  static [Symbol.hasInstance](instance: unknown): boolean;
@@ -35,7 +48,8 @@ export declare class Struct {
35
48
  * @remarks
36
49
  * The reason we don't use a positional argument for `byteLength` is because it's confusing - TypedArray constructors take a `length` argument that is in elements, not bytes.
37
50
  * Another reason is because this allows you to pass in a `DataView` or a `TypedArray` directly to reinterpret its memory as a struct.
38
- */
51
+ *
52
+ * Instead of passing an undefined `buffer` property, consider using the `.alloc` static method. */
39
53
  constructor(arg: {
40
54
  readonly buffer: ArrayBufferLike;
41
55
  readonly byteOffset?: number;
package/esm/core.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"core.d.ts","sourceRoot":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@@ export function structBytes(struct, start, end) {
41
41
  * Note there are no predeclared string-keyed properties - all property names are reserved for user-defined fields
42
42
  */
43
43
  export class Struct {
44
+ /**
45
+ * Create a new instance of this Struct in a newly allocated buffer
46
+ * @param args options for allocation. Note that if the class does not have a static BYTE_LENGTH property, you *must* provide a byteLength here.
47
+ * @returns
48
+ */
49
+ static alloc(args) {
50
+ const byteLength = args?.byteLength ?? Reflect.get(this, "BYTE_LENGTH");
51
+ const maxByteLength = args?.maxByteLength ??
52
+ Reflect.get(this, "MAX_BYTE_LENGTH");
53
+ if (typeof byteLength !== "number") {
54
+ throw new TypeError("Cannot allocate struct - its class must have a static BYTE_LENGTH property");
55
+ }
56
+ const buffer = new ArrayBuffer(byteLength, { maxByteLength });
57
+ return new this({ buffer });
58
+ }
44
59
  get [(_a = dataViewSymbol, Symbol.toStringTag)]() {
45
60
  return Struct.name;
46
61
  }
@@ -60,7 +75,8 @@ export class Struct {
60
75
  * @remarks
61
76
  * The reason we don't use a positional argument for `byteLength` is because it's confusing - TypedArray constructors take a `length` argument that is in elements, not bytes.
62
77
  * Another reason is because this allows you to pass in a `DataView` or a `TypedArray` directly to reinterpret its memory as a struct.
63
- */
78
+ *
79
+ * Instead of passing an undefined `buffer` property, consider using the `.alloc` static method. */
64
80
  constructor(arg) {
65
81
  Object.defineProperty(this, _a, {
66
82
  enumerable: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rotu/structview",
3
- "version": "0.12.0+dnt",
3
+ "version": "0.13.0+dnt",
4
4
  "description": "Read and write structured binary data with typesafe views",
5
5
  "repository": {
6
6
  "type": "git",