Skip to content

highlightRanges

highlightRanges(item, positions): HighlightRange[]

Convert matched positions into an array of ranges for custom rendering.

Each range indicates a contiguous segment of the string and whether it was part of the match. positions are counted like in highlight; the ranges are UTF-16 offsets, so item.slice(start, end) is the segment. Useful for building custom highlight components, e.g. React elements, where the framework takes care of escaping.

Parameter Type
item string
positions readonly number[]

HighlightRange[]

highlightRanges('fuzzy', [0, 3, 4]);
// → [{ start: 0, end: 1, matched: true }, { start: 1, end: 3, matched: false },
// { start: 3, end: 5, matched: true }]