Skip to content

Commit bafc86f

Browse files
Trottrvagg
authored andcommitted
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 89d1149 commit bafc86f

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
@@ -120,7 +120,7 @@ function fromString(string, encoding) {
120120

121121
function fromObject(obj) {
122122
if (obj instanceof Buffer) {
123-
var b = allocate(obj.length);
123+
const b = allocate(obj.length);
124124

125125
if (b.length === 0)
126126
return b;
@@ -130,9 +130,9 @@ function fromObject(obj) {
130130
}
131131

132132
if (Array.isArray(obj)) {
133-
var length = obj.length;
134-
var b = allocate(length);
135-
for (var i = 0; i < length; i++)
133+
const length = obj.length;
134+
const b = allocate(length);
135+
for (let i = 0; i < length; i++)
136136
b[i] = obj[i] & 255;
137137
return b;
138138
}
@@ -146,22 +146,22 @@ function fromObject(obj) {
146146
}
147147

148148
if (obj.buffer instanceof ArrayBuffer || obj.length) {
149-
var length;
149+
let length;
150150
if (typeof obj.length !== 'number' || obj.length !== obj.length)
151151
length = 0;
152152
else
153153
length = obj.length;
154-
var b = allocate(length);
155-
for (var i = 0; i < length; i++) {
154+
const b = allocate(length);
155+
for (let i = 0; i < length; i++) {
156156
b[i] = obj[i] & 255;
157157
}
158158
return b;
159159
}
160160

161161
if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
162162
var array = obj.data;
163-
var b = allocate(array.length);
164-
for (var i = 0; i < array.length; i++)
163+
const b = allocate(array.length);
164+
for (let i = 0; i < array.length; i++)
165165
b[i] = array[i] & 255;
166166
return b;
167167
}
@@ -226,15 +226,15 @@ Buffer.concat = function(list, length) {
226226

227227
if (length === undefined) {
228228
length = 0;
229-
for (var i = 0; i < list.length; i++)
229+
for (let i = 0; i < list.length; i++)
230230
length += list[i].length;
231231
} else {
232232
length = length >>> 0;
233233
}
234234

235235
var buffer = new Buffer(length);
236236
var pos = 0;
237-
for (var i = 0; i < list.length; i++) {
237+
for (let i = 0; i < list.length; i++) {
238238
var buf = list[i];
239239
buf.copy(buffer, pos);
240240
pos += buf.length;
@@ -396,10 +396,11 @@ function slowToString(encoding, start, end) {
396396

397397

398398
Buffer.prototype.toString = function() {
399+
let result;
399400
if (arguments.length === 0) {
400-
var result = this.utf8Slice(0, this.length);
401+
result = this.utf8Slice(0, this.length);
401402
} else {
402-
var result = slowToString.apply(this, arguments);
403+
result = slowToString.apply(this, arguments);
403404
}
404405
if (result === undefined)
405406
throw new Error('toString failed');

0 commit comments

Comments
 (0)