doc: clarify Buffer.indexOf() and lastIndexOf()#10162
doc: clarify Buffer.indexOf() and lastIndexOf()#10162dcposch wants to merge 4 commits intonodejs:masterfrom
Conversation
doc/api/buffer.md
Outdated
There was a problem hiding this comment.
Long lines. Please wrap at <= 80 chars :-)
There was a problem hiding this comment.
Sounds good, done. FYI though, that file has lots of lines over 80 chars
doc/api/buffer.md
Outdated
doc/api/buffer.md
Outdated
There was a problem hiding this comment.
const b = Buffer.from('abcdef');
doc/api/buffer.md
Outdated
There was a problem hiding this comment.
```js
const b = Buffer.from('abcdef');
|
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 |
Trott
left a comment
There was a problem hiding this comment.
General direction is great 💯 Thanks! 🎆 I have several nits for the documentation changes and a suggestion for the tests.
doc/api/buffer.md
Outdated
There was a problem hiding this comment.
No reason to say "Edge cases." Just start right in with "If `value is not a string..."
There was a problem hiding this comment.
Yeah, "edge case" terminology often has somewhat negative interpretations.
doc/api/buffer.md
Outdated
There was a problem hiding this comment.
Similarly, remove "Edge case examples:".
doc/api/buffer.md
Outdated
There was a problem hiding this comment.
"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); // 2Also, 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.
Sounds good. I've added semicolons and console.log to be consistent with the other Buffer examples
doc/api/buffer.md
Outdated
doc/api/buffer.md
Outdated
There was a problem hiding this comment.
And remove this too, please. Thanks.
doc/api/buffer.md
Outdated
There was a problem hiding this comment.
Same comment as above regarding "Prints" etc. and semi-colons too.
test/parallel/test-buffer-indexof.js
Outdated
There was a problem hiding this comment.
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.
|
/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 |
doc/api/buffer.md
Outdated
There was a problem hiding this comment.
@nodejs/documentation: Should "Buffer" be surrounded in back-ticks in this line?
There was a problem hiding this comment.
Based on how it's used in other parts of this file, yes. Fixed
Trott
left a comment
There was a problem hiding this comment.
LGTM if CI is ✅ and other revewer's nits are addressed. Thanks for doing this!
doc/api/buffer.md
Outdated
There was a problem hiding this comment.
String.indexOf() should have corresponding link.
There was a problem hiding this comment.
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.
doc/api/buffer.md
Outdated
There was a problem hiding this comment.
String.lastIndexOf() should have corresponding link.
8836e63 to
409b4b6
Compare
trevnorris
left a comment
There was a problem hiding this comment.
Excellent stuff. Just one change suggestion, but not critical.
lib/buffer.js
Outdated
There was a problem hiding this comment.
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.
test/parallel/test-buffer-indexof.js
Outdated
There was a problem hiding this comment.
Nit: This could be a const
There was a problem hiding this comment.
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.
Changed s to be const in any case
test/parallel/test-buffer-indexof.js
Outdated
There was a problem hiding this comment.
Nit: Strictly speaking, zero is not being coerced to zero :D
test/parallel/test-buffer-indexof.js
Outdated
There was a problem hiding this comment.
Can we have similar tests, like the following, for indexOf as well?
There was a problem hiding this comment.
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.
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 <[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]>
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]>
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]>
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 <[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]>
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]>
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]>
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 <[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]>
PR-URL: nodejs#10162 Fixes: nodejs#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]>
PR-URL: nodejs#10162 Fixes: nodejs#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]>
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 <[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]>
PR-URL: nodejs#10162 Fixes: nodejs#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]>
PR-URL: nodejs#10162 Fixes: nodejs#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]>
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 <[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]>
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]>
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]>
|
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 <[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]>
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]>
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]>
Checklist
make -j8 test(UNIX), orvcbuild test nosign(Windows) passesAffected core subsystem(s)
doc
Description of change
Clarifies the
bufferdocumentation to explain how type coercion works forindexOf()andlastIndexOf().Fixes #9801
@vsemozhetbyt @silverwind