Skip to content

Commit d0112fc

Browse files
dcposchsilverwind
authored andcommitted
doc: clarify Buffer.indexOf/lastIndexOf edge cases
PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent dd50cd9 commit d0112fc

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

doc/api/buffer.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,30 @@ console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2'));
11641164
console.log(utf16Buffer.indexOf('\u03a3', -4, 'ucs2'));
11651165
```
11661166

1167+
If `value` is not a string, number, or `Buffer`, this method will throw a
1168+
`TypeError`. If `value` is a number, it will be coerced to a valid byte value,
1169+
an integer between 0 and 255.
1170+
1171+
If `byteOffset` is not a number, it will be coerced to a number. Any arguments
1172+
that coerce to `NaN` or 0, like `{}`, `[]`, `null` or `undefined`, will search
1173+
the whole buffer. This behavior matches [`String#indexOf()`].
1174+
1175+
```js
1176+
const b = Buffer.from('abcdef');
1177+
1178+
// Passing a value that's a number, but not a valid byte
1179+
// Prints: 2, equivalent to searching for 99 or 'c'
1180+
console.log(b.indexOf(99.9));
1181+
console.log(b.indexOf(256 + 99));
1182+
1183+
// Passing a byteOffset that coerces to NaN or 0
1184+
// Prints: 1, searching the whole buffer
1185+
console.log(b.indexOf('b', undefined));
1186+
console.log(b.indexOf('b', {}));
1187+
console.log(b.indexOf('b', null));
1188+
console.log(b.indexOf('b', []));
1189+
```
1190+
11671191
### buf.includes(value[, byteOffset][, encoding])
11681192
<!-- YAML
11691193
added: v5.3.0
@@ -1284,6 +1308,33 @@ console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'ucs2'));
12841308
console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'ucs2'));
12851309
```
12861310

1311+
If `value` is not a string, number, or `Buffer`, this method will throw a
1312+
`TypeError`. If `value` is a number, it will be coerced to a valid byte value,
1313+
an integer between 0 and 255.
1314+
1315+
If `byteOffset` is not a number, it will be coerced to a number. Any arguments
1316+
that coerce to `NaN`, like `{}` or `undefined`, will search the whole buffer.
1317+
This behavior matches [`String#lastIndexOf()`].
1318+
1319+
```js
1320+
const b = Buffer.from('abcdef');
1321+
1322+
// Passing a value that's a number, but not a valid byte
1323+
// Prints: 2, equivalent to searching for 99 or 'c'
1324+
console.log(b.lastIndexOf(99.9));
1325+
console.log(b.lastIndexOf(256 + 99));
1326+
1327+
// Passing a byteOffset that coerces to NaN
1328+
// Prints: 1, searching the whole buffer
1329+
console.log(b.lastIndexOf('b', undefined));
1330+
console.log(b.lastIndexOf('b', {}));
1331+
1332+
// Passing a byteOffset that coerces to 0
1333+
// Prints: -1, equivalent to passing 0
1334+
console.log(b.lastIndexOf('b', null));
1335+
console.log(b.lastIndexOf('b', []));
1336+
```
1337+
12871338
### buf.length
12881339
<!-- YAML
12891340
added: v0.1.90
@@ -2463,6 +2514,8 @@ console.log(buf);
24632514
[RFC1345]: https://tools.ietf.org/html/rfc1345
24642515
[RFC4648, Section 5]: https://tools.ietf.org/html/rfc4648#section-5
24652516
[`String.prototype.length`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
2517+
[`String#indexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
2518+
[`String#lastIndexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
24662519
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
24672520
[`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
24682521
[`Uint32Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array

0 commit comments

Comments
 (0)