Skip to content

Commit

Permalink
doc: buffer.fill empty value
Browse files Browse the repository at this point in the history
PR-URL: #45794
Fixes: #45727
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
  • Loading branch information
marco-ippolito authored and danielleadams committed Jan 4, 2023
1 parent 1e40024 commit a17b6de
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,7 @@ changes:
-->

* `value` {string|Buffer|Uint8Array|integer} The value with which 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:**
Expand All @@ -1902,6 +1903,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: <Buffer 00 00 00 00 00>
```

```cjs
Expand All @@ -1913,6 +1920,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: <Buffer 00 00 00 00 00>
```

`value` is coerced to a `uint32` value if it is not a string, `Buffer`, or
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-buffer-fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

0 comments on commit a17b6de

Please sign in to comment.