From 6549bc2a3540ca36e1cf2fbd45f9a5f68d34a1c4 Mon Sep 17 00:00:00 2001 From: "dcposch@dcpos.ch" Date: Wed, 7 Dec 2016 00:47:38 -0800 Subject: [PATCH] buffer: fix comments in bidirectionalIndexOf PR-URL: https://github.com/nodejs/node/pull/10162 Fixes: https://github.com/nodejs/node/issues/9801 Reviewed-By: Roman Reiss Reviewed-By: Stephen Belanger Reviewed-By: Trevor Norris Reviewed-By: James M Snell Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Rich Trott --- lib/buffer.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 2349474952c462..fa8d1c61d4a309 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -588,9 +588,10 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { } else if (byteOffset < -0x80000000) { byteOffset = -0x80000000; } - byteOffset = +byteOffset; // Coerce to Number. - if (isNaN(byteOffset)) { - // If the offset is undefined, null, NaN, "foo", etc, search whole buffer. + // Coerce to Number. Values like null and [] become 0. + byteOffset = +byteOffset; + // If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer. + if (Number.isNaN(byteOffset)) { byteOffset = dir ? 0 : (buffer.length - 1); } dir = !!dir; // Cast to bool.