@@ -37,9 +37,9 @@ buffer-layout`.
37
37
All examples are from the ` test/examples.js ` unit test and assume the
38
38
following context:
39
39
40
- var assert = require('assert');
41
- var util = require('util');
42
- var lo = require('buffer-layout');
40
+ const assert = require('assert');
41
+ const util = require('util');
42
+ const lo = require('buffer-layout');
43
43
44
44
The examples give only a taste of what can be done. Structures, unions,
45
45
and sequences can nest; [ union
@@ -57,8 +57,8 @@ The C definition:
57
57
58
58
The buffer-layout way:
59
59
60
- var ds = lo.seq(lo.s16(), 4);
61
- var b = new Buffer(8);
60
+ const ds = lo.seq(lo.s16(), 4);
61
+ const b = new Buffer(8);
62
62
assert.equal(ds.encode([1, -1, 3, -3], b), 4 * 2);
63
63
assert.equal(Buffer('0100ffff0300fdff', 'hex').compare(b), 0);
64
64
assert.deepEqual(ds.decode(b), [1, -1, 3, -3]);
@@ -77,11 +77,11 @@ The C definition:
77
77
78
78
The buffer-layout way:
79
79
80
- var ds = lo.struct([lo.u8('v'),
80
+ const ds = lo.struct([lo.u8('v'),
81
81
lo.seq(lo.u8(), 3), // alignment padding
82
82
lo.u32('u32')]);
83
83
assert.equal(ds.offsetOf('u32'), 4);
84
- var b = new Buffer(8);
84
+ const b = new Buffer(8);
85
85
b.fill(0xbd);
86
86
assert.equal(ds.encode({v: 1, u32: 0x12345678}, b), 1 + 3 + 4);
87
87
assert.equal(Buffer('01bdbdbd78563412', 'hex').compare(b), 0);
@@ -105,10 +105,10 @@ The C definition:
105
105
106
106
The buffer-layout way:
107
107
108
- var ds = lo.struct([lo.u8('v'),
108
+ const ds = lo.struct([lo.u8('v'),
109
109
lo.u32('u32')]);
110
110
assert.equal(ds.offsetOf('u32'), 1);
111
- var b = new Buffer(5);
111
+ const b = new Buffer(5);
112
112
b.fill(0xbd);
113
113
assert.equal(ds.encode({v: 1, u32: 0x12345678}, b), 1 + 4);
114
114
assert.equal(Buffer('0178563412', 'hex').compare(b), 0);
@@ -131,12 +131,12 @@ four bytes depends on the first byte. The C definition:
131
131
132
132
The buffer-layout way:
133
133
134
- var t = lo.u8('t');
135
- var un = lo.union(t, lo.seq(lo.u8(), 4, 'u8'));
136
- var u32 = un.addVariant('w'.charCodeAt(0), lo.u32(), 'u32');
137
- var s16 = un.addVariant('h'.charCodeAt(0), lo.seq(lo.s16(), 2), 's16');
138
- var f32 = un.addVariant('f'.charCodeAt(0), lo.f32(), 'f32');
139
- var b = new Buffer(un.span);
134
+ const t = lo.u8('t');
135
+ const un = lo.union(t, lo.seq(lo.u8(), 4, 'u8'));
136
+ const u32 = un.addVariant('w'.charCodeAt(0), lo.u32(), 'u32');
137
+ const s16 = un.addVariant('h'.charCodeAt(0), lo.seq(lo.s16(), 2), 's16');
138
+ const f32 = un.addVariant('f'.charCodeAt(0), lo.f32(), 'f32');
139
+ const b = new Buffer(un.span);
140
140
assert.deepEqual(un.decode(Buffer('7778563412', 'hex')),
141
141
{u32: 0x12345678});
142
142
assert.deepEqual(un.decode(Buffer('660000bd41', 'hex')),
@@ -172,7 +172,7 @@ representing the union and the variants:
172
172
lo.bindConstructorLayout(Vf32,
173
173
Union.layout_.addVariant('f'.charCodeAt(0), lo.f32(), 'f32'));
174
174
175
- var v = Union.decode(Buffer('7778563412', 'hex'));
175
+ let v = Union.decode(Buffer('7778563412', 'hex'));
176
176
assert(v instanceof Vu32);
177
177
assert(v instanceof Union);
178
178
assert.equal(v.u32, 0x12345678);
@@ -182,7 +182,7 @@ representing the union and the variants:
182
182
assert.equal(v.t, 0xa5);
183
183
assert.deepEqual(v.u8, [0xa5, 0xa5, 0xa5, 0xa5]);
184
184
185
- var b = new Buffer(Union.layout_.span);
185
+ const b = new Buffer(Union.layout_.span);
186
186
v = new Vf32(23.625);
187
187
v.encode(b);
188
188
assert.equal(Buffer('660000bd41', 'hex').compare(b), 0);
@@ -205,8 +205,8 @@ The C definition:
205
205
206
206
The buffer-layout way:
207
207
208
- var ds = lo.bits(lo.u32());
209
- var b = new Buffer(4);
208
+ const ds = lo.bits(lo.u32());
209
+ const b = new Buffer(4);
210
210
ds.addField(3, 'b00l03');
211
211
ds.addBoolean('flg03');
212
212
ds.addField(24, 'b04l18');
@@ -227,10 +227,10 @@ The C definition:
227
227
228
228
The buffer-layout way:
229
229
230
- var ds = lo.nu64be();
231
- var b = Buffer('0102030405060708', 'hex');
232
- var v = 72623859790382856;
233
- var nv = v - 6;
230
+ const ds = lo.nu64be();
231
+ const b = Buffer('0102030405060708', 'hex');
232
+ const v = 72623859790382856;
233
+ const nv = v - 6;
234
234
assert.equal(v, nv);
235
235
assert.equal(ds.decode(b), nv);
236
236
@@ -248,10 +248,10 @@ The C definition:
248
248
249
249
The buffer-layout way:
250
250
251
- var ds = lo.cstr();
252
- var b = new Buffer(8);
251
+ const ds = lo.cstr();
252
+ const b = new Buffer(8);
253
253
assert.equal(ds.encode('hi!', b), 3 + 1);
254
- var slen = ds.getSpan(b);
254
+ const slen = ds.getSpan(b);
255
255
assert.equal(slen, 4);
256
256
assert.equal(Buffer('68692100', 'hex').compare(b.slice(0, slen)), 0);
257
257
assert.equal(ds.decode(b), 'hi!');
@@ -262,8 +262,8 @@ See [CString](http://pabigot.github.io/buffer-layout/module-Layout-CString.html)
262
262
263
263
The buffer-layout way:
264
264
265
- var ds = lo.blob(4);
266
- var b = Buffer('0102030405060708', 'hex');
265
+ const ds = lo.blob(4);
266
+ const b = Buffer('0102030405060708', 'hex');
267
267
assert.equal(Buffer('03040506', 'hex').compare(ds.decode(b, 2)), 0);
268
268
269
269
See [ Blob] ( http://pabigot.github.io/buffer-layout/module-Layout-Blob.html ) .
@@ -272,15 +272,15 @@ See [Blob](http://pabigot.github.io/buffer-layout/module-Layout-Blob.html).
272
272
273
273
The buffer-layout way:
274
274
275
- var pr = lo.seq(lo.cstr(), 2);
276
- var n = lo.u8('n');
277
- var vla = lo.seq(pr, lo.offset(n, -1), 'a');
278
- var st = lo.struct([n, vla], 'st');
279
- var b = new Buffer(32);
280
- var arr = [['k1', 'v1'], ['k2', 'v2'], ['k3', 'etc']];
275
+ const pr = lo.seq(lo.cstr(), 2);
276
+ const n = lo.u8('n');
277
+ const vla = lo.seq(pr, lo.offset(n, -1), 'a');
278
+ const st = lo.struct([n, vla], 'st');
279
+ const b = new Buffer(32);
280
+ const arr = [['k1', 'v1'], ['k2', 'v2'], ['k3', 'etc']];
281
281
b.fill(0);
282
282
assert.equal(st.encode({a: arr}, b), 1 + (2 * ((2 + 1) + (2 + 1)) + (2 + 1) + (3 + 1)));
283
- var span = st.getSpan(b);
283
+ const span = st.getSpan(b);
284
284
assert.equal(span, 20);
285
285
assert.equal(Buffer('036b31007631006b32007632006b330065746300', 'hex')
286
286
.compare(b.slice(0, span)), 0);
@@ -302,12 +302,12 @@ The C definition:
302
302
303
303
The buffer-layout way:
304
304
305
- var st = lo.struct([lo.u8('prop'),
305
+ const st = lo.struct([lo.u8('prop'),
306
306
lo.seq(lo.u16(),
307
307
lo.greedy(lo.u16().span),
308
308
'data')],
309
309
'ds');
310
- var b = Buffer('21010002030405', 'hex');
310
+ const b = Buffer('21010002030405', 'hex');
311
311
assert.deepEqual(st.decode(b), {prop: 33, data: [0x0001, 0x0302, 0x0504]});
312
312
b.fill(0xFF);
313
313
assert.equal(st.encode({prop: 9, data: [5,6]}, b), 1 + 2 * 2);
@@ -326,14 +326,14 @@ encoded union. This could be used to make something similar to
326
326
Here's the code that defines the union, the variants, and the
327
327
recognition of ` true ` and ` false ` values for ` b ` as distinct variants:
328
328
329
- var un = lo.union(lo.u8('t'));
330
- var u8 = un.addVariant('B'.charCodeAt(0), lo.u8(), 'u8');
331
- var s16 = un.addVariant('h'.charCodeAt(0), lo.s16(), 's16');
332
- var s48 = un.addVariant('Q'.charCodeAt(0), lo.s48(), 's48');
333
- var cstr = un.addVariant('s'.charCodeAt(0), lo.cstr(), 'str');
334
- var tr = un.addVariant('T'.charCodeAt(0), lo.const(true), 'b');
335
- var fa = un.addVariant('F'.charCodeAt(0), lo.const(false), 'b');
336
- var b = new Buffer(1 + 6);
329
+ const un = lo.union(lo.u8('t'));
330
+ const u8 = un.addVariant('B'.charCodeAt(0), lo.u8(), 'u8');
331
+ const s16 = un.addVariant('h'.charCodeAt(0), lo.s16(), 's16');
332
+ const s48 = un.addVariant('Q'.charCodeAt(0), lo.s48(), 's48');
333
+ const cstr = un.addVariant('s'.charCodeAt(0), lo.cstr(), 'str');
334
+ const tr = un.addVariant('T'.charCodeAt(0), lo.const(true), 'b');
335
+ const fa = un.addVariant('F'.charCodeAt(0), lo.const(false), 'b');
336
+ const b = new Buffer(1 + 6);
337
337
un.configGetSourceVariant(function(src) {
338
338
if (src.hasOwnProperty('b')) {
339
339
return src.b ? tr : fa;
@@ -357,7 +357,7 @@ decoding each of the alternatives:
357
357
assert.equal(un.decode(b).s16, -32000);
358
358
359
359
b.fill(0xff);
360
- var v48 = Math.pow(2, 47) - 1;
360
+ const v48 = Math.pow(2, 47) - 1;
361
361
assert.equal(un.encode({s48: v48}, b), 1 + 6);
362
362
assert.equal(un.getSpan(b), 7);
363
363
assert.equal(Buffer('51ffffffffff7f', 'hex').compare(b), 0);
0 commit comments