compact-encoding 2.8.0 → 2.9.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 (4) hide show
  1. package/README.md +4 -3
  2. package/index.js +4 -4
  3. package/package.json +1 -1
  4. package/test.js +69 -77
package/README.md CHANGED
@@ -11,13 +11,13 @@ npm install compact-encoding
11
11
  ``` js
12
12
  const cenc = require('compact-encoding')
13
13
 
14
- const state = { start: 0, end: 0, buffer: null }
14
+ const state = cenc.state()
15
15
 
16
16
  // use preencode to figure out how big a buffer is needed
17
17
  cenc.uint.preencode(state, 42)
18
18
  cenc.string.preencode(state, 'hi')
19
19
 
20
- console.log(state) // { start: 0, end: 4, buffer: null }
20
+ console.log(state) // { start: 0, end: 4, buffer: null, cache: null }
21
21
 
22
22
  state.buffer = Buffer.allocUnsafe(state.end)
23
23
 
@@ -36,13 +36,14 @@ cenc.string.decode(state) // 'hi'
36
36
 
37
37
  #### `state`
38
38
 
39
- Should be an object that looks like this `{ start, end, buffer }`.
39
+ Should be an object that looks like this `{ start, end, buffer, cache }`.
40
40
 
41
41
  You can also get a blank state object using `cenc.state()`.
42
42
 
43
43
  * `start` is the byte offset to start encoding/decoding at.
44
44
  * `end` is the byte offset indicating the end of the buffer.
45
45
  * `buffer` should be either a Node.js Buffer or Uint8Array.
46
+ * `cache` is used internally be codecs, starts out as `null`.
46
47
 
47
48
  #### `enc.preencode(state, val)`
48
49
 
package/index.js CHANGED
@@ -3,8 +3,8 @@ const b4a = require('b4a')
3
3
  const LE = (new Uint8Array(new Uint16Array([0xff]).buffer))[0] === 0xff
4
4
  const BE = !LE
5
5
 
6
- exports.state = function () {
7
- return { start: 0, end: 0, buffer: null }
6
+ exports.state = function (start = 0, end = 0, buffer = null) {
7
+ return { start, end, buffer, cache: null }
8
8
  }
9
9
 
10
10
  const uint = exports.uint = {
@@ -445,7 +445,7 @@ function fromAbstractEncoder (enc) {
445
445
  }
446
446
 
447
447
  exports.encode = function encode (enc, m) {
448
- const state = { start: 0, end: 0, buffer: null }
448
+ const state = exports.state()
449
449
  enc.preencode(state, m)
450
450
  state.buffer = b4a.allocUnsafe(state.end)
451
451
  enc.encode(state, m)
@@ -453,7 +453,7 @@ exports.encode = function encode (enc, m) {
453
453
  }
454
454
 
455
455
  exports.decode = function decode (enc, buffer) {
456
- return enc.decode({ start: 0, end: buffer.byteLength, buffer })
456
+ return enc.decode(exports.state(0, buffer.byteLength, buffer))
457
457
  }
458
458
 
459
459
  function zigZag (enc) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "description": "A series of compact encoding schemes for building small and fast parsers and serializers",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/test.js CHANGED
@@ -5,19 +5,19 @@ tape('uint', function (t) {
5
5
  const state = enc.state()
6
6
 
7
7
  enc.uint.preencode(state, 42)
8
- t.alike(state, { start: 0, end: 1, buffer: null })
8
+ t.alike(state, enc.state(0, 1))
9
9
  enc.uint.preencode(state, 4200)
10
- t.alike(state, { start: 0, end: 4, buffer: null })
10
+ t.alike(state, enc.state(0, 4))
11
11
  enc.uint.preencode(state, Number.MAX_SAFE_INTEGER)
12
- t.alike(state, { start: 0, end: 13, buffer: null })
12
+ t.alike(state, enc.state(0, 13))
13
13
 
14
14
  state.buffer = Buffer.alloc(state.end)
15
15
  enc.uint.encode(state, 42)
16
- t.alike(state, { start: 1, end: 13, buffer: Buffer.from([42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) })
16
+ t.alike(state, enc.state(1, 13, Buffer.from([42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])))
17
17
  enc.uint.encode(state, 4200)
18
- t.alike(state, { start: 4, end: 13, buffer: Buffer.from([42, 0xfd, 104, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0]) })
18
+ t.alike(state, enc.state(4, 13, Buffer.from([42, 0xfd, 104, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0])))
19
19
  enc.uint.encode(state, Number.MAX_SAFE_INTEGER)
20
- t.alike(state, { start: 13, end: 13, buffer: Buffer.from([42, 0xfd, 104, 16, 0xff, 255, 255, 255, 255, 255, 255, 31, 0]) })
20
+ t.alike(state, enc.state(13, 13, Buffer.from([42, 0xfd, 104, 16, 0xff, 255, 255, 255, 255, 255, 255, 31, 0])))
21
21
 
22
22
  state.start = 0
23
23
  t.is(enc.uint.decode(state), 42)
@@ -32,15 +32,15 @@ tape('int', function (t) {
32
32
  const state = enc.state()
33
33
 
34
34
  enc.int.preencode(state, 42)
35
- t.alike(state, { start: 0, end: 1, buffer: null })
35
+ t.alike(state, enc.state(0, 1))
36
36
  enc.int.preencode(state, -4200)
37
- t.alike(state, { start: 0, end: 4, buffer: null })
37
+ t.alike(state, enc.state(0, 4))
38
38
 
39
39
  state.buffer = Buffer.alloc(state.end)
40
40
  enc.int.encode(state, 42)
41
- t.alike(state, { start: 1, end: 4, buffer: Buffer.from([84, 0, 0, 0]) })
41
+ t.alike(state, enc.state(1, 4, Buffer.from([84, 0, 0, 0])))
42
42
  enc.int.encode(state, -4200)
43
- t.alike(state, { start: 4, end: 4, buffer: Buffer.from([84, 0xfd, 207, 32]) })
43
+ t.alike(state, enc.state(4, 4, Buffer.from([84, 0xfd, 207, 32])))
44
44
 
45
45
  state.start = 0
46
46
  t.is(enc.int.decode(state), 42)
@@ -54,12 +54,12 @@ tape('float64', function (t) {
54
54
  const state = enc.state()
55
55
 
56
56
  enc.float64.preencode(state, 162.2377294)
57
- t.alike(state, { start: 0, end: 8, buffer: null })
57
+ t.alike(state, enc.state(0, 8))
58
58
 
59
59
  state.buffer = Buffer.alloc(state.end)
60
- t.alike(state, { start: 0, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) })
60
+ t.alike(state, enc.state(0, 8, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0])))
61
61
  enc.float64.encode(state, 162.2377294)
62
- t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) })
62
+ t.alike(state, enc.state(8, 8, Buffer.from([0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40])))
63
63
 
64
64
  state.start = 0
65
65
  t.is(enc.float64.decode(state), 162.2377294)
@@ -74,13 +74,13 @@ tape('float64', function (t) {
74
74
 
75
75
  enc.int.preencode(state, 0)
76
76
  enc.float64.preencode(state, 162.2377294)
77
- t.alike(state, { start: 0, end: 9, buffer: null })
77
+ t.alike(state, enc.state(0, 9))
78
78
 
79
79
  state.buffer = Buffer.alloc(state.end)
80
- t.alike(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) })
80
+ t.alike(state, enc.state(0, 9, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0])))
81
81
  enc.int.encode(state, 0)
82
82
  enc.float64.encode(state, 162.2377294)
83
- t.alike(state, { start: 9, end: 9, buffer: Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) })
83
+ t.alike(state, enc.state(9, 9, Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40])))
84
84
 
85
85
  state.start = 0
86
86
  t.is(enc.int.decode(state), 0)
@@ -91,10 +91,10 @@ tape('float64', function (t) {
91
91
  const buf = Buffer.alloc(10)
92
92
  state.start = 0
93
93
  state.buffer = buf.subarray(1)
94
- t.alike(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) })
94
+ t.alike(state, enc.state(0, 9, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0])))
95
95
  enc.int.encode(state, 0)
96
96
  enc.float64.encode(state, 162.2377294)
97
- t.alike(state, { start: 9, end: 9, buffer: Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) })
97
+ t.alike(state, enc.state(9, 9, Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40])))
98
98
  t.alike(buf, Buffer.from([0, 0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]))
99
99
 
100
100
  state.start = 0
@@ -110,7 +110,7 @@ tape('float64', function (t) {
110
110
  enc.float64.preencode(state, 162.2377294)
111
111
  state.buffer = Buffer.alloc(state.end)
112
112
  enc.float64.encode(state, 0)
113
- t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) })
113
+ t.alike(state, enc.state(8, 8, Buffer.from([0, 0, 0, 0, 0, 0, 0, 0])))
114
114
 
115
115
  state.start = 0
116
116
  t.is(enc.float64.decode(state), 0)
@@ -124,7 +124,7 @@ tape('float64', function (t) {
124
124
  enc.float64.preencode(state, Infinity)
125
125
  state.buffer = Buffer.alloc(state.end)
126
126
  enc.float64.encode(state, Infinity)
127
- t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0xf0, 0x7f]) })
127
+ t.alike(state, enc.state(8, 8, Buffer.from([0, 0, 0, 0, 0, 0, 0xf0, 0x7f])))
128
128
 
129
129
  state.start = 0
130
130
  t.is(enc.float64.decode(state), Infinity)
@@ -138,7 +138,7 @@ tape('float64', function (t) {
138
138
  enc.float64.preencode(state, 0.1 + 0.2)
139
139
  state.buffer = Buffer.alloc(state.end)
140
140
  enc.float64.encode(state, 0.1 + 0.2)
141
- t.alike(state, { start: 8, end: 8, buffer: Buffer.from([0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f]) })
141
+ t.alike(state, enc.state(8, 8, Buffer.from([0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f])))
142
142
 
143
143
  state.start = 0
144
144
  t.is(enc.float64.decode(state), 0.1 + 0.2)
@@ -149,19 +149,19 @@ tape('buffer', function (t) {
149
149
  const state = enc.state()
150
150
 
151
151
  enc.buffer.preencode(state, Buffer.from('hi'))
152
- t.alike(state, { start: 0, end: 3, buffer: null })
152
+ t.alike(state, enc.state(0, 3))
153
153
  enc.buffer.preencode(state, Buffer.from('hello'))
154
- t.alike(state, { start: 0, end: 9, buffer: null })
154
+ t.alike(state, enc.state(0, 9))
155
155
  enc.buffer.preencode(state, null)
156
- t.alike(state, { start: 0, end: 10, buffer: null })
156
+ t.alike(state, enc.state(0, 10))
157
157
 
158
158
  state.buffer = Buffer.alloc(state.end)
159
159
  enc.buffer.encode(state, Buffer.from('hi'))
160
- t.alike(state, { start: 3, end: 10, buffer: Buffer.from('\x02hi\x00\x00\x00\x00\x00\x00\x00') })
160
+ t.alike(state, enc.state(3, 10, Buffer.from('\x02hi\x00\x00\x00\x00\x00\x00\x00')))
161
161
  enc.buffer.encode(state, Buffer.from('hello'))
162
- t.alike(state, { start: 9, end: 10, buffer: Buffer.from('\x02hi\x05hello\x00') })
162
+ t.alike(state, enc.state(9, 10, Buffer.from('\x02hi\x05hello\x00')))
163
163
  enc.buffer.encode(state, null)
164
- t.alike(state, { start: 10, end: 10, buffer: Buffer.from('\x02hi\x05hello\x00') })
164
+ t.alike(state, enc.state(10, 10, Buffer.from('\x02hi\x05hello\x00')))
165
165
 
166
166
  state.start = 0
167
167
  t.alike(enc.buffer.decode(state), Buffer.from('hi'))
@@ -176,11 +176,11 @@ tape('raw', function (t) {
176
176
  const state = enc.state()
177
177
 
178
178
  enc.raw.preencode(state, Buffer.from('hi'))
179
- t.alike(state, { start: 0, end: 2, buffer: null })
179
+ t.alike(state, enc.state(0, 2))
180
180
 
181
181
  state.buffer = Buffer.alloc(state.end)
182
182
  enc.raw.encode(state, Buffer.from('hi'))
183
- t.alike(state, { start: 2, end: 2, buffer: Buffer.from('hi') })
183
+ t.alike(state, enc.state(2, 2, Buffer.from('hi')))
184
184
 
185
185
  state.start = 0
186
186
  t.alike(enc.raw.decode(state), Buffer.from('hi'))
@@ -191,11 +191,11 @@ tape('uint16array', function (t) {
191
191
  const state = enc.state()
192
192
 
193
193
  enc.uint16array.preencode(state, new Uint16Array([1, 2, 3]))
194
- t.alike(state, { start: 0, end: 7, buffer: null })
194
+ t.alike(state, enc.state(0, 7))
195
195
 
196
196
  state.buffer = Buffer.alloc(state.end)
197
197
  enc.uint16array.encode(state, new Uint16Array([1, 2, 3]))
198
- t.alike(state, { start: 7, end: 7, buffer: Buffer.from([3, 1, 0, 2, 0, 3, 0]) })
198
+ t.alike(state, enc.state(7, 7, Buffer.from([3, 1, 0, 2, 0, 3, 0])))
199
199
 
200
200
  state.start = 0
201
201
  t.alike(enc.uint16array.decode(state), new Uint16Array([1, 2, 3]))
@@ -208,15 +208,15 @@ tape('uint32array', function (t) {
208
208
  const state = enc.state()
209
209
 
210
210
  enc.uint32array.preencode(state, new Uint32Array([1]))
211
- t.alike(state, { start: 0, end: 5, buffer: null })
211
+ t.alike(state, enc.state(0, 5))
212
212
  enc.uint32array.preencode(state, new Uint32Array([42, 43]))
213
- t.alike(state, { start: 0, end: 14, buffer: null })
213
+ t.alike(state, enc.state(0, 14))
214
214
 
215
215
  state.buffer = Buffer.alloc(state.end)
216
216
  enc.uint32array.encode(state, new Uint32Array([1]))
217
- t.alike(state, { start: 5, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) })
217
+ t.alike(state, enc.state(5, 14, Buffer.from([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])))
218
218
  enc.uint32array.encode(state, new Uint32Array([42, 43]))
219
- t.alike(state, { start: 14, end: 14, buffer: Buffer.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0]) })
219
+ t.alike(state, enc.state(14, 14, Buffer.from([1, 1, 0, 0, 0, 2, 42, 0, 0, 0, 43, 0, 0, 0])))
220
220
 
221
221
  state.start = 0
222
222
  t.alike(enc.uint32array.decode(state), new Uint32Array([1]))
@@ -230,11 +230,11 @@ tape('int16array', function (t) {
230
230
  const state = enc.state()
231
231
 
232
232
  enc.int16array.preencode(state, new Int16Array([1, -2, 3]))
233
- t.alike(state, { start: 0, end: 7, buffer: null })
233
+ t.alike(state, enc.state(0, 7))
234
234
 
235
235
  state.buffer = Buffer.alloc(state.end)
236
236
  enc.int16array.encode(state, new Int16Array([1, -2, 3]))
237
- t.alike(state, { start: 7, end: 7, buffer: Buffer.from([3, 1, 0, 0xfe, 0xff, 3, 0]) })
237
+ t.alike(state, enc.state(7, 7, Buffer.from([3, 1, 0, 0xfe, 0xff, 3, 0])))
238
238
 
239
239
  state.start = 0
240
240
  t.alike(enc.int16array.decode(state), new Int16Array([1, -2, 3]))
@@ -247,11 +247,11 @@ tape('int32array', function (t) {
247
247
  const state = enc.state()
248
248
 
249
249
  enc.int32array.preencode(state, new Int32Array([1, -2, 3]))
250
- t.alike(state, { start: 0, end: 13, buffer: null })
250
+ t.alike(state, enc.state(0, 13))
251
251
 
252
252
  state.buffer = Buffer.alloc(state.end)
253
253
  enc.int32array.encode(state, new Int32Array([1, -2, 3]))
254
- t.alike(state, { start: 13, end: 13, buffer: Buffer.from([3, 1, 0, 0, 0, 0xfe, 0xff, 0xff, 0xff, 3, 0, 0, 0]) })
254
+ t.alike(state, enc.state(13, 13, Buffer.from([3, 1, 0, 0, 0, 0xfe, 0xff, 0xff, 0xff, 3, 0, 0, 0])))
255
255
 
256
256
  state.start = 0
257
257
  t.alike(enc.int32array.decode(state), new Int32Array([1, -2, 3]))
@@ -264,11 +264,11 @@ tape('float32array', function (t) {
264
264
  const state = enc.state()
265
265
 
266
266
  enc.float32array.preencode(state, new Float32Array([1.1, -2.2, 3.3]))
267
- t.alike(state, { start: 0, end: 13, buffer: null })
267
+ t.alike(state, enc.state(0, 13))
268
268
 
269
269
  state.buffer = Buffer.alloc(state.end)
270
270
  enc.float32array.encode(state, new Float32Array([1.1, -2.2, 3.3]))
271
- t.alike(state, { start: 13, end: 13, buffer: Buffer.from([3, 0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0x0c, 0xc0, 0x33, 0x33, 0x53, 0x40]) })
271
+ t.alike(state, enc.state(13, 13, Buffer.from([3, 0xcd, 0xcc, 0x8c, 0x3f, 0xcd, 0xcc, 0x0c, 0xc0, 0x33, 0x33, 0x53, 0x40])))
272
272
 
273
273
  state.start = 0
274
274
  t.alike(enc.float32array.decode(state), new Float32Array([1.1, -2.2, 3.3]))
@@ -281,11 +281,11 @@ tape('float64array', function (t) {
281
281
  const state = enc.state()
282
282
 
283
283
  enc.float64array.preencode(state, new Float64Array([1.1, -2.2, 3.3]))
284
- t.alike(state, { start: 0, end: 25, buffer: null })
284
+ t.alike(state, enc.state(0, 25))
285
285
 
286
286
  state.buffer = Buffer.alloc(state.end)
287
287
  enc.float64array.encode(state, new Float64Array([1.1, -2.2, 3.3]))
288
- t.alike(state, { start: 25, end: 25, buffer: Buffer.from([3, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0x01, 0xc0, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x0a, 0x40]) })
288
+ t.alike(state, enc.state(25, 25, Buffer.from([3, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0xf1, 0x3f, 0x9a, 0x99, 0x99, 0x99, 0x99, 0x99, 0x01, 0xc0, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x0a, 0x40])))
289
289
 
290
290
  state.start = 0
291
291
  t.alike(enc.float64array.decode(state), new Float64Array([1.1, -2.2, 3.3]))
@@ -298,15 +298,15 @@ tape('string', function (t) {
298
298
  const state = enc.state()
299
299
 
300
300
  enc.string.preencode(state, '🌾')
301
- t.alike(state, { start: 0, end: 5, buffer: null })
301
+ t.alike(state, enc.state(0, 5))
302
302
  enc.string.preencode(state, 'høsten er fin')
303
- t.alike(state, { start: 0, end: 20, buffer: null })
303
+ t.alike(state, enc.state(0, 20))
304
304
 
305
305
  state.buffer = Buffer.alloc(state.end)
306
306
  enc.string.encode(state, '🌾')
307
- t.alike(state, { start: 5, end: 20, buffer: Buffer.from('\x04🌾\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') })
307
+ t.alike(state, enc.state(5, 20, Buffer.from('\x04🌾\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')))
308
308
  enc.string.encode(state, 'høsten er fin')
309
- t.alike(state, { start: 20, end: 20, buffer: Buffer.from('\x04🌾\x0ehøsten er fin') })
309
+ t.alike(state, enc.state(20, 20, Buffer.from('\x04🌾\x0ehøsten er fin')))
310
310
 
311
311
  state.start = 0
312
312
  t.is(enc.string.decode(state), '🌾')
@@ -320,15 +320,15 @@ tape('fixed32', function (t) {
320
320
  const state = enc.state()
321
321
 
322
322
  enc.fixed32.preencode(state, Buffer.alloc(32).fill('a'))
323
- t.alike(state, { start: 0, end: 32, buffer: null })
323
+ t.alike(state, enc.state(0, 32))
324
324
  enc.fixed32.preencode(state, Buffer.alloc(32).fill('b'))
325
- t.alike(state, { start: 0, end: 64, buffer: null })
325
+ t.alike(state, enc.state(0, 64))
326
326
 
327
327
  state.buffer = Buffer.alloc(state.end)
328
328
  enc.fixed32.encode(state, Buffer.alloc(32).fill('a'))
329
- t.alike(state, { start: 32, end: 64, buffer: Buffer.alloc(64).fill('a', 0, 32) })
329
+ t.alike(state, enc.state(32, 64, Buffer.alloc(64).fill('a', 0, 32)))
330
330
  enc.fixed32.encode(state, Buffer.alloc(32).fill('b'))
331
- t.alike(state, { start: 64, end: 64, buffer: Buffer.alloc(64).fill('a', 0, 32).fill('b', 32, 64) })
331
+ t.alike(state, enc.state(64, 64, Buffer.alloc(64).fill('a', 0, 32).fill('b', 32, 64)))
332
332
 
333
333
  state.start = 0
334
334
  t.alike(enc.fixed32.decode(state), Buffer.alloc(32).fill('a'))
@@ -342,15 +342,15 @@ tape('fixed64', function (t) {
342
342
  const state = enc.state()
343
343
 
344
344
  enc.fixed64.preencode(state, Buffer.alloc(64).fill('a'))
345
- t.alike(state, { start: 0, end: 64, buffer: null })
345
+ t.alike(state, enc.state(0, 64))
346
346
  enc.fixed64.preencode(state, Buffer.alloc(64).fill('b'))
347
- t.alike(state, { start: 0, end: 128, buffer: null })
347
+ t.alike(state, enc.state(0, 128))
348
348
 
349
349
  state.buffer = Buffer.alloc(state.end)
350
350
  enc.fixed64.encode(state, Buffer.alloc(64).fill('a'))
351
- t.alike(state, { start: 64, end: 128, buffer: Buffer.alloc(128).fill('a', 0, 64) })
351
+ t.alike(state, enc.state(64, 128, Buffer.alloc(128).fill('a', 0, 64)))
352
352
  enc.fixed64.encode(state, Buffer.alloc(64).fill('b'))
353
- t.alike(state, { start: 128, end: 128, buffer: Buffer.alloc(128).fill('a', 0, 64).fill('b', 64, 128) })
353
+ t.alike(state, enc.state(128, 128, Buffer.alloc(128).fill('a', 0, 64).fill('b', 64, 128)))
354
354
 
355
355
  state.start = 0
356
356
  t.alike(enc.fixed64.decode(state), Buffer.alloc(64).fill('a'))
@@ -365,15 +365,15 @@ tape('fixed n', function (t) {
365
365
  const fixed = enc.fixed(3)
366
366
 
367
367
  fixed.preencode(state, Buffer.alloc(3).fill('a'))
368
- t.alike(state, { start: 0, end: 3, buffer: null })
368
+ t.alike(state, enc.state(0, 3))
369
369
  fixed.preencode(state, Buffer.alloc(3).fill('b'))
370
- t.alike(state, { start: 0, end: 6, buffer: null })
370
+ t.alike(state, enc.state(0, 6))
371
371
 
372
372
  state.buffer = Buffer.alloc(state.end)
373
373
  fixed.encode(state, Buffer.alloc(3).fill('a'))
374
- t.alike(state, { start: 3, end: 6, buffer: Buffer.alloc(6).fill('a', 0, 3) })
374
+ t.alike(state, enc.state(3, 6, Buffer.alloc(6).fill('a', 0, 3)))
375
375
  fixed.encode(state, Buffer.alloc(3).fill('b'))
376
- t.alike(state, { start: 6, end: 6, buffer: Buffer.alloc(6).fill('a', 0, 3).fill('b', 3, 6) })
376
+ t.alike(state, enc.state(6, 6, Buffer.alloc(6).fill('a', 0, 3).fill('b', 3, 6)))
377
377
 
378
378
  state.start = 0
379
379
  t.alike(fixed.decode(state), Buffer.alloc(3).fill('a'))
@@ -390,15 +390,15 @@ tape('array', function (t) {
390
390
  const arr = enc.array(enc.bool)
391
391
 
392
392
  arr.preencode(state, [true, false, true])
393
- t.alike(state, { start: 0, end: 4, buffer: null })
393
+ t.alike(state, enc.state(0, 4))
394
394
  arr.preencode(state, [false, false, true, true])
395
- t.alike(state, { start: 0, end: 9, buffer: null })
395
+ t.alike(state, enc.state(0, 9))
396
396
 
397
397
  state.buffer = Buffer.alloc(state.end)
398
398
  arr.encode(state, [true, false, true])
399
- t.alike(state, { start: 4, end: 9, buffer: Buffer.from([3, 1, 0, 1, 0, 0, 0, 0, 0]) })
399
+ t.alike(state, enc.state(4, 9, Buffer.from([3, 1, 0, 1, 0, 0, 0, 0, 0])))
400
400
  arr.encode(state, [false, false, true, true])
401
- t.alike(state, { start: 9, end: 9, buffer: Buffer.from([3, 1, 0, 1, 4, 0, 0, 1, 1]) })
401
+ t.alike(state, enc.state(9, 9, Buffer.from([3, 1, 0, 1, 4, 0, 0, 1, 1])))
402
402
 
403
403
  state.start = 0
404
404
  t.alike(arr.decode(state), [true, false, true])
@@ -412,18 +412,14 @@ tape('json', function (t) {
412
412
  const state = enc.state()
413
413
 
414
414
  enc.json.preencode(state, { a: 1, b: 2 })
415
- t.alike(state, { start: 0, end: 14, buffer: null })
415
+ t.alike(state, enc.state(0, 14))
416
416
 
417
417
  state.buffer = Buffer.alloc(state.end)
418
418
  enc.json.encode(state, { a: 1, b: 2 })
419
- t.alike(state, {
420
- start: 14,
421
- end: 14,
422
- buffer: Buffer.concat([
423
- Buffer.from([13]),
424
- Buffer.from('{"a":1,"b":2}')
425
- ])
426
- })
419
+ t.alike(state, enc.state(14, 14, Buffer.concat([
420
+ Buffer.from([13]),
421
+ Buffer.from('{"a":1,"b":2}')
422
+ ])))
427
423
 
428
424
  state.start = 0
429
425
  t.alike(enc.json.decode(state), { a: 1, b: 2 })
@@ -488,11 +484,7 @@ tape('lexint: throws', function (t) {
488
484
 
489
485
  let num = 252
490
486
 
491
- const state = {
492
- start: 0,
493
- end: 0,
494
- buffer: null
495
- }
487
+ const state = enc.state()
496
488
 
497
489
  enc.lexint.preencode(state, num)
498
490
  state.buffer = Buffer.alloc(state.end - state.start)