From ba3d7200bc5019ca1694703d32953f34ba8a4861 Mon Sep 17 00:00:00 2001 From: mehdi Date: Fri, 14 Mar 2025 11:01:09 +0100 Subject: [PATCH] lastIndexOf: fix byteOffset when encoding passed as second arg --- index.js | 2 +- test/methods.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index bdea6048..cfb06608 100644 --- a/index.js +++ b/index.js @@ -712,7 +712,7 @@ function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { // Normalize byteOffset if (typeof byteOffset === 'string') { encoding = byteOffset - byteOffset = 0 + byteOffset = undefined } else if (byteOffset > 0x7fffffff) { byteOffset = 0x7fffffff } else if (byteOffset < -0x80000000) { diff --git a/test/methods.js b/test/methods.js index b1bf75ba..51b3fd6f 100644 --- a/test/methods.js +++ b/test/methods.js @@ -138,3 +138,12 @@ test('buffer.slice out of range', function (t) { t.equal((new B('hallo')).slice(10, 2).toString(), '') t.end() }) + +test('lastIndexOf with encoding as second arg', function (t) { + const b = new B('abcdefghij') + t.equal(b.lastIndexOf('b'), 1) + t.equal(b.lastIndexOf('b', 'utf8'), 1) + t.equal(b.lastIndexOf('b', 'latin1'), 1) + t.equal(b.lastIndexOf('b', 'binary'), 1) + t.end() +})