From c76ba5d4c45c23269c83bf5d9bfd0ac62f2b8bfb Mon Sep 17 00:00:00 2001 From: Rien Boydens <148046320+rien-rty@users.noreply.github.com> Date: Mon, 8 Dec 2025 16:18:00 +0100 Subject: [PATCH 1/2] Fixed incorrect index values --- index.js | 2 +- test.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index f34f074..64a72f8 100644 --- a/index.js +++ b/index.js @@ -78,7 +78,7 @@ function replaceString(str, match, fn) { curCharLen = result[i].length; curCharStart += result[i - 1].length; - result[i] = fn(result[i], i, curCharStart); + result[i] = fn(result[i], (i - 1) / 2, curCharStart); curCharStart += curCharLen; } diff --git a/test.js b/test.js index 761d43d..e8ccb7d 100644 --- a/test.js +++ b/test.js @@ -170,3 +170,14 @@ test("Avoids undefined values due to regex", (t) => { replaceString(string, /(hey)|(you)/, x => x); }); }); + +test("Indexes start at 0 and are contiguous", t => { + const string = 'Hello there general Kenobi'; + const re = /(\w+)/; + + let expectedIndex = 0; + replaceString(string, re, (_, index) => { + t.deepEqual(expectedIndex, index); + index++; + }); +}); From 6ca55efbe88238a78c51f3e7b9ebe03ef06ee852 Mon Sep 17 00:00:00 2001 From: Rien Boydens Date: Mon, 8 Dec 2025 16:22:29 +0100 Subject: [PATCH 2/2] Fixed test --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index e8ccb7d..ab22e88 100644 --- a/test.js +++ b/test.js @@ -176,8 +176,8 @@ test("Indexes start at 0 and are contiguous", t => { const re = /(\w+)/; let expectedIndex = 0; - replaceString(string, re, (_, index) => { + replaceString(string, re, (match, index) => { t.deepEqual(expectedIndex, index); - index++; + expectedIndex++; }); });