From: Feross Aboukhadijeh Date: Sat, 7 Mar 2015 02:11:03 +0000 (-0800) Subject: style: always use braces except when single-line X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=18eb1016a80ad398dccf4cdc7a2127af38b17850;p=buffer.git style: always use braces except when single-line --- diff --git a/bin/download-node-tests.js b/bin/download-node-tests.js index c7e14d6..bae2386 100755 --- a/bin/download-node-tests.js +++ b/bin/download-node-tests.js @@ -35,10 +35,11 @@ function downloadBufferTests (dir, files) { if (!/test-buffer.*/.test(file.name)) return var path - if (file.name !== 'test-buffer-iterator.js') + if (file.name !== 'test-buffer-iterator.js') { path = __dirname + '/../test/node/' + file.name - else + } else { path = __dirname + '/../test/es6/' + file.name + } hyperquest(file.download_url, httpOpts) .pipe(split()) diff --git a/index.js b/index.js index 147a593..cd2a1c8 100644 --- a/index.js +++ b/index.js @@ -65,33 +65,30 @@ Buffer.TYPED_ARRAY_SUPPORT = (function () { * prototype. */ function Buffer (subject, encoding, noZero) { - if (!(this instanceof Buffer)) - return new Buffer(subject, encoding, noZero) + if (!(this instanceof Buffer)) return new Buffer(subject, encoding, noZero) var type = typeof subject - - // Find the length var length + if (type === 'number') { length = +subject } else if (type === 'string') { length = Buffer.byteLength(subject, encoding) - } else if (type === 'object' && subject !== null) { // assume object is array-like - if (subject.type === 'Buffer' && isArray(subject.data)) - subject = subject.data + } else if (type === 'object' && subject !== null) { + // assume object is array-like + if (subject.type === 'Buffer' && isArray(subject.data)) subject = subject.data length = +subject.length } else { throw new TypeError('must start with number, buffer, array or string') } - if (length > kMaxLength) - throw new RangeError('Attempt to allocate Buffer larger than maximum ' + - 'size: 0x' + kMaxLength.toString(16) + ' bytes') + if (length > kMaxLength) { + throw new RangeError('Attempt to allocate Buffer larger than maximum size: 0x' + + kMaxLength.toString(16) + ' bytes') + } - if (length < 0) - length = 0 - else - length >>>= 0 // Coerce to uint32. + if (length < 0) length = 0 + else length >>>= 0 // coerce to uint32 var self = this if (Buffer.TYPED_ARRAY_SUPPORT) { @@ -112,11 +109,13 @@ function Buffer (subject, encoding, noZero) { } else if (isArrayish(subject)) { // Treat array-ish objects as a byte array if (Buffer.isBuffer(subject)) { - for (i = 0; i < length; i++) + for (i = 0; i < length; i++) { self[i] = subject.readUInt8(i) + } } else { - for (i = 0; i < length; i++) + for (i = 0; i < length; i++) { self[i] = ((subject[i] % 256) + 256) % 256 + } } } else if (type === 'string') { self.write(subject, 0, encoding) @@ -126,15 +125,13 @@ function Buffer (subject, encoding, noZero) { } } - if (length > 0 && length <= Buffer.poolSize) - self.parent = rootParent + if (length > 0 && length <= Buffer.poolSize) self.parent = rootParent return self } function SlowBuffer (subject, encoding, noZero) { - if (!(this instanceof SlowBuffer)) - return new SlowBuffer(subject, encoding, noZero) + if (!(this instanceof SlowBuffer)) return new SlowBuffer(subject, encoding, noZero) var buf = new Buffer(subject, encoding, noZero) delete buf.parent @@ -146,8 +143,9 @@ Buffer.isBuffer = function (b) { } Buffer.compare = function (a, b) { - if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) + if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { throw new TypeError('Arguments must be Buffers') + } if (a === b) return 0 @@ -281,8 +279,7 @@ Buffer.prototype.toString = function (encoding, start, end) { return utf16leSlice(this, start, end) default: - if (loweredCase) - throw new TypeError('Unknown encoding: ' + encoding) + if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) encoding = (encoding + '').toLowerCase() loweredCase = true } @@ -300,8 +297,7 @@ Buffer.prototype.inspect = function () { var max = exports.INSPECT_MAX_BYTES if (this.length > 0) { str = this.toString('hex', 0, max).match(/.{2}/g).join(' ') - if (this.length > max) - str += ' ... ' + if (this.length > max) str += ' ... ' } return '' } @@ -392,8 +388,9 @@ Buffer.prototype.write = function (string, offset, length, encoding) { offset = Number(offset) || 0 - if (length < 0 || offset < 0 || offset > this.length) + if (length < 0 || offset < 0 || offset > this.length) { throw new RangeError('attempt to write outside buffer bounds') + } var remaining = this.length - offset if (!length) { @@ -517,22 +514,19 @@ Buffer.prototype.slice = function (start, end) { if (start < 0) { start += len - if (start < 0) - start = 0 + if (start < 0) start = 0 } else if (start > len) { start = len } if (end < 0) { end += len - if (end < 0) - end = 0 + if (end < 0) end = 0 } else if (end > len) { end = len } - if (end < start) - end = start + if (end < start) end = start var newBuf if (Buffer.TYPED_ARRAY_SUPPORT) { @@ -545,8 +539,7 @@ Buffer.prototype.slice = function (start, end) { } } - if (newBuf.length) - newBuf.parent = this.parent || this + if (newBuf.length) newBuf.parent = this.parent || this return newBuf } @@ -555,23 +548,21 @@ Buffer.prototype.slice = function (start, end) { * Need to make sure that buffer isn't trying to write out of bounds. */ function checkOffset (offset, ext, length) { - if ((offset % 1) !== 0 || offset < 0) - throw new RangeError('offset is not uint') - if (offset + ext > length) - throw new RangeError('Trying to access beyond buffer length') + if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') + if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') } Buffer.prototype.readUIntLE = function (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 - if (!noAssert) - checkOffset(offset, byteLength, this.length) + if (!noAssert) checkOffset(offset, byteLength, this.length) var val = this[offset] var mul = 1 var i = 0 - while (++i < byteLength && (mul *= 0x100)) + while (++i < byteLength && (mul *= 0x100)) { val += this[offset + i] * mul + } return val } @@ -579,38 +570,36 @@ Buffer.prototype.readUIntLE = function (offset, byteLength, noAssert) { Buffer.prototype.readUIntBE = function (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 - if (!noAssert) + if (!noAssert) { checkOffset(offset, byteLength, this.length) + } var val = this[offset + --byteLength] var mul = 1 - while (byteLength > 0 && (mul *= 0x100)) + while (byteLength > 0 && (mul *= 0x100)) { val += this[offset + --byteLength] * mul + } return val } Buffer.prototype.readUInt8 = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 1, this.length) + if (!noAssert) checkOffset(offset, 1, this.length) return this[offset] } Buffer.prototype.readUInt16LE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 2, this.length) + if (!noAssert) checkOffset(offset, 2, this.length) return this[offset] | (this[offset + 1] << 8) } Buffer.prototype.readUInt16BE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 2, this.length) + if (!noAssert) checkOffset(offset, 2, this.length) return (this[offset] << 8) | this[offset + 1] } Buffer.prototype.readUInt32LE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 4, this.length) + if (!noAssert) checkOffset(offset, 4, this.length) return ((this[offset]) | (this[offset + 1] << 8) | @@ -619,30 +608,28 @@ Buffer.prototype.readUInt32LE = function (offset, noAssert) { } Buffer.prototype.readUInt32BE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 4, this.length) + if (!noAssert) checkOffset(offset, 4, this.length) return (this[offset] * 0x1000000) + - ((this[offset + 1] << 16) | - (this[offset + 2] << 8) | - this[offset + 3]) + ((this[offset + 1] << 16) | + (this[offset + 2] << 8) | + this[offset + 3]) } Buffer.prototype.readIntLE = function (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 - if (!noAssert) - checkOffset(offset, byteLength, this.length) + if (!noAssert) checkOffset(offset, byteLength, this.length) var val = this[offset] var mul = 1 var i = 0 - while (++i < byteLength && (mul *= 0x100)) + while (++i < byteLength && (mul *= 0x100)) { val += this[offset + i] * mul + } mul *= 0x80 - if (val >= mul) - val -= Math.pow(2, 8 * byteLength) + if (val >= mul) val -= Math.pow(2, 8 * byteLength) return val } @@ -650,85 +637,74 @@ Buffer.prototype.readIntLE = function (offset, byteLength, noAssert) { Buffer.prototype.readIntBE = function (offset, byteLength, noAssert) { offset = offset >>> 0 byteLength = byteLength >>> 0 - if (!noAssert) - checkOffset(offset, byteLength, this.length) + if (!noAssert) checkOffset(offset, byteLength, this.length) var i = byteLength var mul = 1 var val = this[offset + --i] - while (i > 0 && (mul *= 0x100)) + while (i > 0 && (mul *= 0x100)) { val += this[offset + --i] * mul + } mul *= 0x80 - if (val >= mul) - val -= Math.pow(2, 8 * byteLength) + if (val >= mul) val -= Math.pow(2, 8 * byteLength) return val } Buffer.prototype.readInt8 = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 1, this.length) - if (!(this[offset] & 0x80)) - return (this[offset]) + if (!noAssert) checkOffset(offset, 1, this.length) + if (!(this[offset] & 0x80)) return (this[offset]) return ((0xff - this[offset] + 1) * -1) } Buffer.prototype.readInt16LE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 2, this.length) + if (!noAssert) checkOffset(offset, 2, this.length) var val = this[offset] | (this[offset + 1] << 8) return (val & 0x8000) ? val | 0xFFFF0000 : val } Buffer.prototype.readInt16BE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 2, this.length) + if (!noAssert) checkOffset(offset, 2, this.length) var val = this[offset + 1] | (this[offset] << 8) return (val & 0x8000) ? val | 0xFFFF0000 : val } Buffer.prototype.readInt32LE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 4, this.length) + if (!noAssert) checkOffset(offset, 4, this.length) return (this[offset]) | - (this[offset + 1] << 8) | - (this[offset + 2] << 16) | - (this[offset + 3] << 24) + (this[offset + 1] << 8) | + (this[offset + 2] << 16) | + (this[offset + 3] << 24) } Buffer.prototype.readInt32BE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 4, this.length) + if (!noAssert) checkOffset(offset, 4, this.length) return (this[offset] << 24) | - (this[offset + 1] << 16) | - (this[offset + 2] << 8) | - (this[offset + 3]) + (this[offset + 1] << 16) | + (this[offset + 2] << 8) | + (this[offset + 3]) } Buffer.prototype.readFloatLE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 4, this.length) + if (!noAssert) checkOffset(offset, 4, this.length) return ieee754.read(this, offset, true, 23, 4) } Buffer.prototype.readFloatBE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 4, this.length) + if (!noAssert) checkOffset(offset, 4, this.length) return ieee754.read(this, offset, false, 23, 4) } Buffer.prototype.readDoubleLE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 8, this.length) + if (!noAssert) checkOffset(offset, 8, this.length) return ieee754.read(this, offset, true, 52, 8) } Buffer.prototype.readDoubleBE = function (offset, noAssert) { - if (!noAssert) - checkOffset(offset, 8, this.length) + if (!noAssert) checkOffset(offset, 8, this.length) return ieee754.read(this, offset, false, 52, 8) } @@ -742,14 +718,14 @@ Buffer.prototype.writeUIntLE = function (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 byteLength = byteLength >>> 0 - if (!noAssert) - checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) + if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) var mul = 1 var i = 0 this[offset] = value & 0xFF - while (++i < byteLength && (mul *= 0x100)) + while (++i < byteLength && (mul *= 0x100)) { this[offset + i] = (value / mul) >>> 0 & 0xFF + } return offset + byteLength } @@ -758,14 +734,14 @@ Buffer.prototype.writeUIntBE = function (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 byteLength = byteLength >>> 0 - if (!noAssert) - checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) + if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) var i = byteLength - 1 var mul = 1 this[offset + i] = value & 0xFF - while (--i >= 0 && (mul *= 0x100)) + while (--i >= 0 && (mul *= 0x100)) { this[offset + i] = (value / mul) >>> 0 & 0xFF + } return offset + byteLength } @@ -773,8 +749,7 @@ Buffer.prototype.writeUIntBE = function (value, offset, byteLength, noAssert) { Buffer.prototype.writeUInt8 = function (value, offset, noAssert) { value = +value offset = offset >>> 0 - if (!noAssert) - checkInt(this, value, offset, 1, 0xff, 0) + if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) this[offset] = value return offset + 1 @@ -791,24 +766,26 @@ function objectWriteUInt16 (buf, value, offset, littleEndian) { Buffer.prototype.writeUInt16LE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 - if (!noAssert) - checkInt(this, value, offset, 2, 0xffff, 0) + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = value this[offset + 1] = (value >>> 8) - } else objectWriteUInt16(this, value, offset, true) + } else { + objectWriteUInt16(this, value, offset, true) + } return offset + 2 } Buffer.prototype.writeUInt16BE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 - if (!noAssert) - checkInt(this, value, offset, 2, 0xffff, 0) + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = (value >>> 8) this[offset + 1] = value - } else objectWriteUInt16(this, value, offset, false) + } else { + objectWriteUInt16(this, value, offset, false) + } return offset + 2 } @@ -822,28 +799,30 @@ function objectWriteUInt32 (buf, value, offset, littleEndian) { Buffer.prototype.writeUInt32LE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 - if (!noAssert) - checkInt(this, value, offset, 4, 0xffffffff, 0) + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset + 3] = (value >>> 24) this[offset + 2] = (value >>> 16) this[offset + 1] = (value >>> 8) this[offset] = value - } else objectWriteUInt32(this, value, offset, true) + } else { + objectWriteUInt32(this, value, offset, true) + } return offset + 4 } Buffer.prototype.writeUInt32BE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 - if (!noAssert) - checkInt(this, value, offset, 4, 0xffffffff, 0) + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = (value >>> 24) this[offset + 1] = (value >>> 16) this[offset + 2] = (value >>> 8) this[offset + 3] = value - } else objectWriteUInt32(this, value, offset, false) + } else { + objectWriteUInt32(this, value, offset, false) + } return offset + 4 } @@ -851,20 +830,20 @@ Buffer.prototype.writeIntLE = function (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) { - checkInt(this, - value, - offset, - byteLength, - Math.pow(2, 8 * byteLength - 1) - 1, - -Math.pow(2, 8 * byteLength - 1)) + checkInt( + this, value, offset, byteLength, + Math.pow(2, 8 * byteLength - 1) - 1, + -Math.pow(2, 8 * byteLength - 1) + ) } var i = 0 var mul = 1 var sub = value < 0 ? 1 : 0 this[offset] = value & 0xFF - while (++i < byteLength && (mul *= 0x100)) + while (++i < byteLength && (mul *= 0x100)) { this[offset + i] = ((value / mul) >> 0) - sub & 0xFF + } return offset + byteLength } @@ -873,20 +852,20 @@ Buffer.prototype.writeIntBE = function (value, offset, byteLength, noAssert) { value = +value offset = offset >>> 0 if (!noAssert) { - checkInt(this, - value, - offset, - byteLength, - Math.pow(2, 8 * byteLength - 1) - 1, - -Math.pow(2, 8 * byteLength - 1)) + checkInt( + this, value, offset, byteLength, + Math.pow(2, 8 * byteLength - 1) - 1, + -Math.pow(2, 8 * byteLength - 1) + ) } var i = byteLength - 1 var mul = 1 var sub = value < 0 ? 1 : 0 this[offset + i] = value & 0xFF - while (--i >= 0 && (mul *= 0x100)) + while (--i >= 0 && (mul *= 0x100)) { this[offset + i] = ((value / mul) >> 0) - sub & 0xFF + } return offset + byteLength } @@ -894,8 +873,7 @@ Buffer.prototype.writeIntBE = function (value, offset, byteLength, noAssert) { Buffer.prototype.writeInt8 = function (value, offset, noAssert) { value = +value offset = offset >>> 0 - if (!noAssert) - checkInt(this, value, offset, 1, 0x7f, -0x80) + if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value) if (value < 0) value = 0xff + value + 1 this[offset] = value @@ -905,53 +883,57 @@ Buffer.prototype.writeInt8 = function (value, offset, noAssert) { Buffer.prototype.writeInt16LE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 - if (!noAssert) - checkInt(this, value, offset, 2, 0x7fff, -0x8000) + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = value this[offset + 1] = (value >>> 8) - } else objectWriteUInt16(this, value, offset, true) + } else { + objectWriteUInt16(this, value, offset, true) + } return offset + 2 } Buffer.prototype.writeInt16BE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 - if (!noAssert) - checkInt(this, value, offset, 2, 0x7fff, -0x8000) + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = (value >>> 8) this[offset + 1] = value - } else objectWriteUInt16(this, value, offset, false) + } else { + objectWriteUInt16(this, value, offset, false) + } return offset + 2 } Buffer.prototype.writeInt32LE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 - if (!noAssert) - checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = value this[offset + 1] = (value >>> 8) this[offset + 2] = (value >>> 16) this[offset + 3] = (value >>> 24) - } else objectWriteUInt32(this, value, offset, true) + } else { + objectWriteUInt32(this, value, offset, true) + } return offset + 4 } Buffer.prototype.writeInt32BE = function (value, offset, noAssert) { value = +value offset = offset >>> 0 - if (!noAssert) - checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) if (value < 0) value = 0xffffffff + value + 1 if (Buffer.TYPED_ARRAY_SUPPORT) { this[offset] = (value >>> 24) this[offset + 1] = (value >>> 16) this[offset + 2] = (value >>> 8) this[offset + 3] = value - } else objectWriteUInt32(this, value, offset, false) + } else { + objectWriteUInt32(this, value, offset, false) + } return offset + 4 } @@ -962,8 +944,9 @@ function checkIEEE754 (buf, value, offset, ext, max, min) { } function writeFloat (buf, value, offset, littleEndian, noAssert) { - if (!noAssert) + if (!noAssert) { checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) + } ieee754.write(buf, value, offset, littleEndian, 23, 4) return offset + 4 } @@ -977,8 +960,9 @@ Buffer.prototype.writeFloatBE = function (value, offset, noAssert) { } function writeDouble (buf, value, offset, littleEndian, noAssert) { - if (!noAssert) + if (!noAssert) { checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) + } ieee754.write(buf, value, offset, littleEndian, 52, 8) return offset + 8 } @@ -1006,16 +990,17 @@ Buffer.prototype.copy = function (target, target_start, start, end) { if (target.length === 0 || self.length === 0) return 0 // Fatal error conditions - if (target_start < 0) + if (target_start < 0) { throw new RangeError('targetStart out of bounds') + } if (start < 0 || start >= self.length) throw new RangeError('sourceStart out of bounds') if (end < 0) throw new RangeError('sourceEnd out of bounds') // Are we oob? - if (end > this.length) - end = this.length - if (target.length - target_start < end - start) + if (end > this.length) end = this.length + if (target.length - target_start < end - start) { end = target.length - target_start + start + } var len = end - start @@ -1294,8 +1279,7 @@ function base64ToBytes (str) { function blitBuffer (src, dst, offset, length) { for (var i = 0; i < length; i++) { - if ((i + offset >= dst.length) || (i >= src.length)) - break + if ((i + offset >= dst.length) || (i >= src.length)) break dst[i + offset] = src[i] } return i diff --git a/test/write.js b/test/write.js index e396436..e59c047 100644 --- a/test/write.js +++ b/test/write.js @@ -107,13 +107,8 @@ test('hex of write{Uint,Int}{8,16,32}{LE,BE} with overflow', function (t) { next.writeInt32BE(~0, 0) var readfn = 'read' + x + y + z var r = reads.shift() - if (isnan(r)) - t.pass('equal') - else - t.equal( - v1[readfn](0, true), - r - ) + if (isnan(r)) t.pass('equal') + else t.equal(v1[readfn](0, true), r) } } }