Skip to content

Commit 8a90f5c

Browse files
marco-ippolitotargos
authored andcommitted
doc: buffer.fill empty value
PR-URL: #45794 Fixes: #45727 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Paolo Insogna <[email protected]>
1 parent 424419c commit 8a90f5c

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
@@ -1884,6 +1884,7 @@ changes:
18841884
-->
18851885

18861886
* `value` {string|Buffer|Uint8Array|integer} The value with which to fill `buf`.
1887+
Empty value (string, Uint8Array, Buffer) is coerced to `0`.
18871888
* `offset` {integer} Number of bytes to skip before starting to fill `buf`.
18881889
**Default:** `0`.
18891890
* `end` {integer} Where to stop filling `buf` (not inclusive). **Default:**
@@ -1904,6 +1905,12 @@ const b = Buffer.allocUnsafe(50).fill('h');
19041905

19051906
console.log(b.toString());
19061907
// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
1908+
1909+
// Fill a buffer with empty string
1910+
const c = Buffer.allocUnsafe(5).fill('');
1911+
1912+
console.log(c.fill(''));
1913+
// Prints: <Buffer 00 00 00 00 00>
19071914
```
19081915

19091916
```cjs
@@ -1915,6 +1922,12 @@ const b = Buffer.allocUnsafe(50).fill('h');
19151922

19161923
console.log(b.toString());
19171924
// Prints: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
1925+
1926+
// Fill a buffer with empty string
1927+
const c = Buffer.allocUnsafe(5).fill('');
1928+
1929+
console.log(c.fill(''));
1930+
// Prints: <Buffer 00 00 00 00 00>
19181931
```
19191932

19201933
`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)