-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc: clarify Buffer.indexOf() and lastIndexOf() #10162
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there!
@@ -1150,6 +1150,33 @@ console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2')); | |||
console.log(utf16Buffer.indexOf('\u03a3', -4, 'ucs2')); | |||
``` | |||
|
|||
Edge cases: | |||
|
|||
If `value` is not a string, number, or Buffer, this method will throw a `TypeError`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Long lines. Please wrap at <= 80 chars :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, done. FYI though, that file has lots of lines over 80 chars
|
||
Edge case examples: | ||
|
||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```js
Edge case examples: | ||
|
||
``` | ||
var b = new Buffer('abcdef') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const b = Buffer.from('abcdef');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, my b.
Edge case examples: | ||
|
||
``` | ||
var b = new Buffer('abcdef') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```js
const b = Buffer.from('abcdef');
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
I don't think we need to explicitly be testing |
62882e1
to
b7e3c3f
Compare
Why not make the tests in this style so we test equal behaviour with string functions? assert.equal(b.lastIndexOf('b', 0), s.lastIndexOf('b', 0)) // -1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General direction is great 💯 Thanks! 🎆 I have several nits for the documentation changes and a suggestion for the tests.
@@ -1150,6 +1150,34 @@ console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2')); | |||
console.log(utf16Buffer.indexOf('\u03a3', -4, 'ucs2')); | |||
``` | |||
|
|||
Edge cases: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No reason to say "Edge cases." Just start right in with "If `value is not a string..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, "edge case" terminology often has somewhat negative interpretations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, fixed
that coerce to `NaN` or 0, like `{}`, `[]`, `null` or `undefined`, will search | ||
the whole buffer. This behavior matches [`String.indexOf()`]. | ||
|
||
Edge case examples: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, remove "Edge case examples:".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
const b = Buffer.from('abcdef') | ||
|
||
// Passing a value that's a number, but not a valid byte | ||
// Prints: 2, equivalent to searching for 99 or 'c' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Prints" is not accurate. These don't print anything except in the REPL. I believe the way we handle this in other contexts is like this:
b.indexOf(99.9); // 2
Also, note that we use semi-colons in the example code throughout the docs. Please do the same here. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I've added semicolons and console.log
to be consistent with the other Buffer
examples
@@ -1270,6 +1298,37 @@ console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'ucs2')); | |||
console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'ucs2')); | |||
``` | |||
|
|||
Edge cases: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same: Don't specify "Edge cases".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
that coerce to `NaN`, like `{}` or `undefined`, will search the whole buffer. | ||
This behavior matches [`String.lastIndexOf()`]. | ||
|
||
Edge case examples: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And remove this too, please. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
const b = Buffer.from('abcdef') | ||
|
||
// Passing a value that's a number, but not a valid byte | ||
// Prints: 2, equivalent to searching for 99 or 'c' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above regarding "Prints" etc. and semi-colons too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
assert.equal(b.lastIndexOf('b', undefined), 1); | ||
assert.equal(b.lastIndexOf('b', null), -1); | ||
assert.equal(s.lastIndexOf('b', undefined), 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize there's a lot of assert.equal()
in this test file, but if you could at least use assert.strictEqual()
for the stuff you're adding, that would probably be better. If you want to change surrounding instances in the file too, awesome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
/cc @nodejs/documentation |
Agreed on all the suggestions from @Trott and the direct buffer to string test suggestion by @silverwind. Once those changes are in, it should be good to me. |
@Qard @silverwind done |
@@ -1150,6 +1150,30 @@ console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2')); | |||
console.log(utf16Buffer.indexOf('\u03a3', -4, 'ucs2')); | |||
``` | |||
|
|||
If `value` is not a string, number, or Buffer, this method will throw a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nodejs/documentation: Should "Buffer" be surrounded in back-ticks in this line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on how it's used in other parts of this file, yes. Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM if CI is ✅ and other revewer's nits are addressed. Thanks for doing this!
|
||
If `byteOffset` is not a number, it will be coerced to a number. Any arguments | ||
that coerce to `NaN` or 0, like `{}`, `[]`, `null` or `undefined`, will search | ||
the whole buffer. This behavior matches [`String.indexOf()`]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String.indexOf()
should have corresponding link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I figured out how to build the docs and verified that the link works now.
I also changed it to String#indexOf()
for consistency.
|
||
If `byteOffset` is not a number, it will be coerced to a number. Any arguments | ||
that coerce to `NaN`, like `{}` or `undefined`, will search the whole buffer. | ||
This behavior matches [`String.lastIndexOf()`]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String.lastIndexOf()
should have corresponding link.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
8836e63
to
409b4b6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent stuff. Just one change suggestion, but not critical.
byteOffset = +byteOffset; // Coerce to Number. | ||
// Coerce to Number. Values like null and [] become 0. | ||
byteOffset = +byteOffset; | ||
// If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer. | ||
if (isNaN(byteOffset)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're going to explicitly coerce the value first, then let's go ahead and use Number.isNaN()
instead. Which explicitly checks for NaN
, w/o first coercing the argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call, fixed
assert.strictEqual(b.lastIndexOf('b', [2]), 1); | ||
|
||
// Behavior should match String.lastIndexOf() | ||
var s = 'abcdef'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This could be a const
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lots of var
in this file (mixed with some let
and const
). Might be cool to ban var
in the future and convert all of our variables to let
or const
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed s
to be const
in any case
assert.strictEqual(b.lastIndexOf('b', undefined), 1); | ||
assert.strictEqual(b.lastIndexOf('b', {}), 1); | ||
|
||
// The following offsets coerce to 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Strictly speaking, zero is not being coerced to zero :D
// The following offset coerces to 2, in other words +[2] === 2 | ||
assert.strictEqual(b.lastIndexOf('b', [2]), 1); | ||
|
||
// Behavior should match String.lastIndexOf() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have similar tests, like the following, for indexOf
as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but indexOf
is less interesting. Args coercing to 0 and NaN both have the same behavior, searching the whole buffer. Only lastIndexOf
has this weirdness where, for example, passing an offset of null
vs undefined
does different things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Added tests for indexOf
ping @dcposch |
Test type coercion for non-number offset arguments. Verify that Buffer and String behave the same way. PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Test type coercion for non-number offset arguments. Verify that Buffer and String behave the same way. PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Test type coercion for non-number offset arguments. Verify that Buffer and String behave the same way. PR-URL: nodejs#10162 Fixes: nodejs#9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: nodejs#10162 Fixes: nodejs#9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: nodejs#10162 Fixes: nodejs#9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Test type coercion for non-number offset arguments. Verify that Buffer and String behave the same way. PR-URL: nodejs#10162 Fixes: nodejs#9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: nodejs#10162 Fixes: nodejs#9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: nodejs#10162 Fixes: nodejs#9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Test type coercion for non-number offset arguments. Verify that Buffer and String behave the same way. PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This would need a backport PR if it should land on v4 |
Do you want me to make one?
|
Test type coercion for non-number offset arguments. Verify that Buffer and String behave the same way. PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
PR-URL: #10162 Fixes: #9801 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
Checklist
make -j8 test
(UNIX), orvcbuild test nosign
(Windows) passesAffected core subsystem(s)
doc
Description of change
Clarifies the
buffer
documentation to explain how type coercion works forindexOf()
andlastIndexOf()
.Fixes #9801
@vsemozhetbyt @silverwind