@chr33s/pdf-restructure 5.0.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.
Files changed (93) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +323 -0
  3. package/dist/array.d.ts +15 -0
  4. package/dist/array.js +95 -0
  5. package/dist/array.js.map +1 -0
  6. package/dist/base.d.ts +4 -0
  7. package/dist/base.js +16 -0
  8. package/dist/base.js.map +1 -0
  9. package/dist/bitfield.d.ts +11 -0
  10. package/dist/bitfield.js +37 -0
  11. package/dist/bitfield.js.map +1 -0
  12. package/dist/boolean.d.ts +10 -0
  13. package/dist/boolean.js +18 -0
  14. package/dist/boolean.js.map +1 -0
  15. package/dist/buffer.d.ts +11 -0
  16. package/dist/buffer.js +31 -0
  17. package/dist/buffer.js.map +1 -0
  18. package/dist/decode-stream.d.ts +26 -0
  19. package/dist/decode-stream.js +84 -0
  20. package/dist/decode-stream.js.map +1 -0
  21. package/dist/encode-stream.d.ts +19 -0
  22. package/dist/encode-stream.js +137 -0
  23. package/dist/encode-stream.js.map +1 -0
  24. package/dist/enum.d.ts +11 -0
  25. package/dist/enum.js +25 -0
  26. package/dist/enum.js.map +1 -0
  27. package/dist/index.d.ts +17 -0
  28. package/dist/index.js +18 -0
  29. package/dist/index.js.map +1 -0
  30. package/dist/lazy-array.d.ts +22 -0
  31. package/dist/lazy-array.js +75 -0
  32. package/dist/lazy-array.js.map +1 -0
  33. package/dist/number.d.ts +51 -0
  34. package/dist/number.js +76 -0
  35. package/dist/number.js.map +1 -0
  36. package/dist/optional.d.ts +14 -0
  37. package/dist/optional.js +34 -0
  38. package/dist/optional.js.map +1 -0
  39. package/dist/pointer.d.ts +28 -0
  40. package/dist/pointer.js +160 -0
  41. package/dist/pointer.js.map +1 -0
  42. package/dist/reserved.d.ts +12 -0
  43. package/dist/reserved.js +23 -0
  44. package/dist/reserved.js.map +1 -0
  45. package/dist/string.d.ts +14 -0
  46. package/dist/string.js +123 -0
  47. package/dist/string.js.map +1 -0
  48. package/dist/struct.d.ts +15 -0
  49. package/dist/struct.js +93 -0
  50. package/dist/struct.js.map +1 -0
  51. package/dist/utils.d.ts +13 -0
  52. package/dist/utils.js +45 -0
  53. package/dist/utils.js.map +1 -0
  54. package/dist/versioned-struct.d.ts +18 -0
  55. package/dist/versioned-struct.js +129 -0
  56. package/dist/versioned-struct.js.map +1 -0
  57. package/package.json +47 -0
  58. package/src/array.ts +113 -0
  59. package/src/base.ts +17 -0
  60. package/src/bitfield.ts +46 -0
  61. package/src/boolean.ts +24 -0
  62. package/src/buffer.ts +40 -0
  63. package/src/decode-stream.ts +97 -0
  64. package/src/encode-stream.ts +161 -0
  65. package/src/enum.ts +32 -0
  66. package/src/index.ts +17 -0
  67. package/src/lazy-array.ts +91 -0
  68. package/src/number.ts +88 -0
  69. package/src/optional.ts +46 -0
  70. package/src/pointer.ts +204 -0
  71. package/src/reserved.ts +29 -0
  72. package/src/string.ts +154 -0
  73. package/src/struct.ts +127 -0
  74. package/src/utils.ts +55 -0
  75. package/src/versioned-struct.ts +174 -0
  76. package/test/array.test.ts +95 -0
  77. package/test/bitfield.test.ts +52 -0
  78. package/test/boolean.test.ts +35 -0
  79. package/test/buffer.test.ts +49 -0
  80. package/test/decode-stream.test.ts +104 -0
  81. package/test/encode-stream.test.ts +111 -0
  82. package/test/enum.test.ts +30 -0
  83. package/test/lazy-array.test.ts +70 -0
  84. package/test/number.test.ts +222 -0
  85. package/test/optional.test.ts +105 -0
  86. package/test/pointer.test.ts +248 -0
  87. package/test/reserved.test.ts +28 -0
  88. package/test/string.test.ts +114 -0
  89. package/test/struct.test.ts +164 -0
  90. package/test/versioned-struct.test.ts +462 -0
  91. package/tsconfig.json +9 -0
  92. package/tsconfig.typecheck.json +14 -0
  93. package/vitest.config.ts +12 -0
