Skip to content

Commit dd60859

Browse files
dcposchitaloacasas
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 e7c953a commit dd60859

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

doc/api/buffer.md

+53
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,30 @@ console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2'));
11511151
console.log(utf16Buffer.indexOf('\u03a3', -4, 'ucs2'));
11521152
```
11531153

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

1298+
If `value` is not a string, number, or `Buffer`, this method will throw a
1299+
`TypeError`. If `value` is a number, it will be coerced to a valid byte value,
1300+
an integer between 0 and 255.
1301+
1302+
If `byteOffset` is not a number, it will be coerced to a number. Any arguments
1303+
that coerce to `NaN`, like `{}` or `undefined`, will search the whole buffer.
1304+
This behavior matches [`String#lastIndexOf()`].
1305+
1306+
```js
1307+
const b = Buffer.from('abcdef');
1308+
1309+
// Passing a value that's a number, but not a valid byte
1310+
// Prints: 2, equivalent to searching for 99 or 'c'
1311+
console.log(b.lastIndexOf(99.9));
1312+
console.log(b.lastIndexOf(256 + 99));
1313+
1314+
// Passing a byteOffset that coerces to NaN
1315+
// Prints: 1, searching the whole buffer
1316+
console.log(b.lastIndexOf('b', undefined));
1317+
console.log(b.lastIndexOf('b', {}));
1318+
1319+
// Passing a byteOffset that coerces to 0
1320+
// Prints: -1, equivalent to passing 0
1321+
console.log(b.lastIndexOf('b', null));
1322+
console.log(b.lastIndexOf('b', []));
1323+
```
1324+
12741325
### buf.length
12751326
<!-- YAML
12761327
added: v0.1.90
@@ -2443,6 +2494,8 @@ console.log(buf);
24432494
[RFC1345]: https://tools.ietf.org/html/rfc1345
24442495
[RFC4648, Section 5]: https://tools.ietf.org/html/rfc4648#section-5
24452496
[`String.prototype.length`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
2497+
[`String#indexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
2498+
[`String#lastIndexOf()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
24462499
[`TypedArray`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
24472500
[`TypedArray.from()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
24482501
[`Uint32Array`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array

0 commit comments

Comments
 (0)