From: Feross Aboukhadijeh Date: Tue, 27 Sep 2016 02:11:31 +0000 (-0700) Subject: Remove unneeded `that` parameter X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=8762e1f4d6f1748d2617562e91c6ddd098a92cf6;p=buffer.git Remove unneeded `that` parameter Thanks for pointing this out, @jdalton. --- diff --git a/index.js b/index.js index 75e7924..4957287 100644 --- a/index.js +++ b/index.js @@ -51,14 +51,14 @@ function typedArraySupport () { } } -function createBuffer (that, length) { +function createBuffer (length) { if (K_MAX_LENGTH < length) { throw new RangeError('Invalid typed array length') } // Return an augmented `Uint8Array` instance - that = new Uint8Array(length) - that.__proto__ = Buffer.prototype - return that + var buf = new Uint8Array(length) + buf.__proto__ = Buffer.prototype + return buf } /** @@ -79,9 +79,9 @@ function Buffer (arg, encodingOrOffset, length) { 'If encoding is specified then the first argument must be a string' ) } - return allocUnsafe(this, arg) + return allocUnsafe(arg) } - return from(this, arg, encodingOrOffset, length) + return from(arg, encodingOrOffset, length) } Buffer.prototype.__proto__ = Uint8Array.prototype @@ -100,20 +100,20 @@ if (typeof Symbol !== 'undefined' && Symbol.species && Buffer.poolSize = 8192 // not used by this implementation -function from (that, value, encodingOrOffset, length) { +function from (value, encodingOrOffset, length) { if (typeof value === 'number') { throw new TypeError('"value" argument must not be a number') } if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) { - return fromArrayBuffer(that, value, encodingOrOffset, length) + return fromArrayBuffer(value, encodingOrOffset, length) } if (typeof value === 'string') { - return fromString(that, value, encodingOrOffset) + return fromString(value, encodingOrOffset) } - return fromObject(that, value) + return fromObject(value) } /** @@ -125,7 +125,7 @@ function from (that, value, encodingOrOffset, length) { * Buffer.from(arrayBuffer[, byteOffset[, length]]) **/ Buffer.from = function (value, encodingOrOffset, length) { - return from(null, value, encodingOrOffset, length) + return from(value, encodingOrOffset, length) } function assertSize (size) { @@ -136,20 +136,20 @@ function assertSize (size) { } } -function alloc (that, size, fill, encoding) { +function alloc (size, fill, encoding) { assertSize(size) if (size <= 0) { - return createBuffer(that, size) + return createBuffer(size) } if (fill !== undefined) { // Only pay attention to encoding if it's a string. This // prevents accidentally sending in a number that would // be interpretted as a start offset. return typeof encoding === 'string' - ? createBuffer(that, size).fill(fill, encoding) - : createBuffer(that, size).fill(fill) + ? createBuffer(size).fill(fill, encoding) + : createBuffer(size).fill(fill) } - return createBuffer(that, size) + return createBuffer(size) } /** @@ -157,28 +157,28 @@ function alloc (that, size, fill, encoding) { * alloc(size[, fill[, encoding]]) **/ Buffer.alloc = function (size, fill, encoding) { - return alloc(null, size, fill, encoding) + return alloc(size, fill, encoding) } -function allocUnsafe (that, size) { +function allocUnsafe (size) { assertSize(size) - return createBuffer(that, size < 0 ? 0 : checked(size) | 0) + return createBuffer(size < 0 ? 0 : checked(size) | 0) } /** * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. * */ Buffer.allocUnsafe = function (size) { - return allocUnsafe(null, size) + return allocUnsafe(size) } /** * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. */ Buffer.allocUnsafeSlow = function (size) { - return allocUnsafe(null, size) + return allocUnsafe(size) } -function fromString (that, string, encoding) { +function fromString (string, encoding) { if (typeof encoding !== 'string' || encoding === '') { encoding = 'utf8' } @@ -188,30 +188,30 @@ function fromString (that, string, encoding) { } var length = byteLength(string, encoding) | 0 - that = createBuffer(that, length) + var buf = createBuffer(length) - var actual = that.write(string, encoding) + var actual = buf.write(string, encoding) if (actual !== length) { // Writing a hex string, for example, that contains invalid characters will // cause everything after the first invalid character to be ignored. (e.g. // 'abxxcd' will be treated as 'ab') - that = that.slice(0, actual) + buf = buf.slice(0, actual) } - return that + return buf } -function fromArrayLike (that, array) { +function fromArrayLike (array) { var length = array.length < 0 ? 0 : checked(array.length) | 0 - that = createBuffer(that, length) + var buf = createBuffer(length) for (var i = 0; i < length; i += 1) { - that[i] = array[i] & 255 + buf[i] = array[i] & 255 } - return that + return buf } -function fromArrayBuffer (that, array, byteOffset, length) { +function fromArrayBuffer (array, byteOffset, length) { array.byteLength // this throws if `array` is not a valid ArrayBuffer if (byteOffset < 0 || array.byteLength < byteOffset) { @@ -222,44 +222,44 @@ function fromArrayBuffer (that, array, byteOffset, length) { throw new RangeError('\'length\' is out of bounds') } + var buf if (byteOffset === undefined && length === undefined) { - array = new Uint8Array(array) + buf = new Uint8Array(array) } else if (length === undefined) { - array = new Uint8Array(array, byteOffset) + buf = new Uint8Array(array, byteOffset) } else { - array = new Uint8Array(array, byteOffset, length) + buf = new Uint8Array(array, byteOffset, length) } - // Return an augmented `Uint8Array` instance, for best performance - that = array - that.__proto__ = Buffer.prototype - return that + // Return an augmented `Uint8Array` instance + buf.__proto__ = Buffer.prototype + return buf } -function fromObject (that, obj) { +function fromObject (obj) { if (Buffer.isBuffer(obj)) { var len = checked(obj.length) | 0 - that = createBuffer(that, len) + var buf = createBuffer(len) - if (that.length === 0) { - return that + if (buf.length === 0) { + return buf } - obj.copy(that, 0, 0, len) - return that + obj.copy(buf, 0, 0, len) + return buf } if (obj) { if ((typeof ArrayBuffer !== 'undefined' && obj.buffer instanceof ArrayBuffer) || 'length' in obj) { if (typeof obj.length !== 'number' || isnan(obj.length)) { - return createBuffer(that, 0) + return createBuffer(0) } - return fromArrayLike(that, obj) + return fromArrayLike(obj) } if (obj.type === 'Buffer' && Array.isArray(obj.data)) { - return fromArrayLike(that, obj.data) + return fromArrayLike(obj.data) } } @@ -1030,6 +1030,7 @@ Buffer.prototype.slice = function slice (start, end) { if (end < start) end = start var newBuf = this.subarray(start, end) + // Return an augmented `Uint8Array` instance newBuf.__proto__ = Buffer.prototype return newBuf }