GeezerVison is showing github.com — doc: buffer.fill empty value · nodejs/node@108220c
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: doc/api/buffer.md +13Lines changed: 13 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1882,6 +1882,7 @@ changes: |
1882 | 1882 | --> |
1883 | 1883 |
|
1884 | 1884 | * `value` {string|Buffer|Uint8Array|integer} The value with which to fill `buf`. |
| 1885 | + Empty value (string, Uint8Array, Buffer) is coerced to `0`. |
1885 | 1886 | * `offset` {integer} Number of bytes to skip before starting to fill `buf`. |
1886 | 1887 | **Default:** `0`. |
1887 | 1888 | * `end` {integer} Where to stop filling `buf` (not inclusive). **Default:** |
@@ -1902,6 +1903,12 @@ const b = Buffer.allocUnsafe(50).fill('h'); |
1902 | 1903 |
|
1903 | 1904 | console.log(b.toString()); |
1904 | 1905 | // 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> |
1905 | 1912 | ``` |
1906 | 1913 |
|
1907 | 1914 | ```cjs |
@@ -1913,6 +1920,12 @@ const b = Buffer.allocUnsafe(50).fill('h'); |
1913 | 1920 |
|
1914 | 1921 | console.log(b.toString()); |
1915 | 1922 | // 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> |
1916 | 1929 | ``` |
1917 | 1930 |
|
1918 | 1931 | `value` is coerced to a `uint32` value if it is not a string, `Buffer`, or |
|
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-buffer-fill.js +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -429,3 +429,18 @@ assert.throws(() => { |
429 | 429 | code: 'ERR_INVALID_ARG_VALUE', |
430 | 430 | name: 'TypeError' |
431 | 431 | }); |
| 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 | +} |