Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 9663c16

Browse files
committed
Layout: ES6 conversion: let/const, object literals
1 parent f0475ef commit 9663c16

File tree

5 files changed

+145
-145
lines changed

5 files changed

+145
-145
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module.exports = {
1010
rules: {
1111
'guard-for-in': 'off',
1212
'new-cap': 'off',
13-
'no-var': 'off',
1413
'prefer-rest-params': 'off',
1514
'prefer-spread': 'off',
1615
__temporary: 'off',

README.md

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ buffer-layout`.
3737
All examples are from the `test/examples.js` unit test and assume the
3838
following context:
3939

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');
4343

4444
The examples give only a taste of what can be done. Structures, unions,
4545
and sequences can nest; [union
@@ -57,8 +57,8 @@ The C definition:
5757

5858
The buffer-layout way:
5959

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);
6262
assert.equal(ds.encode([1, -1, 3, -3], b), 4 * 2);
6363
assert.equal(Buffer('0100ffff0300fdff', 'hex').compare(b), 0);
6464
assert.deepEqual(ds.decode(b), [1, -1, 3, -3]);
@@ -77,11 +77,11 @@ The C definition:
7777

7878
The buffer-layout way:
7979

80-
var ds = lo.struct([lo.u8('v'),
80+
const ds = lo.struct([lo.u8('v'),
8181
lo.seq(lo.u8(), 3), // alignment padding
8282
lo.u32('u32')]);
8383
assert.equal(ds.offsetOf('u32'), 4);
84-
var b = new Buffer(8);
84+
const b = new Buffer(8);
8585
b.fill(0xbd);
8686
assert.equal(ds.encode({v: 1, u32: 0x12345678}, b), 1 + 3 + 4);
8787
assert.equal(Buffer('01bdbdbd78563412', 'hex').compare(b), 0);
@@ -105,10 +105,10 @@ The C definition:
105105

106106
The buffer-layout way:
107107

108-
var ds = lo.struct([lo.u8('v'),
108+
const ds = lo.struct([lo.u8('v'),
109109
lo.u32('u32')]);
110110
assert.equal(ds.offsetOf('u32'), 1);
111-
var b = new Buffer(5);
111+
const b = new Buffer(5);
112112
b.fill(0xbd);
113113
assert.equal(ds.encode({v: 1, u32: 0x12345678}, b), 1 + 4);
114114
assert.equal(Buffer('0178563412', 'hex').compare(b), 0);
@@ -131,12 +131,12 @@ four bytes depends on the first byte. The C definition:
131131

132132
The buffer-layout way:
133133

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);
140140
assert.deepEqual(un.decode(Buffer('7778563412', 'hex')),
141141
{u32: 0x12345678});
142142
assert.deepEqual(un.decode(Buffer('660000bd41', 'hex')),
@@ -172,7 +172,7 @@ representing the union and the variants:
172172
lo.bindConstructorLayout(Vf32,
173173
Union.layout_.addVariant('f'.charCodeAt(0), lo.f32(), 'f32'));
174174

175-
var v = Union.decode(Buffer('7778563412', 'hex'));
175+
let v = Union.decode(Buffer('7778563412', 'hex'));
176176
assert(v instanceof Vu32);
177177
assert(v instanceof Union);
178178
assert.equal(v.u32, 0x12345678);
@@ -182,7 +182,7 @@ representing the union and the variants:
182182
assert.equal(v.t, 0xa5);
183183
assert.deepEqual(v.u8, [0xa5, 0xa5, 0xa5, 0xa5]);
184184

185-
var b = new Buffer(Union.layout_.span);
185+
const b = new Buffer(Union.layout_.span);
186186
v = new Vf32(23.625);
187187
v.encode(b);
188188
assert.equal(Buffer('660000bd41', 'hex').compare(b), 0);
@@ -205,8 +205,8 @@ The C definition:
205205

206206
The buffer-layout way:
207207

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);
210210
ds.addField(3, 'b00l03');
211211
ds.addBoolean('flg03');
212212
ds.addField(24, 'b04l18');
@@ -227,10 +227,10 @@ The C definition:
227227

228228
The buffer-layout way:
229229

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;
234234
assert.equal(v, nv);
235235
assert.equal(ds.decode(b), nv);
236236

@@ -248,10 +248,10 @@ The C definition:
248248

249249
The buffer-layout way:
250250

251-
var ds = lo.cstr();
252-
var b = new Buffer(8);
251+
const ds = lo.cstr();
252+
const b = new Buffer(8);
253253
assert.equal(ds.encode('hi!', b), 3 + 1);
254-
var slen = ds.getSpan(b);
254+
const slen = ds.getSpan(b);
255255
assert.equal(slen, 4);
256256
assert.equal(Buffer('68692100', 'hex').compare(b.slice(0, slen)), 0);
257257
assert.equal(ds.decode(b), 'hi!');
@@ -262,8 +262,8 @@ See [CString](http://pabigot.github.io/buffer-layout/module-Layout-CString.html)
262262

263263
The buffer-layout way:
264264

265-
var ds = lo.blob(4);
266-
var b = Buffer('0102030405060708', 'hex');
265+
const ds = lo.blob(4);
266+
const b = Buffer('0102030405060708', 'hex');
267267
assert.equal(Buffer('03040506', 'hex').compare(ds.decode(b, 2)), 0);
268268

269269
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).
272272

273273
The buffer-layout way:
274274

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']];
281281
b.fill(0);
282282
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);
284284
assert.equal(span, 20);
285285
assert.equal(Buffer('036b31007631006b32007632006b330065746300', 'hex')
286286
.compare(b.slice(0, span)), 0);
@@ -302,12 +302,12 @@ The C definition:
302302

303303
The buffer-layout way:
304304

305-
var st = lo.struct([lo.u8('prop'),
305+
const st = lo.struct([lo.u8('prop'),
306306
lo.seq(lo.u16(),
307307
lo.greedy(lo.u16().span),
308308
'data')],
309309
'ds');
310-
var b = Buffer('21010002030405', 'hex');
310+
const b = Buffer('21010002030405', 'hex');
311311
assert.deepEqual(st.decode(b), {prop: 33, data: [0x0001, 0x0302, 0x0504]});
312312
b.fill(0xFF);
313313
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
326326
Here's the code that defines the union, the variants, and the
327327
recognition of `true` and `false` values for `b` as distinct variants:
328328

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);
337337
un.configGetSourceVariant(function(src) {
338338
if (src.hasOwnProperty('b')) {
339339
return src.b ? tr : fa;
@@ -357,7 +357,7 @@ decoding each of the alternatives:
357357
assert.equal(un.decode(b).s16, -32000);
358358

359359
b.fill(0xff);
360-
var v48 = Math.pow(2, 47) - 1;
360+
const v48 = Math.pow(2, 47) - 1;
361361
assert.equal(un.encode({s48: v48}, b), 1 + 6);
362362
assert.equal(un.getSpan(b), 7);
363363
assert.equal(Buffer('51ffffffffff7f', 'hex').compare(b), 0);

0 commit comments

Comments
 (0)