Skip to content

Commit

Permalink
buffer: alias UInt ➡️ Uint in buffer methods
Browse files Browse the repository at this point in the history
It’s *so* hard to remember that it’s `UintXArray` but not
`(write|read)UintX`. Let’s fix that by just providing aliases. 😊

PR-URL: #34729
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
addaleax committed Sep 22, 2020
1 parent 987e0cb commit 9d07217
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
42 changes: 42 additions & 0 deletions doc/api/buffer.md
Expand Up @@ -1705,6 +1705,9 @@ console.log(buf.readIntLE(0, 6).toString(16));
<!-- YAML
added: v0.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.readUint8()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand Down Expand Up @@ -1732,6 +1735,9 @@ console.log(buf.readUInt8(2));
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.readUint16BE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand All @@ -1758,6 +1764,9 @@ console.log(buf.readUInt16BE(1).toString(16));
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.readUint16LE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand Down Expand Up @@ -1786,6 +1795,9 @@ console.log(buf.readUInt16LE(2).toString(16));
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.readUint32BE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand All @@ -1810,6 +1822,9 @@ console.log(buf.readUInt32BE(0).toString(16));
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.readUint32LE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand All @@ -1836,6 +1851,9 @@ console.log(buf.readUInt32LE(1).toString(16));
<!-- YAML
added: v0.11.15
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.readUintBE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand Down Expand Up @@ -1865,6 +1883,9 @@ console.log(buf.readUIntBE(1, 6).toString(16));
<!-- YAML
added: v0.11.15
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.readUintLE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand Down Expand Up @@ -2643,6 +2664,9 @@ console.log(buf);
<!-- YAML
added: v0.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.writeUint8()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand Down Expand Up @@ -2674,6 +2698,9 @@ console.log(buf);
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.writeUint16BE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand Down Expand Up @@ -2703,6 +2730,9 @@ console.log(buf);
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.writeUint16LE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand Down Expand Up @@ -2732,6 +2762,9 @@ console.log(buf);
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.writeUint32BE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand Down Expand Up @@ -2760,6 +2793,9 @@ console.log(buf);
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.writeUint32LE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand Down Expand Up @@ -2788,6 +2824,9 @@ console.log(buf);
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.writeUintBE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand Down Expand Up @@ -2818,6 +2857,9 @@ console.log(buf);
<!-- YAML
added: v0.5.5
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/34729
description: This function is also available as `buf.writeUintLE()`.
- version: v10.0.0
pr-url: https://github.com/nodejs/node/pull/18395
description: Removed `noAssert` and no implicit coercion of the offset
Expand Down
14 changes: 14 additions & 0 deletions lib/internal/buffer.js
Expand Up @@ -961,6 +961,13 @@ function addBufferPrototypeMethods(proto) {
proto.readUIntBE = readUIntBE;
proto.readUInt32BE = readUInt32BE;
proto.readUInt16BE = readUInt16BE;
proto.readUintLE = readUIntLE;
proto.readUint32LE = readUInt32LE;
proto.readUint16LE = readUInt16LE;
proto.readUint8 = readUInt8;
proto.readUintBE = readUIntBE;
proto.readUint32BE = readUInt32BE;
proto.readUint16BE = readUInt16BE;
proto.readIntLE = readIntLE;
proto.readInt32LE = readInt32LE;
proto.readInt16LE = readInt16LE;
Expand All @@ -976,6 +983,13 @@ function addBufferPrototypeMethods(proto) {
proto.writeUIntBE = writeUIntBE;
proto.writeUInt32BE = writeUInt32BE;
proto.writeUInt16BE = writeUInt16BE;
proto.writeUintLE = writeUIntLE;
proto.writeUint32LE = writeUInt32LE;
proto.writeUint16LE = writeUInt16LE;
proto.writeUint8 = writeUInt8;
proto.writeUintBE = writeUIntBE;
proto.writeUint32BE = writeUInt32BE;
proto.writeUint16BE = writeUInt16BE;
proto.writeIntLE = writeIntLE;
proto.writeInt32LE = writeInt32LE;
proto.writeInt16LE = writeInt16LE;
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-buffer-writeuint.js
Expand Up @@ -220,3 +220,12 @@ const assert = require('assert');
val *= 0x100;
}
}

for (const fn of [
'UInt8', 'UInt16LE', 'UInt16BE', 'UInt32LE', 'UInt32BE', 'UIntLE', 'UIntBE'
]) {
const p = Buffer.prototype;
const lowerFn = fn.replace(/UInt/, 'Uint');
assert.strictEqual(p[`write${fn}`], p[`write${lowerFn}`]);
assert.strictEqual(p[`read${fn}`], p[`read${lowerFn}`]);
}

0 comments on commit 9d07217

Please sign in to comment.