@@ -0,0 +1,111 @@
1
+ import { Buffer } from "node:buffer";
2
+ import { describe, expect, test } from "vitest";
3
+ import { EncodeStream } from "../src/index.js";
4
+
5
+ describe("EncodeStream", () => {
6
+ test("should write a buffer", () => {
7
+ const stream = new EncodeStream(new Uint8Array(3));
8
+ stream.writeBuffer(new Uint8Array([1, 2, 3]));
9
+ expect(stream.buffer).to.deep.equal(new Uint8Array([1, 2, 3]));
10
+ });
11
+
12
+ test("should writeUInt16BE", () => {
13
+ const stream = new EncodeStream(new Uint8Array(2));
14
+ stream.writeUInt16BE(0xabcd);
15
+ expect(stream.buffer).to.deep.equal(new Uint8Array([0xab, 0xcd]));
16
+ });
17
+
18
+ test("should writeUInt16LE", () => {
19
+ const stream = new EncodeStream(new Uint8Array(2));
20
+ stream.writeUInt16LE(0xcdab);
21
+ expect(stream.buffer).to.deep.equal(new Uint8Array([0xab, 0xcd]));
22
+ });
23
+
24
+ test("should writeUInt24BE", () => {
25
+ const stream = new EncodeStream(new Uint8Array(3));
26
+ stream.writeUInt24BE(0xabcdef);
27
+ expect(stream.buffer).to.deep.equal(new Uint8Array([0xab, 0xcd, 0xef]));
28
+ });
29
+
30
+ test("should writeUInt24LE", () => {
31
+ const stream = new EncodeStream(new Uint8Array(3));
32
+ stream.writeUInt24LE(0xabcdef);
33
+ expect(stream.buffer).to.deep.equal(new Uint8Array([0xef, 0xcd, 0xab]));
34
+ });
35
+
36
+ test("should writeInt24BE", () => {
37
+ const stream = new EncodeStream(new Uint8Array(6));
38
+ stream.writeInt24BE(-21724);
39
+ stream.writeInt24BE(0xabcdef);
40
+ expect(stream.buffer).to.deep.equal(new Uint8Array([0xff, 0xab, 0x24, 0xab, 0xcd, 0xef]));
41
+ });
42
+
43
+ test("should writeInt24LE", () => {
44
+ const stream = new EncodeStream(new Uint8Array(6));
45
+ stream.writeInt24LE(-21724);
46
+ stream.writeInt24LE(0xabcdef);
47
+ expect(stream.buffer).to.deep.equal(new Uint8Array([0x24, 0xab, 0xff, 0xef, 0xcd, 0xab]));
48
+ });
49
+
50
+ test("should fill", () => {
51
+ const stream = new EncodeStream(new Uint8Array(5));
52
+ stream.fill(10, 5);
53
+ expect(stream.buffer).to.deep.equal(new Uint8Array([10, 10, 10, 10, 10]));
54
+ });
55
+
56
+ describe("writeString", () => {
57
+ test("should encode ascii by default", () => {
58
+ const expected = Buffer.from("some text", "ascii");
59
+ const stream = new EncodeStream(new Uint8Array(expected.length));
60
+ stream.writeString("some text");
61
+ expect(stream.buffer).to.deep.equal(new Uint8Array(expected));
62
+ });
63
+
64
+ test("should encode ascii", () => {
65
+ const expected = Buffer.from("some text", "ascii");
66
+ const stream = new EncodeStream(new Uint8Array(expected.length));
67
+ stream.writeString("some text", "ascii");
68
+ expect(stream.buffer).to.deep.equal(new Uint8Array(expected));
69
+ });
70
+
71
+ test("should encode utf8", () => {
72
+ const expected = Buffer.from("unicode! 👍", "utf8");
73
+ const stream = new EncodeStream(new Uint8Array(expected.length));
74
+ stream.writeString("unicode! 👍", "utf8");
75
+ expect(stream.buffer).to.deep.equal(new Uint8Array(expected));
76
+ });
77
+
78
+ test("should encode utf16le", () => {
79
+ const expected = Buffer.from("unicode! 👍", "utf16le");
80
+ const stream = new EncodeStream(new Uint8Array(expected.length));
81
+ stream.writeString("unicode! 👍", "utf16le");
82
+ expect(stream.buffer).to.deep.equal(new Uint8Array(expected));
83
+ });
84
+
85
+ test("should encode ucs2", () => {
86
+ const expected = Buffer.from("unicode! 👍", "ucs2");
87
+ const stream = new EncodeStream(new Uint8Array(expected.length));
88
+ stream.writeString("unicode! 👍", "ucs2");
89
+ expect(stream.buffer).to.deep.equal(new Uint8Array(expected));
90
+ });
91
+
92
+ test("should encode utf16be", () => {
93
+ const expected = Buffer.from("unicode! 👍", "utf16le");
94
+ for (let i = 0; i < expected.length - 1; i += 2) {
95
+ const byte = expected[i];
96
+ expected[i] = expected[i + 1];
97
+ expected[i + 1] = byte;
98
+ }
99
+ const stream = new EncodeStream(new Uint8Array(expected.length));
100
+ stream.writeString("unicode! 👍", "utf16be");
101
+ expect(stream.buffer).to.deep.equal(new Uint8Array(expected));
102
+ });
103
+
104
+ test("should throw for unsupported encoding", () => {
105
+ const stream = new EncodeStream(new Uint8Array(19));
106
+ expect(() => stream.writeString("äccented cháracters", "mac")).to.throw(
107
+ "Unsupported encoding: mac",
108
+ );
109
+ });
110
+ });
111
+ });
@@ -0,0 +1,30 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { DecodeStream, EncodeStream, Enum, uint8 } from "../src/index.js";
3
+
4
+ describe("Enum", () => {
5
+ const e = new Enum(uint8, ["foo", "bar", "baz"]);
6
+
7
+ test("should have the right size", () => {
8
+ expect(e.size()).to.equal(1);
9
+ });
10
+
11
+ test("should decode", () => {
12
+ const stream = new DecodeStream(new Uint8Array([1, 2, 0]));
13
+ expect(e.decode(stream)).to.equal("bar");
14
+ expect(e.decode(stream)).to.equal("baz");
15
+ expect(e.decode(stream)).to.equal("foo");
16
+ });
17
+
18
+ test("should encode", () => {
19
+ const stream = new EncodeStream(new Uint8Array(3));
20
+ e.encode(stream, "bar");
21
+ e.encode(stream, "baz");
22
+ e.encode(stream, "foo");
23
+ expect(stream.buffer).to.deep.equal(new Uint8Array([1, 2, 0]));
24
+ });
25
+
26
+ test("should throw on unknown option", () => {
27
+ const stream = new EncodeStream(new Uint8Array(1));
28
+ expect(() => e.encode(stream, "unknown")).to.throw(/unknown option/i);
29
+ });
30
+ });
@@ -0,0 +1,70 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { DecodeStream, EncodeStream, LazyArray as LazyArrayT, uint8 } from "../src/index.js";
3
+
4
+ describe("LazyArray", () => {
5
+ describe("decode", () => {
6
+ test("should decode items lazily", () => {
7
+ const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
8
+ const array = new LazyArrayT(uint8, 4);
9
+
10
+ const arr = array.decode(stream);
11
+ expect(arr).to.not.be.an("array");
12
+ expect(arr).to.have.length(4);
13
+ expect(stream.pos).to.equal(4);
14
+
15
+ expect(arr.get(0)).to.equal(1);
16
+ expect(arr.get(1)).to.equal(2);
17
+ expect(arr.get(2)).to.equal(3);
18
+ expect(arr.get(3)).to.equal(4);
19
+
20
+ expect(arr.get(-1)).to.equal(undefined);
21
+ expect(arr.get(5)).to.equal(undefined);
22
+ });
23
+
24
+ test("should be able to convert to an array", () => {
25
+ const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
26
+ const array = new LazyArrayT(uint8, 4);
27
+ const arr = array.decode(stream);
28
+
29
+ expect(arr.toArray()).to.deep.equal([1, 2, 3, 4]);
30
+ });
31
+
32
+ test("should have an inspect method", () => {
33
+ const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
34
+ const array = new LazyArrayT(uint8, 4);
35
+ const arr = array.decode(stream);
36
+
37
+ expect(arr.inspect()).to.equal("[1,2,3,4]");
38
+ });
39
+
40
+ test("should decode length as number before array", () => {
41
+ const stream = new DecodeStream(new Uint8Array([4, 1, 2, 3, 4, 5]));
42
+ const array = new LazyArrayT(uint8, uint8);
43
+ const arr = array.decode(stream);
44
+
45
+ expect(arr.toArray()).to.deep.equal([1, 2, 3, 4]);
46
+ });
47
+ });
48
+
49
+ describe("size", () => {
50
+ test("should work with LazyArrays", () => {
51
+ const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
52
+ const array = new LazyArrayT(uint8, 4);
53
+ const arr = array.decode(stream);
54
+
55
+ expect(array.size(arr)).to.equal(4);
56
+ });
57
+ });
58
+
59
+ describe("encode", () => {
60
+ test("should work with LazyArrays", () => {
61
+ const stream = new DecodeStream(new Uint8Array([1, 2, 3, 4, 5]));
62
+ const array = new LazyArrayT(uint8, 4);
63
+ const arr = array.decode(stream);
64
+
65
+ const enc = new EncodeStream(new Uint8Array(array.size(arr)));
66
+ array.encode(enc, arr);
67
+ expect(enc.buffer).to.deep.equal(new Uint8Array([1, 2, 3, 4]));
68
+ });
69
+ });
70
+ });
@@ -0,0 +1,222 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import {
3
+ DecodeStream,
4
+ double,
5
+ doublebe,
6
+ doublele,
7
+ EncodeStream,
8
+ fixed16,
9
+ fixed16be,
10
+ fixed16le,
11
+ fixed32,
12
+ fixed32be,
13
+ fixed32le,
14
+ float,
15
+ floatbe,
16
+ floatle,
17
+ int16,
18
+ int16be,
19
+ int16le,
20
+ int24,
21
+ int24be,
22
+ int24le,
23
+ int32,
24
+ int32be,
25
+ int32le,
26
+ int8,
27
+ uint16,
28
+ uint16be,
29
+ uint16le,
30
+ uint24,
31
+ uint24be,
32
+ uint24le,
33
+ uint32,
34
+ uint32be,
35
+ uint32le,
36
+ uint8,
37
+ } from "../src/index.js";
38
+
39
+ type IntegerCase = {
40
+ name: string;
41
+ type: any;
42
+ buffer: number[];
43
+ expected: number[];
44
+ size: number;
45
+ };
46
+
47
+ const integerCases: IntegerCase[] = [
48
+ { name: "uint8", type: uint8, buffer: [0xab, 0xff], expected: [0xab, 0xff], size: 1 },
49
+ { name: "uint16be", type: uint16be, buffer: [0xab, 0xff], expected: [0xabff], size: 2 },
50
+ { name: "uint16le", type: uint16le, buffer: [0xff, 0xab], expected: [0xabff], size: 2 },
51
+ { name: "uint24be", type: uint24be, buffer: [0xff, 0xab, 0x24], expected: [0xffab24], size: 3 },
52
+ { name: "uint24le", type: uint24le, buffer: [0x24, 0xab, 0xff], expected: [0xffab24], size: 3 },
53
+ {
54
+ name: "uint32be",
55
+ type: uint32be,
56
+ buffer: [0xff, 0xab, 0x24, 0xbf],
57
+ expected: [0xffab24bf],
58
+ size: 4,
59
+ },
60
+ {
61
+ name: "uint32le",
62
+ type: uint32le,
63
+ buffer: [0xbf, 0x24, 0xab, 0xff],
64
+ expected: [0xffab24bf],
65
+ size: 4,
66
+ },
67
+ { name: "int8", type: int8, buffer: [0x7f, 0xff], expected: [127, -1], size: 1 },
68
+ { name: "int16be", type: int16be, buffer: [0xff, 0xab], expected: [-85], size: 2 },
69
+ { name: "int16le", type: int16le, buffer: [0xab, 0xff], expected: [-85], size: 2 },
70
+ { name: "int24be", type: int24be, buffer: [0xff, 0xab, 0x24], expected: [-21724], size: 3 },
71
+ { name: "int24le", type: int24le, buffer: [0x24, 0xab, 0xff], expected: [-21724], size: 3 },
72
+ {
73
+ name: "int32be",
74
+ type: int32be,
75
+ buffer: [0xff, 0xab, 0x24, 0xbf],
76
+ expected: [-5561153],
77
+ size: 4,
78
+ },
79
+ {
80
+ name: "int32le",
81
+ type: int32le,
82
+ buffer: [0xbf, 0x24, 0xab, 0xff],
83
+ expected: [-5561153],
84
+ size: 4,
85
+ },
86
+ ];
87
+
88
+ type FloatCase = {
89
+ name: string;
90
+ type: any;
91
+ buffer: number[];
92
+ expected: number;
93
+ size: number;
94
+ precision?: number;
95
+ };
96
+
97
+ const floatCases: FloatCase[] = [
98
+ {
99
+ name: "floatbe",
100
+ type: floatbe,
101
+ buffer: [0x43, 0x7a, 0x8c, 0xcd],
102
+ expected: 250.55,
103
+ size: 4,
104
+ precision: 0.005,
105
+ },
106
+ {
107
+ name: "floatle",
108
+ type: floatle,
109
+ buffer: [0xcd, 0x8c, 0x7a, 0x43],
110
+ expected: 250.55,
111
+ size: 4,
112
+ precision: 0.005,
113
+ },
114
+ {
115
+ name: "doublebe",
116
+ type: doublebe,
117
+ buffer: [0x40, 0x93, 0x4a, 0x3d, 0x70, 0xa3, 0xd7, 0x0a],
118
+ expected: 1234.56,
119
+ size: 8,
120
+ },
121
+ {
122
+ name: "doublele",
123
+ type: doublele,
124
+ buffer: [0x0a, 0xd7, 0xa3, 0x70, 0x3d, 0x4a, 0x93, 0x40],
125
+ expected: 1234.56,
126
+ size: 8,
127
+ },
128
+ {
129
+ name: "fixed16be",
130
+ type: fixed16be,
131
+ buffer: [0x19, 0x57],
132
+ expected: 25.34,
133
+ size: 2,
134
+ precision: 0.005,
135
+ },
136
+ {
137
+ name: "fixed16le",
138
+ type: fixed16le,
139
+ buffer: [0x57, 0x19],
140
+ expected: 25.34,
141
+ size: 2,
142
+ precision: 0.005,
143
+ },
144
+ {
145
+ name: "fixed32be",
146
+ type: fixed32be,
147
+ buffer: [0x00, 0xfa, 0x8c, 0xcc],
148
+ expected: 250.55,
149
+ size: 4,
150
+ precision: 0.005,
151
+ },
152
+ {
153
+ name: "fixed32le",
154
+ type: fixed32le,
155
+ buffer: [0xcc, 0x8c, 0xfa, 0x00],
156
+ expected: 250.55,
157
+ size: 4,
158
+ precision: 0.005,
159
+ },
160
+ ];
161
+
162
+ const aliasCases: Array<[string, any, any]> = [
163
+ ["uint16", uint16, uint16be],
164
+ ["uint24", uint24, uint24be],
165
+ ["uint32", uint32, uint32be],
166
+ ["int16", int16, int16be],
167
+ ["int24", int24, int24be],
168
+ ["int32", int32, int32be],
169
+ ["float", float, floatbe],
170
+ ["double", double, doublebe],
171
+ ["fixed16", fixed16, fixed16be],
172
+ ["fixed32", fixed32, fixed32be],
173
+ ];
174
+
175
+ describe("Number", () => {
176
+ test.each(aliasCases)("%s should alias the big-endian variant", (name, alias, target) => {
177
+ expect(alias).to.equal(target);
178
+ });
179
+
180
+ describe.each(integerCases)("$name", ({ type, buffer, expected, size }) => {
181
+ test("should decode", () => {
182
+ const stream = new DecodeStream(new Uint8Array(buffer));
183
+ expected.forEach((value) => {
184
+ expect(type.decode(stream)).to.equal(value);
185
+ });
186
+ });
187
+
188
+ test("should report size", () => {
189
+ expect(type.size()).to.equal(size);
190
+ });
191
+
192
+ test("should encode", () => {
193
+ const stream = new EncodeStream(new Uint8Array(buffer.length));
194
+ expected.forEach((value) => {
195
+ type.encode(stream, value);
196
+ });
197
+ expect(stream.buffer).to.deep.equal(new Uint8Array(buffer));
198
+ });
199
+ });
200
+
201
+ describe.each(floatCases)("$name", ({ type, buffer, expected, size, precision }) => {
202
+ test("should decode", () => {
203
+ const stream = new DecodeStream(new Uint8Array(buffer));
204
+ const value = type.decode(stream);
205
+ if (precision) {
206
+ expect(value).to.be.closeTo(expected, precision);
207
+ } else {
208
+ expect(value).to.equal(expected);
209
+ }
210
+ });
211
+
212
+ test("should report size", () => {
213
+ expect(type.size()).to.equal(size);
214
+ });
215
+
216
+ test("should encode", () => {
217
+ const stream = new EncodeStream(new Uint8Array(buffer.length));
218
+ type.encode(stream, expected);
219
+ expect(stream.buffer).to.deep.equal(new Uint8Array(buffer));
220
+ });
221
+ });
222
+ });
@@ -0,0 +1,105 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { DecodeStream, EncodeStream, Optional, uint8 } from "../src/index.js";
3
+
4
+ describe("Optional", () => {
5
+ describe("decode", () => {
6
+ test("should not decode when condition is falsy", () => {
7
+ const stream = new DecodeStream(new Uint8Array([0]));
8
+ const optional = new Optional(uint8, false);
9
+ expect(optional.decode(stream)).to.equal(undefined);
10
+ expect(stream.pos).to.equal(0);
11
+ });
12
+
13
+ test("should not decode when condition is a function and falsy", () => {
14
+ const stream = new DecodeStream(new Uint8Array([0]));
15
+ const optional = new Optional(uint8, () => false);
16
+ expect(optional.decode(stream)).to.equal(undefined);
17
+ expect(stream.pos).to.equal(0);
18
+ });
19
+
20
+ test("should decode when condition is omitted", () => {
21
+ const stream = new DecodeStream(new Uint8Array([0]));
22
+ const optional = new Optional(uint8);
23
+ expect(optional.decode(stream)).to.equal(0);
24
+ expect(stream.pos).to.equal(1);
25
+ });
26
+
27
+ test("should decode when condition is truthy", () => {
28
+ const stream = new DecodeStream(new Uint8Array([0]));
29
+ const optional = new Optional(uint8, true);
30
+ expect(optional.decode(stream)).to.equal(0);
31
+ expect(stream.pos).to.equal(1);
32
+ });
33
+
34
+ test("should decode when condition is a function and truthy", () => {
35
+ const stream = new DecodeStream(new Uint8Array([0]));
36
+ const optional = new Optional(uint8, () => true);
37
+ expect(optional.decode(stream)).to.equal(0);
38
+ expect(stream.pos).to.equal(1);
39
+ });
40
+ });
41
+
42
+ describe("size", () => {
43
+ test("should return 0 when condition is falsy", () => {
44
+ const optional = new Optional(uint8, false);
45
+ expect(optional.size()).to.equal(0);
46
+ });
47
+
48
+ test("should return 0 when condition is a function and falsy", () => {
49
+ const optional = new Optional(uint8, () => false);
50
+ expect(optional.size()).to.equal(0);
51
+ });
52
+
53
+ test("should return given type size when condition is omitted", () => {
54
+ const optional = new Optional(uint8);
55
+ expect(optional.size()).to.equal(1);
56
+ });
57
+
58
+ test("should return given type size when condition is truthy", () => {
59
+ const optional = new Optional(uint8, true);
60
+ expect(optional.size()).to.equal(1);
61
+ });
62
+
63
+ test("should return given type size when condition is a function and truthy", () => {
64
+ const optional = new Optional(uint8, () => true);
65
+ expect(optional.size()).to.equal(1);
66
+ });
67
+ });
68
+
69
+ describe("encode", () => {
70
+ test("should not encode when condition is falsy", () => {
71
+ const optional = new Optional(uint8, false);
72
+ const stream = new EncodeStream(new Uint8Array(0));
73
+ optional.encode(stream, 128);
74
+ expect(stream.buffer).to.deep.equal(new Uint8Array(0));
75
+ });
76
+
77
+ test("should not encode when condition is a function and falsy", () => {
78
+ const optional = new Optional(uint8, () => false);
79
+ const stream = new EncodeStream(new Uint8Array(0));
80
+ optional.encode(stream, 128);
81
+ expect(stream.buffer).to.deep.equal(new Uint8Array(0));
82
+ });
83
+
84
+ test("should encode when condition is omitted", () => {
85
+ const optional = new Optional(uint8);
86
+ const stream = new EncodeStream(new Uint8Array(1));
87
+ optional.encode(stream, 128);
88
+ expect(stream.buffer).to.deep.equal(new Uint8Array([128]));
89
+ });
90
+
91
+ test("should encode when condition is truthy", () => {
92
+ const optional = new Optional(uint8, true);
93
+ const stream = new EncodeStream(new Uint8Array(1));
94
+ optional.encode(stream, 128);
95
+ expect(stream.buffer).to.deep.equal(new Uint8Array([128]));
96
+ });
97
+
98
+ test("should encode when condition is a function and truthy", () => {
99
+ const optional = new Optional(uint8, () => true);
100
+ const stream = new EncodeStream(new Uint8Array(1));
101
+ optional.encode(stream, 128);
102
+ expect(stream.buffer).to.deep.equal(new Uint8Array([128]));
103
+ });
104
+ });
105
+ });