From: Feross Aboukhadijeh Date: Thu, 29 Oct 2020 05:04:55 +0000 (-1000) Subject: standard X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=9605e31ad8fe59d9e495971c611ee92c81012b8a;p=buffer.git standard --- diff --git a/bin/download-node-tests.js b/bin/download-node-tests.js index c05a98e..e0d4fd9 100755 --- a/bin/download-node-tests.js +++ b/bin/download-node-tests.js @@ -1,22 +1,22 @@ #!/usr/bin/env node -var concat = require('concat-stream') -var cp = require('child_process') -var fs = require('fs') -var hyperquest = require('hyperquest') -var path = require('path') -var split = require('split') -var through = require('through2') - -var url = 'https://api.github.com/repos/nodejs/node/contents' -var dirs = [ +const concat = require('concat-stream') +const cp = require('child_process') +const fs = require('fs') +const hyperquest = require('hyperquest') +const path = require('path') +const split = require('split') +const through = require('through2') + +const url = 'https://api.github.com/repos/nodejs/node/contents' +const dirs = [ '/test/parallel', '/test/pummel' ] cp.execSync('rm -rf node/test-*.js', { cwd: path.join(__dirname, '../test') }) -var httpOpts = { +const httpOpts = { headers: { 'User-Agent': null // auth if github rate-limits you... @@ -25,7 +25,7 @@ var httpOpts = { } dirs.forEach(function (dir) { - var req = hyperquest(url + dir, httpOpts) + const req = hyperquest(url + dir, httpOpts) req.pipe(concat(function (data) { if (req.response.statusCode !== 200) { throw new Error(url + dir + ': ' + data.toString()) @@ -57,7 +57,7 @@ function downloadBufferTests (dir, files) { console.log(file.download_url) - var out = path.join(__dirname, '../test/node', file.name) + const out = path.join(__dirname, '../test/node', file.name) hyperquest(file.download_url, httpOpts) .pipe(split()) .pipe(testfixer(file.name)) @@ -69,14 +69,14 @@ function downloadBufferTests (dir, files) { } function testfixer (filename) { - var firstline = true + let firstline = true return through(function (line, enc, cb) { line = line.toString() if (firstline) { // require buffer explicitly - var preamble = 'var Buffer = require(\'../../\').Buffer;' + const preamble = 'var Buffer = require(\'../../\').Buffer;' if (/use strict/.test(line)) line += '\n' + preamble else line += preamble + '\n' + line firstline = false diff --git a/bin/test.js b/bin/test.js index 93d4c55..35c8aac 100644 --- a/bin/test.js +++ b/bin/test.js @@ -1,17 +1,17 @@ #!/usr/bin/env node -var cp = require('child_process') -var fs = require('fs') -var path = require('path') +const cp = require('child_process') +const fs = require('fs') +const path = require('path') -var node = cp.spawn('npm', ['run', 'test-node'], { stdio: 'inherit' }) +const node = cp.spawn('npm', ['run', 'test-node'], { stdio: 'inherit' }) node.on('close', function (code) { if (code !== 0) return process.exit(code) runBrowserTests() }) function runBrowserTests () { - var airtapYmlPath = path.join(__dirname, '..', '.airtap.yml') + const airtapYmlPath = path.join(__dirname, '..', '.airtap.yml') writeES5AirtapYml() cp.spawn('npm', ['run', 'test-browser-es5'], { stdio: 'inherit' }) diff --git a/index.js b/index.js index 1f4871b..5fe58eb 100644 --- a/index.js +++ b/index.js @@ -8,9 +8,9 @@ 'use strict' -var base64 = require('base64-js') -var ieee754 = require('ieee754') -var customInspectSymbol = +const base64 = require('base64-js') +const ieee754 = require('ieee754') +const customInspectSymbol = (typeof Symbol === 'function' && typeof Symbol['for'] === 'function') // eslint-disable-line dot-notation ? Symbol['for']('nodejs.util.inspect.custom') // eslint-disable-line dot-notation : null @@ -19,7 +19,7 @@ exports.Buffer = Buffer exports.SlowBuffer = SlowBuffer exports.INSPECT_MAX_BYTES = 50 -var K_MAX_LENGTH = 0x7fffffff +const K_MAX_LENGTH = 0x7fffffff exports.kMaxLength = K_MAX_LENGTH /** @@ -49,8 +49,8 @@ if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' && function typedArraySupport () { // Can typed array instances can be augmented? try { - var arr = new Uint8Array(1) - var proto = { foo: function () { return 42 } } + const arr = new Uint8Array(1) + const proto = { foo: function () { return 42 } } Object.setPrototypeOf(proto, Uint8Array.prototype) Object.setPrototypeOf(arr, proto) return arr.foo() === 42 @@ -80,7 +80,7 @@ function createBuffer (length) { throw new RangeError('The value "' + length + '" is invalid for option "size"') } // Return an augmented `Uint8Array` instance - var buf = new Uint8Array(length) + const buf = new Uint8Array(length) Object.setPrototypeOf(buf, Buffer.prototype) return buf } @@ -143,12 +143,12 @@ function from (value, encodingOrOffset, length) { ) } - var valueOf = value.valueOf && value.valueOf() + const valueOf = value.valueOf && value.valueOf() if (valueOf != null && valueOf !== value) { return Buffer.from(valueOf, encodingOrOffset, length) } - var b = fromObject(value) + const b = fromObject(value) if (b) return b if (typeof Symbol !== 'undefined' && Symbol.toPrimitive != null && @@ -238,10 +238,10 @@ function fromString (string, encoding) { throw new TypeError('Unknown encoding: ' + encoding) } - var length = byteLength(string, encoding) | 0 - var buf = createBuffer(length) + const length = byteLength(string, encoding) | 0 + let buf = createBuffer(length) - var actual = buf.write(string, encoding) + const actual = buf.write(string, encoding) if (actual !== length) { // Writing a hex string, for example, that contains invalid characters will @@ -254,9 +254,9 @@ function fromString (string, encoding) { } function fromArrayLike (array) { - var length = array.length < 0 ? 0 : checked(array.length) | 0 - var buf = createBuffer(length) - for (var i = 0; i < length; i += 1) { + const length = array.length < 0 ? 0 : checked(array.length) | 0 + const buf = createBuffer(length) + for (let i = 0; i < length; i += 1) { buf[i] = array[i] & 255 } return buf @@ -264,7 +264,7 @@ function fromArrayLike (array) { function fromArrayView (arrayView) { if (isInstance(arrayView, Uint8Array)) { - var copy = new Uint8Array(arrayView) + const copy = new Uint8Array(arrayView) return fromArrayBuffer(copy.buffer, copy.byteOffset, copy.byteLength) } return fromArrayLike(arrayView) @@ -279,7 +279,7 @@ function fromArrayBuffer (array, byteOffset, length) { throw new RangeError('"length" is outside of buffer bounds') } - var buf + let buf if (byteOffset === undefined && length === undefined) { buf = new Uint8Array(array) } else if (length === undefined) { @@ -296,8 +296,8 @@ function fromArrayBuffer (array, byteOffset, length) { function fromObject (obj) { if (Buffer.isBuffer(obj)) { - var len = checked(obj.length) | 0 - var buf = createBuffer(len) + const len = checked(obj.length) | 0 + const buf = createBuffer(len) if (buf.length === 0) { return buf @@ -352,10 +352,10 @@ Buffer.compare = function compare (a, b) { if (a === b) return 0 - var x = a.length - var y = b.length + let x = a.length + let y = b.length - for (var i = 0, len = Math.min(x, y); i < len; ++i) { + for (let i = 0, len = Math.min(x, y); i < len; ++i) { if (a[i] !== b[i]) { x = a[i] y = b[i] @@ -396,7 +396,7 @@ Buffer.concat = function concat (list, length) { return Buffer.alloc(0) } - var i + let i if (length === undefined) { length = 0 for (i = 0; i < list.length; ++i) { @@ -404,10 +404,10 @@ Buffer.concat = function concat (list, length) { } } - var buffer = Buffer.allocUnsafe(length) - var pos = 0 + const buffer = Buffer.allocUnsafe(length) + let pos = 0 for (i = 0; i < list.length; ++i) { - var buf = list[i] + const buf = list[i] if (isInstance(buf, Uint8Array)) { Uint8Array.prototype.set.call( buffer, @@ -438,12 +438,12 @@ function byteLength (string, encoding) { ) } - var len = string.length - var mustMatch = (arguments.length > 2 && arguments[2] === true) + const len = string.length + const mustMatch = (arguments.length > 2 && arguments[2] === true) if (!mustMatch && len === 0) return 0 // Use a for loop to avoid recursion - var loweredCase = false + let loweredCase = false for (;;) { switch (encoding) { case 'ascii': @@ -474,7 +474,7 @@ function byteLength (string, encoding) { Buffer.byteLength = byteLength function slowToString (encoding, start, end) { - var loweredCase = false + let loweredCase = false // No need to verify that "this.length <= MAX_UINT32" since it's a read-only // property of a typed array. @@ -552,28 +552,28 @@ function slowToString (encoding, start, end) { Buffer.prototype._isBuffer = true function swap (b, n, m) { - var i = b[n] + const i = b[n] b[n] = b[m] b[m] = i } Buffer.prototype.swap16 = function swap16 () { - var len = this.length + const len = this.length if (len % 2 !== 0) { throw new RangeError('Buffer size must be a multiple of 16-bits') } - for (var i = 0; i < len; i += 2) { + for (let i = 0; i < len; i += 2) { swap(this, i, i + 1) } return this } Buffer.prototype.swap32 = function swap32 () { - var len = this.length + const len = this.length if (len % 4 !== 0) { throw new RangeError('Buffer size must be a multiple of 32-bits') } - for (var i = 0; i < len; i += 4) { + for (let i = 0; i < len; i += 4) { swap(this, i, i + 3) swap(this, i + 1, i + 2) } @@ -581,11 +581,11 @@ Buffer.prototype.swap32 = function swap32 () { } Buffer.prototype.swap64 = function swap64 () { - var len = this.length + const len = this.length if (len % 8 !== 0) { throw new RangeError('Buffer size must be a multiple of 64-bits') } - for (var i = 0; i < len; i += 8) { + for (let i = 0; i < len; i += 8) { swap(this, i, i + 7) swap(this, i + 1, i + 6) swap(this, i + 2, i + 5) @@ -595,7 +595,7 @@ Buffer.prototype.swap64 = function swap64 () { } Buffer.prototype.toString = function toString () { - var length = this.length + const length = this.length if (length === 0) return '' if (arguments.length === 0) return utf8Slice(this, 0, length) return slowToString.apply(this, arguments) @@ -610,8 +610,8 @@ Buffer.prototype.equals = function equals (b) { } Buffer.prototype.inspect = function inspect () { - var str = '' - var max = exports.INSPECT_MAX_BYTES + let str = '' + const max = exports.INSPECT_MAX_BYTES str = this.toString('hex', 0, max).replace(/(.{2})/g, '$1 ').trim() if (this.length > max) str += ' ... ' return '' @@ -665,14 +665,14 @@ Buffer.prototype.compare = function compare (target, start, end, thisStart, this if (this === target) return 0 - var x = thisEnd - thisStart - var y = end - start - var len = Math.min(x, y) + let x = thisEnd - thisStart + let y = end - start + const len = Math.min(x, y) - var thisCopy = this.slice(thisStart, thisEnd) - var targetCopy = target.slice(start, end) + const thisCopy = this.slice(thisStart, thisEnd) + const targetCopy = target.slice(start, end) - for (var i = 0; i < len; ++i) { + for (let i = 0; i < len; ++i) { if (thisCopy[i] !== targetCopy[i]) { x = thisCopy[i] y = targetCopy[i] @@ -751,9 +751,9 @@ function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { } function arrayIndexOf (arr, val, byteOffset, encoding, dir) { - var indexSize = 1 - var arrLength = arr.length - var valLength = val.length + let indexSize = 1 + let arrLength = arr.length + let valLength = val.length if (encoding !== undefined) { encoding = String(encoding).toLowerCase() @@ -777,9 +777,9 @@ function arrayIndexOf (arr, val, byteOffset, encoding, dir) { } } - var i + let i if (dir) { - var foundIndex = -1 + let foundIndex = -1 for (i = byteOffset; i < arrLength; i++) { if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { if (foundIndex === -1) foundIndex = i @@ -792,8 +792,8 @@ function arrayIndexOf (arr, val, byteOffset, encoding, dir) { } else { if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength for (i = byteOffset; i >= 0; i--) { - var found = true - for (var j = 0; j < valLength; j++) { + let found = true + for (let j = 0; j < valLength; j++) { if (read(arr, i + j) !== read(val, j)) { found = false break @@ -820,7 +820,7 @@ Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) function hexWrite (buf, string, offset, length) { offset = Number(offset) || 0 - var remaining = buf.length - offset + const remaining = buf.length - offset if (!length) { length = remaining } else { @@ -830,13 +830,14 @@ function hexWrite (buf, string, offset, length) { } } - var strLen = string.length + const strLen = string.length if (length > strLen / 2) { length = strLen / 2 } - for (var i = 0; i < length; ++i) { - var parsed = parseInt(string.substr(i * 2, 2), 16) + let i + for (i = 0; i < length; ++i) { + const parsed = parseInt(string.substr(i * 2, 2), 16) if (numberIsNaN(parsed)) return i buf[offset + i] = parsed } @@ -886,7 +887,7 @@ Buffer.prototype.write = function write (string, offset, length, encoding) { ) } - var remaining = this.length - offset + const remaining = this.length - offset if (length === undefined || length > remaining) length = remaining if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { @@ -895,7 +896,7 @@ Buffer.prototype.write = function write (string, offset, length, encoding) { if (!encoding) encoding = 'utf8' - var loweredCase = false + let loweredCase = false for (;;) { switch (encoding) { case 'hex': @@ -945,13 +946,13 @@ function base64Slice (buf, start, end) { function utf8Slice (buf, start, end) { end = Math.min(buf.length, end) - var res = [] + const res = [] - var i = start + let i = start while (i < end) { - var firstByte = buf[i] - var codePoint = null - var bytesPerSequence = (firstByte > 0xEF) + const firstByte = buf[i] + let codePoint = null + let bytesPerSequence = (firstByte > 0xEF) ? 4 : (firstByte > 0xDF) ? 3 @@ -960,7 +961,7 @@ function utf8Slice (buf, start, end) { : 1 if (i + bytesPerSequence <= end) { - var secondByte, thirdByte, fourthByte, tempCodePoint + let secondByte, thirdByte, fourthByte, tempCodePoint switch (bytesPerSequence) { case 1: @@ -1022,17 +1023,17 @@ function utf8Slice (buf, start, end) { // Based on http://stackoverflow.com/a/22747272/680742, the browser with // the lowest limit is Chrome, with 0x10000 args. // We go 1 magnitude less, for safety -var MAX_ARGUMENTS_LENGTH = 0x1000 +const MAX_ARGUMENTS_LENGTH = 0x1000 function decodeCodePointsArray (codePoints) { - var len = codePoints.length + const len = codePoints.length if (len <= MAX_ARGUMENTS_LENGTH) { return String.fromCharCode.apply(String, codePoints) // avoid extra slice() } // Decode in chunks to avoid "call stack size exceeded". - var res = '' - var i = 0 + let res = '' + let i = 0 while (i < len) { res += String.fromCharCode.apply( String, @@ -1043,50 +1044,50 @@ function decodeCodePointsArray (codePoints) { } function asciiSlice (buf, start, end) { - var ret = '' + let ret = '' end = Math.min(buf.length, end) - for (var i = start; i < end; ++i) { + for (let i = start; i < end; ++i) { ret += String.fromCharCode(buf[i] & 0x7F) } return ret } function latin1Slice (buf, start, end) { - var ret = '' + let ret = '' end = Math.min(buf.length, end) - for (var i = start; i < end; ++i) { + for (let i = start; i < end; ++i) { ret += String.fromCharCode(buf[i]) } return ret } function hexSlice (buf, start, end) { - var len = buf.length + const len = buf.length if (!start || start < 0) start = 0 if (!end || end < 0 || end > len) end = len - var out = '' - for (var i = start; i < end; ++i) { + let out = '' + for (let i = start; i < end; ++i) { out += hexSliceLookupTable[buf[i]] } return out } function utf16leSlice (buf, start, end) { - var bytes = buf.slice(start, end) - var res = '' + const bytes = buf.slice(start, end) + let res = '' // If bytes.length is odd, the last 8 bits must be ignored (same as node.js) - for (var i = 0; i < bytes.length - 1; i += 2) { + for (let i = 0; i < bytes.length - 1; i += 2) { res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256)) } return res } Buffer.prototype.slice = function slice (start, end) { - var len = this.length + const len = this.length start = ~~start end = end === undefined ? len : ~~end @@ -1106,7 +1107,7 @@ Buffer.prototype.slice = function slice (start, end) { if (end < start) end = start - var newBuf = this.subarray(start, end) + const newBuf = this.subarray(start, end) // Return an augmented `Uint8Array` instance Object.setPrototypeOf(newBuf, Buffer.prototype) @@ -1127,9 +1128,9 @@ Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) byteLength = byteLength >>> 0 if (!noAssert) checkOffset(offset, byteLength, this.length) - var val = this[offset] - var mul = 1 - var i = 0 + let val = this[offset] + let mul = 1 + let i = 0 while (++i < byteLength && (mul *= 0x100)) { val += this[offset + i] * mul } @@ -1145,8 +1146,8 @@ Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) checkOffset(offset, byteLength, this.length) } - var val = this[offset + --byteLength] - var mul = 1 + let val = this[offset + --byteLength] + let mul = 1 while (byteLength > 0 && (mul *= 0x100)) { val += this[offset + --byteLength] * mul } @@ -1202,9 +1203,9 @@ Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { byteLength = byteLength >>> 0 if (!noAssert) checkOffset(offset, byteLength, this.length) - var val = this[offset] - var mul = 1 - var i = 0 + let val = this[offset] + let mul = 1 + let i = 0 while (++i < byteLength && (mul *= 0x100)) { val += this[offset + i] * mul } @@ -1220,9 +1221,9 @@ Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { byteLength = byteLength >>> 0 if (!noAssert) checkOffset(offset, byteLength, this.length) - var i = byteLength - var mul = 1 - var val = this[offset + --i] + let i = byteLength + let mul = 1 + let val = this[offset + --i] while (i > 0 && (mul *= 0x100)) { val += this[offset + --i] * mul } @@ -1243,14 +1244,14 @@ Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 2, this.length) - var val = this[offset] | (this[offset + 1] << 8) + const val = this[offset] | (this[offset + 1] << 8) return (val & 0x8000) ? val | 0xFFFF0000 : val } Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { offset = offset >>> 0 if (!noAssert) checkOffset(offset, 2, this.length) - var val = this[offset + 1] | (this[offset] << 8) + const val = this[offset + 1] | (this[offset] << 8) return (val & 0x8000) ? val | 0xFFFF0000 : val } @@ -1310,12 +1311,12 @@ Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) { - var maxBytes = Math.pow(2, 8 * byteLength) - 1 + const maxBytes = Math.pow(2, 8 * byteLength) - 1 checkInt(this, value, offset, byteLength, maxBytes, 0) } - var mul = 1 - var i = 0 + let mul = 1 + let i = 0 this[offset] = value & 0xFF while (++i < byteLength && (mul *= 0x100)) { this[offset + i] = (value / mul) & 0xFF @@ -1330,12 +1331,12 @@ Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, offset = offset >>> 0 byteLength = byteLength >>> 0 if (!noAssert) { - var maxBytes = Math.pow(2, 8 * byteLength) - 1 + const maxBytes = Math.pow(2, 8 * byteLength) - 1 checkInt(this, value, offset, byteLength, maxBytes, 0) } - var i = byteLength - 1 - var mul = 1 + let i = byteLength - 1 + let mul = 1 this[offset + i] = value & 0xFF while (--i >= 0 && (mul *= 0x100)) { this[offset + i] = (value / mul) & 0xFF @@ -1401,14 +1402,14 @@ Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, no value = +value offset = offset >>> 0 if (!noAssert) { - var limit = Math.pow(2, (8 * byteLength) - 1) + const limit = Math.pow(2, (8 * byteLength) - 1) checkInt(this, value, offset, byteLength, limit - 1, -limit) } - var i = 0 - var mul = 1 - var sub = 0 + let i = 0 + let mul = 1 + let sub = 0 this[offset] = value & 0xFF while (++i < byteLength && (mul *= 0x100)) { if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { @@ -1424,14 +1425,14 @@ Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, no value = +value offset = offset >>> 0 if (!noAssert) { - var limit = Math.pow(2, (8 * byteLength) - 1) + const limit = Math.pow(2, (8 * byteLength) - 1) checkInt(this, value, offset, byteLength, limit - 1, -limit) } - var i = byteLength - 1 - var mul = 1 - var sub = 0 + let i = byteLength - 1 + let mul = 1 + let sub = 0 this[offset + i] = value & 0xFF while (--i >= 0 && (mul *= 0x100)) { if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { @@ -1560,7 +1561,7 @@ Buffer.prototype.copy = function copy (target, targetStart, start, end) { end = target.length - targetStart + start } - var len = end - start + const len = end - start if (this === target && typeof Uint8Array.prototype.copyWithin === 'function') { // Use built-in when available, missing from IE11 @@ -1598,7 +1599,7 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) { throw new TypeError('Unknown encoding: ' + encoding) } if (val.length === 1) { - var code = val.charCodeAt(0) + const code = val.charCodeAt(0) if ((encoding === 'utf8' && code < 128) || encoding === 'latin1') { // Fast path: If `val` fits into a single byte, use that numeric value. @@ -1625,16 +1626,16 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) { if (!val) val = 0 - var i + let i if (typeof val === 'number') { for (i = start; i < end; ++i) { this[i] = val } } else { - var bytes = Buffer.isBuffer(val) + const bytes = Buffer.isBuffer(val) ? val : Buffer.from(val, encoding) - var len = bytes.length + const len = bytes.length if (len === 0) { throw new TypeError('The value "' + val + '" is invalid for argument "value"') @@ -1650,7 +1651,7 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) { // HELPER FUNCTIONS // ================ -var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g +const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g function base64clean (str) { // Node takes equal signs as end of the Base64 encoding @@ -1668,12 +1669,12 @@ function base64clean (str) { function utf8ToBytes (string, units) { units = units || Infinity - var codePoint - var length = string.length - var leadSurrogate = null - var bytes = [] + let codePoint + const length = string.length + let leadSurrogate = null + const bytes = [] - for (var i = 0; i < length; ++i) { + for (let i = 0; i < length; ++i) { codePoint = string.charCodeAt(i) // is surrogate component @@ -1747,8 +1748,8 @@ function utf8ToBytes (string, units) { } function asciiToBytes (str) { - var byteArray = [] - for (var i = 0; i < str.length; ++i) { + const byteArray = [] + for (let i = 0; i < str.length; ++i) { // Node's code seems to be doing this and not & 0x7F.. byteArray.push(str.charCodeAt(i) & 0xFF) } @@ -1756,9 +1757,9 @@ function asciiToBytes (str) { } function utf16leToBytes (str, units) { - var c, hi, lo - var byteArray = [] - for (var i = 0; i < str.length; ++i) { + let c, hi, lo + const byteArray = [] + for (let i = 0; i < str.length; ++i) { if ((units -= 2) < 0) break c = str.charCodeAt(i) @@ -1776,7 +1777,8 @@ function base64ToBytes (str) { } function blitBuffer (src, dst, offset, length) { - for (var i = 0; i < length; ++i) { + let i + for (i = 0; i < length; ++i) { if ((i + offset >= dst.length) || (i >= src.length)) break dst[i + offset] = src[i] } @@ -1798,12 +1800,12 @@ function numberIsNaN (obj) { // Create lookup table for `toString('hex')` // See: https://github.com/feross/buffer/issues/219 -var hexSliceLookupTable = (function () { - var alphabet = '0123456789abcdef' - var table = new Array(256) - for (var i = 0; i < 16; ++i) { - var i16 = i * 16 - for (var j = 0; j < 16; ++j) { +const hexSliceLookupTable = (function () { + const alphabet = '0123456789abcdef' + const table = new Array(256) + for (let i = 0; i < 16; ++i) { + const i16 = i * 16 + for (let j = 0; j < 16; ++j) { table[i16 + j] = alphabet[i] + alphabet[j] } } diff --git a/test/base64.js b/test/base64.js index 533e76d..977225b 100644 --- a/test/base64.js +++ b/test/base64.js @@ -1,9 +1,9 @@ -var B = require('../').Buffer -var test = require('tape') +const B = require('../').Buffer +const test = require('tape') test('base64: ignore whitespace', function (t) { - var text = '\n YW9ldQ== ' - var buf = new B(text, 'base64') + const text = '\n YW9ldQ== ' + const buf = new B(text, 'base64') t.equal(buf.toString(), 'aoeu') t.end() }) @@ -46,7 +46,7 @@ test('base64: invalid non-alphanumeric characters -- should be stripped', functi }) test('base64: high byte', function (t) { - var highByte = B.from([128]) + const highByte = B.from([128]) t.deepEqual( B.alloc(1, highByte.toString('base64'), 'base64'), highByte diff --git a/test/basic.js b/test/basic.js index 42808ac..5500892 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,15 +1,15 @@ -var B = require('../').Buffer -var test = require('tape') +const B = require('../').Buffer +const test = require('tape') test('instanceof Buffer', function (t) { - var buf = new B([1, 2]) + const buf = new B([1, 2]) t.ok(buf instanceof B) t.end() }) test('convert to Uint8Array in modern browsers', function (t) { - var buf = new B([1, 2]) - var uint8array = new Uint8Array(buf.buffer) + const buf = new B([1, 2]) + const uint8array = new Uint8Array(buf.buffer) t.ok(uint8array instanceof Uint8Array) t.equal(uint8array[0], 1) t.equal(uint8array[1], 2) @@ -17,7 +17,7 @@ test('convert to Uint8Array in modern browsers', function (t) { }) test('indexes from a string', function (t) { - var buf = new B('abc') + const buf = new B('abc') t.equal(buf[0], 97) t.equal(buf[1], 98) t.equal(buf[2], 99) @@ -25,7 +25,7 @@ test('indexes from a string', function (t) { }) test('indexes from an array', function (t) { - var buf = new B([97, 98, 99]) + const buf = new B([97, 98, 99]) t.equal(buf[0], 97) t.equal(buf[1], 98) t.equal(buf[2], 99) @@ -33,7 +33,7 @@ test('indexes from an array', function (t) { }) test('setting index value should modify buffer contents', function (t) { - var buf = new B([97, 98, 99]) + const buf = new B([97, 98, 99]) t.equal(buf[2], 99) t.equal(buf.toString(), 'abc') @@ -44,7 +44,7 @@ test('setting index value should modify buffer contents', function (t) { }) test('storing negative number should cast to unsigned', function (t) { - var buf = new B(1) + let buf = new B(1) buf[0] = -3 t.equal(buf[0], 253) @@ -57,8 +57,8 @@ test('storing negative number should cast to unsigned', function (t) { }) test('test that memory is copied from array-like', function (t) { - var u = new Uint8Array(4) - var b = new B(u) + const u = new Uint8Array(4) + const b = new B(u) b[0] = 1 b[1] = 2 b[2] = 3 diff --git a/test/compare.js b/test/compare.js index 5d4d76c..0a7d811 100644 --- a/test/compare.js +++ b/test/compare.js @@ -1,10 +1,10 @@ -var B = require('../').Buffer -var test = require('tape') +const B = require('../').Buffer +const test = require('tape') test('buffer.compare', function (t) { - var b = new B(1).fill('a') - var c = new B(1).fill('c') - var d = new B(2).fill('aa') + const b = new B(1).fill('a') + const c = new B(1).fill('c') + const d = new B(2).fill('aa') t.equal(b.compare(c), -1) t.equal(c.compare(d), 1) @@ -21,27 +21,27 @@ test('buffer.compare', function (t) { test('buffer.compare argument validation', function (t) { t.throws(function () { - var b = new B(1) + const b = new B(1) B.compare(b, 'abc') }) t.throws(function () { - var b = new B(1) + const b = new B(1) B.compare('abc', b) }) t.throws(function () { - var b = new B(1) + const b = new B(1) b.compare('abc') }) t.end() }) test('buffer.equals', function (t) { - var b = new B(5).fill('abcdf') - var c = new B(5).fill('abcdf') - var d = new B(5).fill('abcde') - var e = new B(6).fill('abcdef') + const b = new B(5).fill('abcdf') + const c = new B(5).fill('abcdf') + const d = new B(5).fill('abcde') + const e = new B(6).fill('abcdef') t.ok(b.equals(c)) t.ok(!c.equals(d)) @@ -51,7 +51,7 @@ test('buffer.equals', function (t) { test('buffer.equals argument validation', function (t) { t.throws(function () { - var b = new B(1) + const b = new B(1) b.equals('abc') }) t.end() diff --git a/test/constructor.js b/test/constructor.js index 96dd2f9..d1083ba 100644 --- a/test/constructor.js +++ b/test/constructor.js @@ -1,5 +1,5 @@ -var B = require('../').Buffer -var test = require('tape') +const B = require('../').Buffer +const test = require('tape') test('new buffer from array', function (t) { t.equal( @@ -34,16 +34,16 @@ test('new buffer from string', function (t) { }) test('new buffer from buffer', function (t) { - var b1 = new B('asdf') - var b2 = new B(b1) + const b1 = new B('asdf') + const b2 = new B(b1) t.equal(b1.toString('hex'), b2.toString('hex')) t.end() }) test('new buffer from ArrayBuffer', function (t) { if (typeof ArrayBuffer !== 'undefined') { - var arraybuffer = new Uint8Array([0, 1, 2, 3]).buffer - var b = new B(arraybuffer) + const arraybuffer = new Uint8Array([0, 1, 2, 3]).buffer + const b = new B(arraybuffer) t.equal(b.length, 4) t.equal(b[0], 0) t.equal(b[1], 1) @@ -55,9 +55,9 @@ test('new buffer from ArrayBuffer', function (t) { }) test('new buffer from ArrayBuffer, shares memory', function (t) { - var u = new Uint8Array([0, 1, 2, 3]) - var arraybuffer = u.buffer - var b = new B(arraybuffer) + const u = new Uint8Array([0, 1, 2, 3]) + const arraybuffer = u.buffer + const b = new B(arraybuffer) t.equal(b.length, 4) t.equal(b[0], 0) t.equal(b[1], 1) @@ -79,8 +79,8 @@ test('new buffer from ArrayBuffer, shares memory', function (t) { test('new buffer from Uint8Array', function (t) { if (typeof Uint8Array !== 'undefined') { - var b1 = new Uint8Array([0, 1, 2, 3]) - var b2 = new B(b1) + const b1 = new Uint8Array([0, 1, 2, 3]) + const b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) @@ -93,8 +93,8 @@ test('new buffer from Uint8Array', function (t) { test('new buffer from Uint16Array', function (t) { if (typeof Uint16Array !== 'undefined') { - var b1 = new Uint16Array([0, 1, 2, 3]) - var b2 = new B(b1) + const b1 = new Uint16Array([0, 1, 2, 3]) + const b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) @@ -107,8 +107,8 @@ test('new buffer from Uint16Array', function (t) { test('new buffer from Uint32Array', function (t) { if (typeof Uint32Array !== 'undefined') { - var b1 = new Uint32Array([0, 1, 2, 3]) - var b2 = new B(b1) + const b1 = new Uint32Array([0, 1, 2, 3]) + const b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) @@ -121,8 +121,8 @@ test('new buffer from Uint32Array', function (t) { test('new buffer from Int16Array', function (t) { if (typeof Int16Array !== 'undefined') { - var b1 = new Int16Array([0, 1, 2, 3]) - var b2 = new B(b1) + const b1 = new Int16Array([0, 1, 2, 3]) + const b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) @@ -135,8 +135,8 @@ test('new buffer from Int16Array', function (t) { test('new buffer from Int32Array', function (t) { if (typeof Int32Array !== 'undefined') { - var b1 = new Int32Array([0, 1, 2, 3]) - var b2 = new B(b1) + const b1 = new Int32Array([0, 1, 2, 3]) + const b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) @@ -149,8 +149,8 @@ test('new buffer from Int32Array', function (t) { test('new buffer from Float32Array', function (t) { if (typeof Float32Array !== 'undefined') { - var b1 = new Float32Array([0, 1, 2, 3]) - var b2 = new B(b1) + const b1 = new Float32Array([0, 1, 2, 3]) + const b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) @@ -163,8 +163,8 @@ test('new buffer from Float32Array', function (t) { test('new buffer from Float64Array', function (t) { if (typeof Float64Array !== 'undefined') { - var b1 = new Float64Array([0, 1, 2, 3]) - var b2 = new B(b1) + const b1 = new Float64Array([0, 1, 2, 3]) + const b2 = new B(b1) t.equal(b1.length, b2.length) t.equal(b1[0], 0) t.equal(b1[1], 1) @@ -181,10 +181,10 @@ test('new buffer from buffer.toJSON() output', function (t) { t.end() return } - var buf = new B('test') - var json = JSON.stringify(buf) - var obj = JSON.parse(json) - var copy = new B(obj) + const buf = new B('test') + const json = JSON.stringify(buf) + const obj = JSON.parse(json) + const copy = new B(obj) t.ok(buf.equals(copy)) t.end() }) diff --git a/test/from-string.js b/test/from-string.js index 630dc15..64414f1 100644 --- a/test/from-string.js +++ b/test/from-string.js @@ -1,47 +1,47 @@ -var B = require('../').Buffer -var test = require('tape') +const B = require('../').Buffer +const test = require('tape') test('detect utf16 surrogate pairs', function (t) { - var text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D' - var buf = new B(text) + const text = '\uD83D\uDE38' + '\uD83D\uDCAD' + '\uD83D\uDC4D' + const buf = new B(text) t.equal(text, buf.toString()) t.end() }) test('detect utf16 surrogate pairs over U+20000 until U+10FFFF', function (t) { - var text = '\uD842\uDFB7' + '\uD93D\uDCAD' + '\uDBFF\uDFFF' - var buf = new B(text) + const text = '\uD842\uDFB7' + '\uD93D\uDCAD' + '\uDBFF\uDFFF' + const buf = new B(text) t.equal(text, buf.toString()) t.end() }) test('replace orphaned utf16 surrogate lead code point', function (t) { - var text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D' - var buf = new B(text) + const text = '\uD83D\uDE38' + '\uD83D' + '\uD83D\uDC4D' + const buf = new B(text) t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d])) t.end() }) test('replace orphaned utf16 surrogate trail code point', function (t) { - var text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D' - var buf = new B(text) + const text = '\uD83D\uDE38' + '\uDCAD' + '\uD83D\uDC4D' + const buf = new B(text) t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0xef, 0xbf, 0xbd, 0xf0, 0x9f, 0x91, 0x8d])) t.end() }) test('do not write partial utf16 code units', function (t) { - var f = new B([0, 0, 0, 0, 0]) + const f = new B([0, 0, 0, 0, 0]) t.equal(f.length, 5) - var size = f.write('あいうえお', 'utf16le') + const size = f.write('あいうえお', 'utf16le') t.equal(size, 4) t.deepEqual(f, new B([0x42, 0x30, 0x44, 0x30, 0x00])) t.end() }) test('handle partial utf16 code points when encoding to utf8 the way node does', function (t) { - var text = '\uD83D\uDE38' + '\uD83D\uDC4D' + const text = '\uD83D\uDE38' + '\uD83D\uDC4D' - var buf = new B(8) + let buf = new B(8) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0xf0, 0x9f, 0x98, 0xb8, 0xf0, 0x9f, 0x91, 0x8d])) @@ -85,9 +85,9 @@ test('handle partial utf16 code points when encoding to utf8 the way node does', }) test('handle invalid utf16 code points when encoding to utf8 the way node does', function (t) { - var text = 'a' + '\uDE38\uD83D' + 'b' + const text = 'a' + '\uDE38\uD83D' + 'b' - var buf = new B(8) + let buf = new B(8) buf.fill(0) buf.write(text) t.deepEqual(buf, new B([0x61, 0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x62])) diff --git a/test/is-buffer.js b/test/is-buffer.js index 177e98e..88e5ad7 100644 --- a/test/is-buffer.js +++ b/test/is-buffer.js @@ -1,6 +1,6 @@ -var B = require('../').Buffer -var isBuffer = require('is-buffer') -var test = require('tape') +const B = require('../').Buffer +const isBuffer = require('is-buffer') +const test = require('tape') test('is-buffer tests', function (t) { t.ok(isBuffer(new B(4)), 'new Buffer(4)') diff --git a/test/methods.js b/test/methods.js index 082c5df..20f445c 100644 --- a/test/methods.js +++ b/test/methods.js @@ -1,8 +1,8 @@ -var B = require('../').Buffer -var test = require('tape') +const B = require('../').Buffer +const test = require('tape') test('buffer.toJSON', function (t) { - var data = [1, 2, 3, 4] + const data = [1, 2, 3, 4] t.deepEqual( new B(data).toJSON(), { type: 'Buffer', data: [1, 2, 3, 4] } @@ -12,10 +12,10 @@ test('buffer.toJSON', function (t) { test('buffer.copy', function (t) { // copied from nodejs.org example - var buf1 = new B(26) - var buf2 = new B(26) + const buf1 = new B(26) + const buf2 = new B(26) - for (var i = 0; i < 26; i++) { + for (let i = 0; i < 26; i++) { buf1[i] = i + 97 // 97 is ASCII a buf2[i] = 33 // ASCII ! } @@ -30,7 +30,7 @@ test('buffer.copy', function (t) { }) test('test offset returns are correct', function (t) { - var b = new B(16) + const b = new B(16) t.equal(4, b.writeUInt32LE(0, 0)) t.equal(6, b.writeUInt16LE(0, 4)) t.equal(7, b.writeUInt8(0, 6)) @@ -40,17 +40,17 @@ test('test offset returns are correct', function (t) { }) test('concat() a varying number of buffers', function (t) { - var zero = [] - var one = [new B('asdf')] - var long = [] - for (var i = 0; i < 10; i++) { + const zero = [] + const one = [new B('asdf')] + const long = [] + for (let i = 0; i < 10; i++) { long.push(new B('asdf')) } - var flatZero = B.concat(zero) - var flatOne = B.concat(one) - var flatLong = B.concat(long) - var flatLongLen = B.concat(long, 40) + const flatZero = B.concat(zero) + const flatOne = B.concat(one) + const flatLong = B.concat(long) + const flatLongLen = B.concat(long, 40) t.equal(flatZero.length, 0) t.equal(flatOne.toString(), 'asdf') @@ -61,21 +61,21 @@ test('concat() a varying number of buffers', function (t) { }) test('concat() works on Uint8Array instances', function (t) { - var result = B.concat([new Uint8Array([1, 2]), new Uint8Array([3, 4])]) - var expected = B.from([1, 2, 3, 4]) + const result = B.concat([new Uint8Array([1, 2]), new Uint8Array([3, 4])]) + const expected = B.from([1, 2, 3, 4]) t.deepEqual(result, expected) t.end() }) test('fill', function (t) { - var b = new B(10) + const b = new B(10) b.fill(2) t.equal(b.toString('hex'), '02020202020202020202') t.end() }) test('fill (string)', function (t) { - var b = new B(10) + const b = new B(10) b.fill('abc') t.equal(b.toString(), 'abcabcabca') b.fill('է') @@ -84,18 +84,18 @@ test('fill (string)', function (t) { }) test('copy() empty buffer with sourceEnd=0', function (t) { - var source = new B([42]) - var destination = new B([43]) + const source = new B([42]) + const destination = new B([43]) source.copy(destination, 0, 0, 0) t.equal(destination.readUInt8(0), 43) t.end() }) test('copy() after slice()', function (t) { - var source = new B(200) - var dest = new B(200) - var expected = new B(200) - for (var i = 0; i < 200; i++) { + const source = new B(200) + const dest = new B(200) + const expected = new B(200) + for (let i = 0; i < 200; i++) { source[i] = i dest[i] = 0 } @@ -107,14 +107,14 @@ test('copy() after slice()', function (t) { }) test('copy() ascending', function (t) { - var b = new B('abcdefghij') + const b = new B('abcdefghij') b.copy(b, 0, 3, 10) t.equal(b.toString(), 'defghijhij') t.end() }) test('copy() descending', function (t) { - var b = new B('abcdefghij') + const b = new B('abcdefghij') b.copy(b, 3, 0, 7) t.equal(b.toString(), 'abcabcdefg') t.end() diff --git a/test/slice.js b/test/slice.js index d8cd20e..035c9aa 100644 --- a/test/slice.js +++ b/test/slice.js @@ -1,13 +1,13 @@ -var B = require('../').Buffer -var test = require('tape') +const B = require('../').Buffer +const test = require('tape') test('modifying buffer created by .slice() modifies original memory', function (t) { - var buf1 = new B(26) - for (var i = 0; i < 26; i++) { + const buf1 = new B(26) + for (let i = 0; i < 26; i++) { buf1[i] = i + 97 // 97 is ASCII a } - var buf2 = buf1.slice(0, 3) + const buf2 = buf1.slice(0, 3) t.equal(buf2.toString('ascii', 0, buf2.length), 'abc') buf2[0] = '!'.charCodeAt(0) @@ -17,12 +17,12 @@ test('modifying buffer created by .slice() modifies original memory', function ( }) test('modifying parent buffer modifies .slice() buffer\'s memory', function (t) { - var buf1 = new B(26) - for (var i = 0; i < 26; i++) { + const buf1 = new B(26) + for (let i = 0; i < 26; i++) { buf1[i] = i + 97 // 97 is ASCII a } - var buf2 = buf1.slice(0, 3) + const buf2 = buf1.slice(0, 3) t.equal(buf2.toString('ascii', 0, buf2.length), 'abc') buf1[0] = '!'.charCodeAt(0) diff --git a/test/static.js b/test/static.js index a2380b5..07ea6c6 100644 --- a/test/static.js +++ b/test/static.js @@ -1,5 +1,5 @@ -var B = require('../').Buffer -var test = require('tape') +const B = require('../').Buffer +const test = require('tape') test('Buffer.isEncoding', function (t) { t.equal(B.isEncoding('HEX'), true) diff --git a/test/to-string.js b/test/to-string.js index 6b46875..3f146a8 100644 --- a/test/to-string.js +++ b/test/to-string.js @@ -1,5 +1,5 @@ -var B = require('../').Buffer -var test = require('tape') +const B = require('../').Buffer +const test = require('tape') test('utf8 buffer to base64', function (t) { t.equal( diff --git a/test/write.js b/test/write.js index 94e99d0..d132e85 100644 --- a/test/write.js +++ b/test/write.js @@ -1,9 +1,9 @@ -var B = require('../').Buffer -var test = require('tape') -var isnan = require('is-nan') +const B = require('../').Buffer +const test = require('tape') +const isnan = require('is-nan') test('buffer.write string should get parsed as number', function (t) { - var b = new B(64) + const b = new B(64) b.writeUInt16LE('1003', 0) t.equal(b.readUInt16LE(0), 1003) t.end() @@ -12,14 +12,14 @@ test('buffer.write string should get parsed as number', function (t) { test('buffer.writeUInt8 a fractional number will get Math.floored', function (t) { // Some extra work is necessary to make this test pass with the Object implementation - var b = new B(1) + const b = new B(1) b.writeInt8(5.5, 0) t.equal(b[0], 5) t.end() }) test('writeUint8 with a negative number throws', function (t) { - var buf = new B(1) + const buf = new B(1) t.throws(function () { buf.writeUInt8(-3, 0) @@ -30,30 +30,30 @@ test('writeUint8 with a negative number throws', function (t) { test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) { t.plan(2 * ((2 * 2 * 2) + 2)) - var hex = [ + const hex = [ '03', '0300', '0003', '03000000', '00000003', 'fd', 'fdff', 'fffd', 'fdffffff', 'fffffffd' ] - var reads = [3, 3, 3, 3, 3, -3, -3, -3, -3, -3] - var xs = ['UInt', 'Int'] - var ys = [8, 16, 32] - for (var i = 0; i < xs.length; i++) { - var x = xs[i] - for (var j = 0; j < ys.length; j++) { - var y = ys[j] - var endianesses = (y === 8) ? [''] : ['LE', 'BE'] - for (var k = 0; k < endianesses.length; k++) { - var z = endianesses[k] + const reads = [3, 3, 3, 3, 3, -3, -3, -3, -3, -3] + const xs = ['UInt', 'Int'] + const ys = [8, 16, 32] + for (let i = 0; i < xs.length; i++) { + const x = xs[i] + for (let j = 0; j < ys.length; j++) { + const y = ys[j] + const endianesses = (y === 8) ? [''] : ['LE', 'BE'] + for (let k = 0; k < endianesses.length; k++) { + const z = endianesses[k] - var v1 = new B(y / 8) - var writefn = 'write' + x + y + z - var val = (x === 'Int') ? -3 : 3 + const v1 = new B(y / 8) + const writefn = 'write' + x + y + z + const val = (x === 'Int') ? -3 : 3 v1[writefn](val, 0) t.equal( v1.toString('hex'), hex.shift() ) - var readfn = 'read' + x + y + z + const readfn = 'read' + x + y + z t.equal( v1[readfn](0), reads.shift() @@ -66,29 +66,29 @@ test('hex of write{Uint,Int}{8,16,32}{LE,BE}', function (t) { test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) { t.plan(3 * ((2 * 2 * 2) + 2)) - var hex = [ + const hex = [ '', '03', '00', '030000', '000000', '', 'fd', 'ff', 'fdffff', 'ffffff' ] - var reads = [ + const reads = [ undefined, 3, 0, NaN, 0, undefined, 253, -256, 16777213, -256 ] - var xs = ['UInt', 'Int'] - var ys = [8, 16, 32] - for (var i = 0; i < xs.length; i++) { - var x = xs[i] - for (var j = 0; j < ys.length; j++) { - var y = ys[j] - var endianesses = (y === 8) ? [''] : ['LE', 'BE'] - for (var k = 0; k < endianesses.length; k++) { - var z = endianesses[k] + const xs = ['UInt', 'Int'] + const ys = [8, 16, 32] + for (let i = 0; i < xs.length; i++) { + const x = xs[i] + for (let j = 0; j < ys.length; j++) { + const y = ys[j] + const endianesses = (y === 8) ? [''] : ['LE', 'BE'] + for (let k = 0; k < endianesses.length; k++) { + const z = endianesses[k] - var v1 = new B((y / 8) - 1) - var next = new B(4) + const v1 = new B((y / 8) - 1) + const next = new B(4) next.writeUInt32BE(0, 0) - var writefn = 'write' + x + y + z - var val = (x === 'Int') ? -3 : 3 + const writefn = 'write' + x + y + z + const val = (x === 'Int') ? -3 : 3 v1[writefn](val, 0, true) t.equal( v1.toString('hex'), @@ -98,8 +98,8 @@ test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) { t.equal(next.readUInt32BE(0), 0) // check that no bytes are read from next buffer. next.writeInt32BE(~0, 0) - var readfn = 'read' + x + y + z - var r = reads.shift() + const readfn = 'read' + x + y + z + const r = reads.shift() if (isnan(r)) t.pass('equal') else t.equal(v1[readfn](0, true), r) } @@ -108,11 +108,11 @@ test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) { t.end() }) test('large values do not improperly roll over (ref #80)', function (t) { - var nums = [-25589992, -633756690, -898146932] - var out = new B(12) + const nums = [-25589992, -633756690, -898146932] + const out = new B(12) out.fill(0) out.writeInt32BE(nums[0], 0) - var newNum = out.readInt32BE(0) + let newNum = out.readInt32BE(0) t.equal(nums[0], newNum) out.writeInt32BE(nums[1], 4) newNum = out.readInt32BE(4) diff --git a/test/write_infinity.js b/test/write_infinity.js index ffe80d6..39f1d37 100644 --- a/test/write_infinity.js +++ b/test/write_infinity.js @@ -1,43 +1,43 @@ -var B = require('../').Buffer -var test = require('tape') +const B = require('../').Buffer +const test = require('tape') test('write/read Infinity as a float', function (t) { - var buf = new B(4) + const buf = new B(4) t.equal(buf.writeFloatBE(Infinity, 0), 4) t.equal(buf.readFloatBE(0), Infinity) t.end() }) test('write/read -Infinity as a float', function (t) { - var buf = new B(4) + const buf = new B(4) t.equal(buf.writeFloatBE(-Infinity, 0), 4) t.equal(buf.readFloatBE(0), -Infinity) t.end() }) test('write/read Infinity as a double', function (t) { - var buf = new B(8) + const buf = new B(8) t.equal(buf.writeDoubleBE(Infinity, 0), 8) t.equal(buf.readDoubleBE(0), Infinity) t.end() }) test('write/read -Infinity as a double', function (t) { - var buf = new B(8) + const buf = new B(8) t.equal(buf.writeDoubleBE(-Infinity, 0), 8) t.equal(buf.readDoubleBE(0), -Infinity) t.end() }) test('write/read float greater than max', function (t) { - var buf = new B(4) + const buf = new B(4) t.equal(buf.writeFloatBE(4e38, 0), 4) t.equal(buf.readFloatBE(0), Infinity) t.end() }) test('write/read float less than min', function (t) { - var buf = new B(4) + const buf = new B(4) t.equal(buf.writeFloatBE(-4e40, 0), 4) t.equal(buf.readFloatBE(0), -Infinity) t.end()