Skip to content

Commit fae7c9d

Browse files
TrottMyles Borins
authored and
Myles Borins
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 c908ff3 commit fae7c9d

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
}
@@ -227,15 +227,15 @@ Buffer.concat = function(list, length) {
227227

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

236236
var buffer = new Buffer(length);
237237
var pos = 0;
238-
for (var i = 0; i < list.length; i++) {
238+
for (let i = 0; i < list.length; i++) {
239239
var buf = list[i];
240240
buf.copy(buffer, pos);
241241
pos += buf.length;
@@ -379,10 +379,11 @@ function slowToString(encoding, start, end) {
379379

380380

381381
Buffer.prototype.toString = function() {
382+
let result;
382383
if (arguments.length === 0) {
383-
var result = this.utf8Slice(0, this.length);
384+
result = this.utf8Slice(0, this.length);
384385
} else {
385-
var result = slowToString.apply(this, arguments);
386+
result = slowToString.apply(this, arguments);
386387
}
387388
if (result === undefined)
388389
throw new Error('toString failed');

0 commit comments

Comments
 (0)