From a4f383f4cbf0ea6ab73c72deab2efd43bab638b1 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Fri, 9 Dec 2022 11:02:42 +0100 Subject: [PATCH] doc: buffer.fill empty value --- doc/api/buffer.md | 15 ++++++++++++++- test/parallel/test-buffer-fill.js | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 2fda3cf23b95a8..ab665779f1e440 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -1884,7 +1884,8 @@ changes: --> * `value` {string|Buffer|Uint8Array|integer} The value with which to fill `buf`. -* `offset` {integer} Number of bytes to skip before starting to fill `buf`. + Empty value (string, Uint8Array, Buffer) is coerced to `0`. +* `offset` {integer} Number of bytes to skip before starting to fill `buf` **Default:** `0`. * `end` {integer} Where to stop filling `buf` (not inclusive). **Default:** [`buf.length`][]. @@ -1904,6 +1905,12 @@ const b = Buffer.allocUnsafe(50).fill('h'); console.log(b.toString()); // Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh + +// Fill a buffer with empty string +const c = Buffer.allocUnsafe(5).fill(''); + +console.log(c.fill('')); +// Prints: ``` ```cjs @@ -1915,6 +1922,12 @@ const b = Buffer.allocUnsafe(50).fill('h'); console.log(b.toString()); // Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh + +// Fill a buffer with empty string +const c = Buffer.allocUnsafe(5).fill(''); + +console.log(c.fill('')); +// Prints: ``` `value` is coerced to a `uint32` value if it is not a string, `Buffer`, or diff --git a/test/parallel/test-buffer-fill.js b/test/parallel/test-buffer-fill.js index e91492a59e81b3..c3476461fdaf8f 100644 --- a/test/parallel/test-buffer-fill.js +++ b/test/parallel/test-buffer-fill.js @@ -429,3 +429,18 @@ assert.throws(() => { code: 'ERR_INVALID_ARG_VALUE', name: 'TypeError' }); + + +{ + const bufEmptyString = Buffer.alloc(5, ''); + assert.strictEqual(bufEmptyString.toString(), '\x00\x00\x00\x00\x00'); + + const bufEmptyArray = Buffer.alloc(5, []); + assert.strictEqual(bufEmptyArray.toString(), '\x00\x00\x00\x00\x00'); + + const bufEmptyBuffer = Buffer.alloc(5, Buffer.alloc(5)); + assert.strictEqual(bufEmptyBuffer.toString(), '\x00\x00\x00\x00\x00'); + + const bufZero = Buffer.alloc(5, 0); + assert.strictEqual(bufZero.toString(), '\x00\x00\x00\x00\x00'); +}