Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc,test: args to buffer.copy can be Uint8Arrays #11486

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ A `RangeError` will be thrown if: `targetStart < 0`, `sourceStart < 0`,
added: v0.1.90
-->

* `target` {Buffer} A `Buffer` to copy into.
* `target` {Buffer|Uint8Array} A `Buffer` or [`Uint8Array`] to copy into.
* `targetStart` {Integer} The offset within `target` at which to begin
copying to. **Default:** `0`
* `sourceStart` {Integer} The offset within `buf` at which to begin copying from.
Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-buffer-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,29 @@ assert.strictEqual(b.copy(c, 0, 100, 10), 0);

// when targetStart > targetLength, zero copied
assert.strictEqual(b.copy(c, 512, 0, 10), 0);

// Test that the `target` can be a Uint8Array.
{
const d = new Uint8Array(c);
// copy 512 bytes, from 0 to 512.
b.fill(++cntr);
d.fill(++cntr);
const copied = b.copy(d, 0, 0, 512);
assert.strictEqual(512, copied);
for (let i = 0; i < d.length; i++) {
assert.strictEqual(b[i], d[i]);
}
}

// Test that the source can be a Uint8Array, too.
{
const e = new Uint8Array(b);
// copy 512 bytes, from 0 to 512.
e.fill(++cntr);
c.fill(++cntr);
const copied = Buffer.prototype.copy.call(e, c, 0, 0, 512);
assert.strictEqual(512, copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(e[i], c[i]);
}
}