Skip to content

Commit 21a94ab

Browse files
dcposchMylesBorins
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 6584ea0 commit 21a94ab

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
@@ -1156,6 +1156,30 @@ console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2'));
11561156
console.log(utf16Buffer.indexOf('\u03a3', -4, 'ucs2'));
11571157
```
11581158

1159+
If `value` is not a string, number, or `Buffer`, this method will throw a
1160+
`TypeError`. If `value` is a number, it will be coerced to a valid byte value,
1161+
an integer between 0 and 255.
1162+
1163+
If `byteOffset` is not a number, it will be coerced to a number. Any arguments
1164+
that coerce to `NaN` or 0, like `{}`, `[]`, `null` or `undefined`, will search
1165+
the whole buffer. This behavior matches [`String#indexOf()`].
1166+
1167+
```js
1168+
const b = Buffer.from('abcdef');
1169+
1170+
// Passing a value that's a number, but not a valid byte
1171+
// Prints: 2, equivalent to searching for 99 or 'c'
1172+
console.log(b.indexOf(99.9));
1173+
console.log(b.indexOf(256 + 99));
1174+
1175+
// Passing a byteOffset that coerces to NaN or 0
1176+
// Prints: 1, searching the whole buffer
1177+
console.log(b.indexOf('b', undefined));
1178+
console.log(b.indexOf('b', {}));
1179+
console.log(b.indexOf('b', null));
1180+
console.log(b.indexOf('b', []));
1181+
```
1182+
11591183
### buf.includes(value[, byteOffset][, encoding])
11601184
<!-- YAML
11611185
added: v5.3.0
@@ -1276,6 +1300,33 @@ console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'ucs2'));
12761300
console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'ucs2'));
12771301
```
12781302

1303+
If `value` is not a string, number, or `Buffer`, this method will throw a
1304+
`TypeError`. If `value` is a number, it will be coerced to a valid byte value,
1305+
an integer between 0 and 255.
1306+
1307+
If `byteOffset` is not a number, it will be coerced to a number. Any arguments
1308+
that coerce to `NaN`, like `{}` or `undefined`, will search the whole buffer.
1309+
This behavior matches [`String#lastIndexOf()`].
1310+
1311+
```js
1312+
const b = Buffer.from('abcdef');
1313+
1314+
// Passing a value that's a number, but not a valid byte
1315+
// Prints: 2, equivalent to searching for 99 or 'c'
1316+
console.log(b.lastIndexOf(99.9));
1317+
console.log(b.lastIndexOf(256 + 99));
1318+
1319+
// Passing a byteOffset that coerces to NaN
1320+
// Prints: 1, searching the whole buffer
1321+
console.log(b.lastIndexOf('b', undefined));
1322+
console.log(b.lastIndexOf('b', {}));
1323+
1324+
// Passing a byteOffset that coerces to 0
1325+
// Prints: -1, equivalent to passing 0
1326+
console.log(b.lastIndexOf('b', null));
1327+
console.log(b.lastIndexOf('b', []));
1328+
```
1329+
12791330
### buf.length
12801331
<!-- YAML
12811332
added: v0.1.90
@@ -2413,6 +2464,8 @@ console.log(buf);
24132464
[RFC1345]: https://tools.ietf.org/html/rfc1345
24142465
[RFC4648, Section 5]: https://tools.ietf.org/html/rfc4648#section-5
24152466
[`String.prototype.length`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
2467+
[`String#indexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
2468+
[`String#lastIndexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
24162469
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
24172470
[`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
24182471
[`Uint32Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array

0 commit comments

Comments
 (0)