@@ -1151,6 +1151,30 @@ console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2'));
1151
1151
console .log (utf16Buffer .indexOf (' \u03a3 ' , - 4 , ' ucs2' ));
1152
1152
```
1153
1153
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
+
1154
1178
### buf.includes(value[ , byteOffset] [ , encoding ] )
1155
1179
<!-- YAML
1156
1180
added: v5.3.0
@@ -1271,6 +1295,33 @@ console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'ucs2'));
1271
1295
console .log (utf16Buffer .lastIndexOf (' \u03a3 ' , - 5 , ' ucs2' ));
1272
1296
```
1273
1297
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
+
1274
1325
### buf.length
1275
1326
<!-- YAML
1276
1327
added: v0.1.90
@@ -2443,6 +2494,8 @@ console.log(buf);
2443
2494
[ RFC1345 ] : https://tools.ietf.org/html/rfc1345
2444
2495
[ RFC4648, Section 5 ] : https://tools.ietf.org/html/rfc4648#section-5
2445
2496
[ `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
2446
2499
[ `TypedArray` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
2447
2500
[ `TypedArray.from()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
2448
2501
[ `Uint32Array` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array
0 commit comments