From b95481dab1af944343ae50e21ee5e0ec87f53bd0 Mon Sep 17 00:00:00 2001 From: Felipe Trost Date: Mon, 27 Nov 2023 15:27:01 +0100 Subject: [PATCH] Fix: handle overlapping match intervals --- src/management-system-v2/lib/useFuzySearch.tsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/management-system-v2/lib/useFuzySearch.tsx b/src/management-system-v2/lib/useFuzySearch.tsx index 0330db317..50c3642f3 100644 --- a/src/management-system-v2/lib/useFuzySearch.tsx +++ b/src/management-system-v2/lib/useFuzySearch.tsx @@ -22,16 +22,19 @@ function highlightText( let lastIndex = 0; const sortedMatches = matches.indices.toSorted((a, b) => a[0] - b[0]); - for (const [start, end] of sortedMatches) { - if (lastIndex < start) + for (let [start, end] of sortedMatches) { + if (end <= lastIndex) continue; + if (start < lastIndex) start = lastIndex; + + if (lastIndex < start) { result.push({value.slice(lastIndex, start)}); + } result.push( {value.slice(start, end + 1)} , ); - lastIndex = end + 1; } if (lastIndex !== value.length)