Skip to content

Commit 89b66dc

Browse files
committed
doc,test: args to buffer.copy can be Uint8Arrays
This was semi-overlooked in #10236 because it always worked and didn’t need additional changes. Ref: #10236 PR-URL: #11486 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent b395ed9 commit 89b66dc

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

doc/api/buffer.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ A `RangeError` will be thrown if: `targetStart < 0`, `sourceStart < 0`,
964964
added: v0.1.90
965965
-->
966966

967-
* `target` {Buffer} A `Buffer` to copy into.
967+
* `target` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`] to copy into.
968968
* `targetStart` {Integer} The offset within `target` at which to begin
969969
copying to. **Default:** `0`
970970
* `sourceStart` {Integer} The offset within `buf` at which to begin copying from.

test/parallel/test-buffer-copy.js

+26
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,29 @@ assert.strictEqual(b.copy(c, 0, 100, 10), 0);
118118

119119
// when targetStart > targetLength, zero copied
120120
assert.strictEqual(b.copy(c, 512, 0, 10), 0);
121+
122+
// Test that the `target` can be a Uint8Array.
123+
{
124+
const d = new Uint8Array(c);
125+
// copy 512 bytes, from 0 to 512.
126+
b.fill(++cntr);
127+
d.fill(++cntr);
128+
const copied = b.copy(d, 0, 0, 512);
129+
assert.strictEqual(512, copied);
130+
for (let i = 0; i < d.length; i++) {
131+
assert.strictEqual(b[i], d[i]);
132+
}
133+
}
134+
135+
// Test that the source can be a Uint8Array, too.
136+
{
137+
const e = new Uint8Array(b);
138+
// copy 512 bytes, from 0 to 512.
139+
e.fill(++cntr);
140+
c.fill(++cntr);
141+
const copied = Buffer.prototype.copy.call(e, c, 0, 0, 512);
142+
assert.strictEqual(512, copied);
143+
for (let i = 0; i < c.length; i++) {
144+
assert.strictEqual(e[i], c[i]);
145+
}
146+
}

0 commit comments

Comments
 (0)