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: buffer.fill empty value #45794

Merged
merged 2 commits into from Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/api/buffer.md
Expand Up @@ -1884,6 +1884,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 @@ -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: <Buffer 00 00 00 00 00>
```

```cjs
Expand All @@ -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: <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
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');
}