Skip to content

Commit 2ac47f8

Browse files
committed
buffer: refactor redeclared variables
A handful of variable declarations in `lib/buffer.js` redeclare the same variable in the same scope. This change removes each redeclaration by switching to `const`, switching to `let`, or explicitly hoisting the `var` declaration. PR-URL: #4886 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Roman Klauke <[email protected]>
1 parent 795eeb1 commit 2ac47f8

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

lib/buffer.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function fromString(string, encoding) {
125125

126126
function fromObject(obj) {
127127
if (obj instanceof Buffer) {
128-
var b = allocate(obj.length);
128+
const b = allocate(obj.length);
129129

130130
if (b.length === 0)
131131
return b;
@@ -135,9 +135,9 @@ function fromObject(obj) {
135135
}
136136

137137
if (Array.isArray(obj)) {
138-
var length = obj.length;
139-
var b = allocate(length);
140-
for (var i = 0; i < length; i++)
138+
const length = obj.length;
139+
const b = allocate(length);
140+
for (let i = 0; i < length; i++)
141141
b[i] = obj[i] & 255;
142142
return b;
143143
}
@@ -151,22 +151,22 @@ function fromObject(obj) {
151151
}
152152

153153
if (obj.buffer instanceof ArrayBuffer || obj.length) {
154-
var length;
154+
let length;
155155
if (typeof obj.length !== 'number' || obj.length !== obj.length)
156156
length = 0;
157157
else
158158
length = obj.length;
159-
var b = allocate(length);
160-
for (var i = 0; i < length; i++) {
159+
const b = allocate(length);
160+
for (let i = 0; i < length; i++) {
161161
b[i] = obj[i] & 255;
162162
}
163163
return b;
164164
}
165165

166166
if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
167167
var array = obj.data;
168-
var b = allocate(array.length);
169-
for (var i = 0; i < array.length; i++)
168+
const b = allocate(array.length);
169+
for (let i = 0; i < array.length; i++)
170170
b[i] = array[i] & 255;
171171
return b;
172172
}
@@ -231,15 +231,15 @@ Buffer.concat = function(list, length) {
231231

232232
if (length === undefined) {
233233
length = 0;
234-
for (var i = 0; i < list.length; i++)
234+
for (let i = 0; i < list.length; i++)
235235
length += list[i].length;
236236
} else {
237237
length = length >>> 0;
238238
}
239239

240240
var buffer = new Buffer(length);
241241
var pos = 0;
242-
for (var i = 0; i < list.length; i++) {
242+
for (let i = 0; i < list.length; i++) {
243243
var buf = list[i];
244244
buf.copy(buffer, pos);
245245
pos += buf.length;
@@ -401,10 +401,11 @@ function slowToString(encoding, start, end) {
401401

402402

403403
Buffer.prototype.toString = function() {
404+
let result;
404405
if (arguments.length === 0) {
405-
var result = this.utf8Slice(0, this.length);
406+
result = this.utf8Slice(0, this.length);
406407
} else {
407-
var result = slowToString.apply(this, arguments);
408+
result = slowToString.apply(this, arguments);
408409
}
409410
if (result === undefined)
410411
throw new Error('"toString()" failed');

0 commit comments

Comments
 (0)