Skip to content

Commit 108220c

Browse files
marco-ippolitodanielleadams
authored andcommitted
doc: buffer.fill empty value
PR-URL: #45794 Fixes: #45727 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
1 parent 24e2a4f commit 108220c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

doc/api/buffer.md

+13
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,7 @@ changes:
18821882
-->
18831883

18841884
* `value` {string|Buffer|Uint8Array|integer} The value with which to fill `buf`.
1885+
Empty value (string, Uint8Array, Buffer) is coerced to `0`.
18851886
* `offset` {integer} Number of bytes to skip before starting to fill `buf`.
18861887
**Default:** `0`.
18871888
* `end` {integer} Where to stop filling `buf` (not inclusive). **Default:**
@@ -1902,6 +1903,12 @@ const b = Buffer.allocUnsafe(50).fill('h');
19021903

19031904
console.log(b.toString());
19041905
// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
1906+
1907+
// Fill a buffer with empty string
1908+
const c = Buffer.allocUnsafe(5).fill('');
1909+
1910+
console.log(c.fill(''));
1911+
// Prints: <Buffer 00 00 00 00 00>
19051912
```
19061913

19071914
```cjs
@@ -1913,6 +1920,12 @@ const b = Buffer.allocUnsafe(50).fill('h');
19131920

19141921
console.log(b.toString());
19151922
// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
1923+
1924+
// Fill a buffer with empty string
1925+
const c = Buffer.allocUnsafe(5).fill('');
1926+
1927+
console.log(c.fill(''));
1928+
// Prints: <Buffer 00 00 00 00 00>
19161929
```
19171930

19181931
`value` is coerced to a `uint32` value if it is not a string, `Buffer`, or

test/parallel/test-buffer-fill.js

+15
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,18 @@ assert.throws(() => {
429429
code: 'ERR_INVALID_ARG_VALUE',
430430
name: 'TypeError'
431431
});
432+
433+
434+
{
435+
const bufEmptyString = Buffer.alloc(5, '');
436+
assert.strictEqual(bufEmptyString.toString(), '\x00\x00\x00\x00\x00');
437+
438+
const bufEmptyArray = Buffer.alloc(5, []);
439+
assert.strictEqual(bufEmptyArray.toString(), '\x00\x00\x00\x00\x00');
440+
441+
const bufEmptyBuffer = Buffer.alloc(5, Buffer.alloc(5));
442+
assert.strictEqual(bufEmptyBuffer.toString(), '\x00\x00\x00\x00\x00');
443+
444+
const bufZero = Buffer.alloc(5, 0);
445+
assert.strictEqual(bufZero.toString(), '\x00\x00\x00\x00\x00');
446+
}

0 commit comments

Comments
 (0)