@@ -1164,6 +1164,30 @@ console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2'));
1164
1164
console .log (utf16Buffer .indexOf (' \u03a3 ' , - 4 , ' ucs2' ));
1165
1165
```
1166
1166
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
+
1167
1191
### buf.includes(value[ , byteOffset] [ , encoding ] )
1168
1192
<!-- YAML
1169
1193
added: v5.3.0
@@ -1284,6 +1308,33 @@ console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'ucs2'));
1284
1308
console .log (utf16Buffer .lastIndexOf (' \u03a3 ' , - 5 , ' ucs2' ));
1285
1309
```
1286
1310
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
+
1287
1338
### buf.length
1288
1339
<!-- YAML
1289
1340
added: v0.1.90
@@ -2463,6 +2514,8 @@ console.log(buf);
2463
2514
[ RFC1345 ] : https://tools.ietf.org/html/rfc1345
2464
2515
[ RFC4648, Section 5 ] : https://tools.ietf.org/html/rfc4648#section-5
2465
2516
[ `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
2466
2519
[ `TypedArray` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
2467
2520
[ `TypedArray.from()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
2468
2521
[ `Uint32Array` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array
0 commit comments