UNPKG

braid-design-system

Version:
46 lines (45 loc) 1.35 kB
import { jsx } from "react/jsx-runtime"; import parseHighlights from "autosuggest-highlight/parse/index.js"; import { Highlight } from "./Highlight/Highlight.mjs"; const formatRanges = (value, highlightRanges, tone) => { if (highlightRanges && value) { let lastEnd = 0; const validatedAndSortedRanges = highlightRanges.sort((a, b) => a.start > b.start ? 1 : -1).reduce( (acc, { end, start }) => { const resolvedEnd = end || value.length; if (resolvedEnd <= start) { return acc; } const adjustedRange = []; if (resolvedEnd > lastEnd) { adjustedRange.push({ // if overlapping, start from end of last range otherwise start from specified range start: start < lastEnd ? lastEnd : start, end }); lastEnd = resolvedEnd; } return [...acc, ...adjustedRange]; }, [] ); return parseHighlights( value, validatedAndSortedRanges.map(({ start, end }) => [ start, end || value.length ]) ).reduce((acc, { text, highlight }, i) => { if (text) { acc.push( highlight ? /* @__PURE__ */ jsx(Highlight, { tone, children: text }, i) : text ); } return acc; }, []); } return [value]; }; export { formatRanges };