buffer: refactor redeclared variables
authorRich Trott <rtrott@gmail.com>
Tue, 26 Jan 2016 21:17:31 +0000 (13:17 -0800)
committerMyles Borins <mborins@us.ibm.com>
Wed, 2 Mar 2016 22:01:11 +0000 (14:01 -0800)
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: https://github.com/nodejs/node/pull/4886
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Klauke <romaaan.git@gmail.com>
lib/buffer.js

index a830f2a..cd4144d 100644 (file)
@@ -120,7 +120,7 @@ function fromString(string, encoding) {
 
 function fromObject(obj) {
   if (obj instanceof Buffer) {
-    var b = allocate(obj.length);
+    const b = allocate(obj.length);
 
     if (b.length === 0)
       return b;
@@ -130,9 +130,9 @@ function fromObject(obj) {
   }
 
   if (Array.isArray(obj)) {
-    var length = obj.length;
-    var b = allocate(length);
-    for (var i = 0; i < length; i++)
+    const length = obj.length;
+    const b = allocate(length);
+    for (let i = 0; i < length; i++)
       b[i] = obj[i] & 255;
     return b;
   }
@@ -146,13 +146,13 @@ function fromObject(obj) {
   }
 
   if (obj.buffer instanceof ArrayBuffer || obj.length) {
-    var length;
+    let length;
     if (typeof obj.length !== 'number' || obj.length !== obj.length)
       length = 0;
     else
       length = obj.length;
-    var b = allocate(length);
-    for (var i = 0; i < length; i++) {
+    const b = allocate(length);
+    for (let i = 0; i < length; i++) {
       b[i] = obj[i] & 255;
     }
     return b;
@@ -160,8 +160,8 @@ function fromObject(obj) {
 
   if (obj.type === 'Buffer' && Array.isArray(obj.data)) {
     var array = obj.data;
-    var b = allocate(array.length);
-    for (var i = 0; i < array.length; i++)
+    const b = allocate(array.length);
+    for (let i = 0; i < array.length; i++)
       b[i] = array[i] & 255;
     return b;
   }
@@ -227,7 +227,7 @@ Buffer.concat = function(list, length) {
 
   if (length === undefined) {
     length = 0;
-    for (var i = 0; i < list.length; i++)
+    for (let i = 0; i < list.length; i++)
       length += list[i].length;
   } else {
     length = length >>> 0;
@@ -235,7 +235,7 @@ Buffer.concat = function(list, length) {
 
   var buffer = new Buffer(length);
   var pos = 0;
-  for (var i = 0; i < list.length; i++) {
+  for (let i = 0; i < list.length; i++) {
     var buf = list[i];
     buf.copy(buffer, pos);
     pos += buf.length;
@@ -379,10 +379,11 @@ function slowToString(encoding, start, end) {
 
 
 Buffer.prototype.toString = function() {
+  let result;
   if (arguments.length === 0) {
-    var result = this.utf8Slice(0, this.length);
+    result = this.utf8Slice(0, this.length);
   } else {
-    var result = slowToString.apply(this, arguments);
+    result = slowToString.apply(this, arguments);
   }
   if (result === undefined)
     throw new Error('toString failed');