Skip to content

Commit 540e358

Browse files
committed
style: use 'standard', minor formatting & syntax changes
1 parent c48a72e commit 540e358

8 files changed

+431
-313
lines changed

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ node_js:
44
- '6'
55
- '8'
66
- '10'
7+
- '12'
8+
- master
79
branches:
810
only:
911
- master

BufferList.js

+126-84
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
'use strict'
22

3-
var symbol = Symbol.for('BufferList')
3+
const symbol = Symbol.for('BufferList')
44

55
function BufferList (buf) {
6-
if (!(this instanceof BufferList))
6+
if (!(this instanceof BufferList)) {
77
return new BufferList(buf)
8+
}
89

910
BufferList._init.call(this, buf)
1011
}
@@ -14,80 +15,100 @@ BufferList._init = function _init (buf) {
1415

1516
this._bufs = []
1617
this.length = 0
17-
if (buf) this.append(buf)
18+
19+
if (buf) {
20+
this.append(buf)
21+
}
1822
}
1923

2024
BufferList.prototype._new = function _new (buf) {
2125
return new BufferList(buf)
2226
}
2327

2428
BufferList.prototype._offset = function _offset (offset) {
25-
var tot = 0, i = 0, _t
26-
if (offset === 0) return [ 0, 0 ]
27-
for (; i < this._bufs.length; i++) {
28-
_t = tot + this._bufs[i].length
29-
if (offset < _t || i == this._bufs.length - 1) {
30-
return [ i, offset - tot ]
29+
if (offset === 0) {
30+
return [0, 0]
31+
}
32+
33+
let tot = 0
34+
35+
for (let i = 0; i < this._bufs.length; i++) {
36+
const _t = tot + this._bufs[i].length
37+
if (offset < _t || i === this._bufs.length - 1) {
38+
return [i, offset - tot]
3139
}
3240
tot = _t
3341
}
3442
}
3543

3644
BufferList.prototype._reverseOffset = function (blOffset) {
37-
var bufferId = blOffset[0]
38-
var offset = blOffset[1]
39-
for (var i = 0; i < bufferId; i++) {
45+
const bufferId = blOffset[0]
46+
let offset = blOffset[1]
47+
48+
for (let i = 0; i < bufferId; i++) {
4049
offset += this._bufs[i].length
4150
}
51+
4252
return offset
4353
}
4454

4555
BufferList.prototype.get = function get (index) {
4656
if (index > this.length || index < 0) {
4757
return undefined
4858
}
49-
var offset = this._offset(index)
59+
60+
const offset = this._offset(index)
61+
5062
return this._bufs[offset[0]][offset[1]]
5163
}
5264

5365
BufferList.prototype.slice = function slice (start, end) {
54-
if (typeof start == 'number' && start < 0)
66+
if (typeof start === 'number' && start < 0) {
5567
start += this.length
56-
if (typeof end == 'number' && end < 0)
68+
}
69+
70+
if (typeof end === 'number' && end < 0) {
5771
end += this.length
72+
}
73+
5874
return this.copy(null, 0, start, end)
5975
}
6076

61-
6277
BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
63-
if (typeof srcStart != 'number' || srcStart < 0)
78+
if (typeof srcStart !== 'number' || srcStart < 0) {
6479
srcStart = 0
65-
if (typeof srcEnd != 'number' || srcEnd > this.length)
80+
}
81+
82+
if (typeof srcEnd !== 'number' || srcEnd > this.length) {
6683
srcEnd = this.length
67-
if (srcStart >= this.length)
84+
}
85+
86+
if (srcStart >= this.length) {
6887
return dst || Buffer.alloc(0)
69-
if (srcEnd <= 0)
88+
}
89+
90+
if (srcEnd <= 0) {
7091
return dst || Buffer.alloc(0)
92+
}
7193

72-
var copy = !!dst
73-
, off = this._offset(srcStart)
74-
, len = srcEnd - srcStart
75-
, bytes = len
76-
, bufoff = (copy && dstStart) || 0
77-
, start = off[1]
78-
, l
79-
, i
94+
const copy = !!dst
95+
const off = this._offset(srcStart)
96+
const len = srcEnd - srcStart
97+
let bytes = len
98+
let bufoff = (copy && dstStart) || 0
99+
let start = off[1]
80100

81101
// copy/slice everything
82-
if (srcStart === 0 && srcEnd == this.length) {
83-
if (!copy) { // slice, but full concat if multiple buffers
102+
if (srcStart === 0 && srcEnd === this.length) {
103+
if (!copy) {
104+
// slice, but full concat if multiple buffers
84105
return this._bufs.length === 1
85106
? this._bufs[0]
86107
: Buffer.concat(this._bufs, this.length)
87108
}
88109

89110
// copy, need to copy individual buffers
90-
for (i = 0; i < this._bufs.length; i++) {
111+
for (let i = 0; i < this._bufs.length; i++) {
91112
this._bufs[i].copy(dst, bufoff)
92113
bufoff += this._bufs[i].length
93114
}
@@ -102,11 +123,13 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
102123
: this._bufs[off[0]].slice(start, start + bytes)
103124
}
104125

105-
if (!copy) // a slice, we need something to copy in to
126+
if (!copy) {
127+
// a slice, we need something to copy in to
106128
dst = Buffer.allocUnsafe(len)
129+
}
107130

108-
for (i = off[0]; i < this._bufs.length; i++) {
109-
l = this._bufs[i].length - start
131+
for (let i = off[0]; i < this._bufs.length; i++) {
132+
const l = this._bufs[i].length - start
110133

111134
if (bytes > l) {
112135
this._bufs[i].copy(dst, bufoff, start)
@@ -118,8 +141,9 @@ BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
118141
bufoff += l
119142
bytes -= l
120143

121-
if (start)
144+
if (start) {
122145
start = 0
146+
}
123147
}
124148

125149
return dst
@@ -129,25 +153,31 @@ BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
129153
start = start || 0
130154
end = typeof end !== 'number' ? this.length : end
131155

132-
if (start < 0)
156+
if (start < 0) {
133157
start += this.length
134-
if (end < 0)
158+
}
159+
160+
if (end < 0) {
135161
end += this.length
162+
}
136163

137164
if (start === end) {
138165
return this._new()
139166
}
140-
var startOffset = this._offset(start)
141-
, endOffset = this._offset(end)
142-
, buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
143167

144-
if (endOffset[1] == 0)
168+
const startOffset = this._offset(start)
169+
const endOffset = this._offset(end)
170+
const buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
171+
172+
if (endOffset[1] === 0) {
145173
buffers.pop()
146-
else
147-
buffers[buffers.length-1] = buffers[buffers.length-1].slice(0, endOffset[1])
174+
} else {
175+
buffers[buffers.length - 1] = buffers[buffers.length - 1].slice(0, endOffset[1])
176+
}
148177

149-
if (startOffset[1] != 0)
178+
if (startOffset[1] !== 0) {
150179
buffers[0] = buffers[0].slice(startOffset[1])
180+
}
151181

152182
return this._new(buffers)
153183
}
@@ -168,23 +198,21 @@ BufferList.prototype.consume = function consume (bytes) {
168198
break
169199
}
170200
}
201+
171202
return this
172203
}
173204

174-
175205
BufferList.prototype.duplicate = function duplicate () {
176-
var i = 0
177-
, copy = this._new()
206+
const copy = this._new()
178207

179-
for (; i < this._bufs.length; i++)
208+
for (let i = 0; i < this._bufs.length; i++) {
180209
copy.append(this._bufs[i])
210+
}
181211

182212
return copy
183213
}
184214

185215
BufferList.prototype.append = function append (buf) {
186-
var i = 0
187-
188216
if (buf == null) {
189217
return this
190218
}
@@ -193,17 +221,20 @@ BufferList.prototype.append = function append (buf) {
193221
// append a view of the underlying ArrayBuffer
194222
this._appendBuffer(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength))
195223
} else if (Array.isArray(buf)) {
196-
for (; i < buf.length; i++)
224+
for (let i = 0; i < buf.length; i++) {
197225
this.append(buf[i])
226+
}
198227
} else if (this._isBufferList(buf)) {
199228
// unwrap argument into individual BufferLists
200-
for (; i < buf._bufs.length; i++)
229+
for (let i = 0; i < buf._bufs.length; i++) {
201230
this.append(buf._bufs[i])
231+
}
202232
} else {
203233
// coerce number arguments to strings, since Buffer(number) does
204234
// uninitialized memory allocation
205-
if (typeof buf == 'number')
235+
if (typeof buf === 'number') {
206236
buf = buf.toString()
237+
}
207238

208239
this._appendBuffer(Buffer.from(buf))
209240
}
@@ -221,6 +252,7 @@ BufferList.prototype.indexOf = function (search, offset, encoding) {
221252
encoding = offset
222253
offset = undefined
223254
}
255+
224256
if (typeof search === 'function' || Array.isArray(search)) {
225257
throw new TypeError('The "value" argument must be one of type string, Buffer, BufferList, or Uint8Array.')
226258
} else if (typeof search === 'number') {
@@ -236,6 +268,7 @@ BufferList.prototype.indexOf = function (search, offset, encoding) {
236268
}
237269

238270
offset = Number(offset || 0)
271+
239272
if (isNaN(offset)) {
240273
offset = 0
241274
}
@@ -252,39 +285,48 @@ BufferList.prototype.indexOf = function (search, offset, encoding) {
252285
return offset > this.length ? this.length : offset
253286
}
254287

255-
var blOffset = this._offset(offset)
256-
var blIndex = blOffset[0] // index of which internal buffer we're working on
257-
var buffOffset = blOffset[1] // offset of the internal buffer we're working on
288+
const blOffset = this._offset(offset)
289+
let blIndex = blOffset[0] // index of which internal buffer we're working on
290+
let buffOffset = blOffset[1] // offset of the internal buffer we're working on
258291

259292
// scan over each buffer
260-
for (blIndex; blIndex < this._bufs.length; blIndex++) {
261-
var buff = this._bufs[blIndex]
293+
for (; blIndex < this._bufs.length; blIndex++) {
294+
const buff = this._bufs[blIndex]
295+
262296
while (buffOffset < buff.length) {
263-
var availableWindow = buff.length - buffOffset
297+
const availableWindow = buff.length - buffOffset
298+
264299
if (availableWindow >= search.length) {
265-
var nativeSearchResult = buff.indexOf(search, buffOffset)
300+
const nativeSearchResult = buff.indexOf(search, buffOffset)
301+
266302
if (nativeSearchResult !== -1) {
267303
return this._reverseOffset([blIndex, nativeSearchResult])
268304
}
305+
269306
buffOffset = buff.length - search.length + 1 // end of native search window
270307
} else {
271-
var revOffset = this._reverseOffset([blIndex, buffOffset])
308+
const revOffset = this._reverseOffset([blIndex, buffOffset])
309+
272310
if (this._match(revOffset, search)) {
273311
return revOffset
274312
}
313+
275314
buffOffset++
276315
}
277316
}
317+
278318
buffOffset = 0
279319
}
320+
280321
return -1
281322
}
282323

283324
BufferList.prototype._match = function (offset, search) {
284325
if (this.length - offset < search.length) {
285326
return false
286327
}
287-
for (var searchOffset = 0; searchOffset < search.length; searchOffset++) {
328+
329+
for (let searchOffset = 0; searchOffset < search.length; searchOffset++) {
288330
if (this.get(offset + searchOffset) !== search[searchOffset]) {
289331
return false
290332
}
@@ -293,28 +335,28 @@ BufferList.prototype._match = function (offset, search) {
293335
}
294336

295337
;(function () {
296-
var methods = {
297-
'readDoubleBE' : 8
298-
, 'readDoubleLE' : 8
299-
, 'readFloatBE' : 4
300-
, 'readFloatLE' : 4
301-
, 'readInt32BE' : 4
302-
, 'readInt32LE' : 4
303-
, 'readUInt32BE' : 4
304-
, 'readUInt32LE' : 4
305-
, 'readInt16BE' : 2
306-
, 'readInt16LE' : 2
307-
, 'readUInt16BE' : 2
308-
, 'readUInt16LE' : 2
309-
, 'readInt8' : 1
310-
, 'readUInt8' : 1
311-
, 'readIntBE' : null
312-
, 'readIntLE' : null
313-
, 'readUIntBE' : null
314-
, 'readUIntLE' : null
315-
}
316-
317-
for (var m in methods) {
338+
const methods = {
339+
readDoubleBE: 8,
340+
readDoubleLE: 8,
341+
readFloatBE: 4,
342+
readFloatLE: 4,
343+
readInt32BE: 4,
344+
readInt32LE: 4,
345+
readUInt32BE: 4,
346+
readUInt32LE: 4,
347+
readInt16BE: 2,
348+
readInt16LE: 2,
349+
readUInt16BE: 2,
350+
readUInt16LE: 2,
351+
readInt8: 1,
352+
readUInt8: 1,
353+
readIntBE: null,
354+
readIntLE: null,
355+
readUIntBE: null,
356+
readUIntLE: null
357+
}
358+
359+
for (const m in methods) {
318360
(function (m) {
319361
if (methods[m] === null) {
320362
BufferList.prototype[m] = function (offset, byteLength) {

0 commit comments

Comments
 (0)