Skip to content

Commit

Permalink
Merge pull request #175 from PROCEED-Labs/ms2/fuzy-search
Browse files Browse the repository at this point in the history
Fix: handle overlapping match intervals
  • Loading branch information
winniel24 authored Nov 29, 2023
2 parents 3d00692 + bd79706 commit 506770f
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/management-system-v2/lib/useFuzySearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ function highlightText<TObj>(
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(<span key={lastIndex}>{value.slice(lastIndex, start)}</span>);
}

result.push(
<span key={start} style={{ color }}>
{value.slice(start, end + 1)}
</span>,
);

lastIndex = end + 1;
}
if (lastIndex !== value.length)
Expand Down

0 comments on commit 506770f

Please sign in to comment.