Highlights all case-insensitive occurrences of query in text with yellow mark styling.
Results are memoized by a cache key incorporating text hash and query to avoid redundant
processing on repeated calls with the same inputs and prevent collisions from same-length
but different text strings.
// Create cache key incorporating text hash, length, and query for low-collision lookup constcacheKey = `${hashText(text)}|${text.length}|${query.toLowerCase()}`; if (highlightCache.has(cacheKey)) { constcachedResult = highlightCache.get(cacheKey)!; // Move to end of Map by deleting and re-setting to make eviction closer to LRU highlightCache.delete(cacheKey); highlightCache.set(cacheKey, cachedResult); returncachedResult; }
// Store in cache, evicting oldest entry if cache is full if (highlightCache.size >= CACHE_SIZE_LIMIT) { constfirstKey = highlightCache.keys().next().value; if (firstKey) { highlightCache.delete(firstKey); } } highlightCache.set(cacheKey, result);
returnresult; };
highlightText(text:string,query:string):ReactNode
Parameters
text: string
The text to highlight.
query: string
The search query to find and highlight.
Returns ReactNode
React nodes array containing text and mark elements; original text if query empty.
Highlights all case-insensitive occurrences of query in text with yellow mark styling. Results are memoized by a cache key incorporating text hash and query to avoid redundant processing on repeated calls with the same inputs and prevent collisions from same-length but different text strings.
Source