@@ -1156,6 +1156,30 @@ console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2'));
1156
1156
console .log (utf16Buffer .indexOf (' \u03a3 ' , - 4 , ' ucs2' ));
1157
1157
```
1158
1158
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
+
1159
1183
### buf.includes(value[ , byteOffset] [ , encoding ] )
1160
1184
<!-- YAML
1161
1185
added: v5.3.0
@@ -1276,6 +1300,33 @@ console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'ucs2'));
1276
1300
console .log (utf16Buffer .lastIndexOf (' \u03a3 ' , - 5 , ' ucs2' ));
1277
1301
```
1278
1302
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
+
1279
1330
### buf.length
1280
1331
<!-- YAML
1281
1332
added: v0.1.90
@@ -2413,6 +2464,8 @@ console.log(buf);
2413
2464
[ RFC1345 ] : https://tools.ietf.org/html/rfc1345
2414
2465
[ RFC4648, Section 5 ] : https://tools.ietf.org/html/rfc4648#section-5
2415
2466
[ `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
2416
2469
[ `TypedArray` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
2417
2470
[ `TypedArray.from()` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from
2418
2471
[ `Uint32Array` ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array
0 commit comments