highlight-words-core
Framework-agnostic utilities for finding and chunking text matches for search-term highlighting
Repository Health
Technical Analysis
highlight-words-core is a small, dependency-free JavaScript library that computes which portions of a string should be highlighted given one or more search words. It returns an array of “chunk” objects ({ start, end, highlight }) describing highlightable and non-highlightable spans, after finding matches, combining overlapping ranges, and filling in the gaps between them.
It does no rendering itself — it is the shared matching/chunking logic extracted from react-highlight-words, and reused by react-native-highlight-words, so any UI layer can consume the same chunk data to render highlighted text.
What You Get
findAll()- the main entry point that finds, combines, and fills in text chunks for a set of search wordsfindChunks()- locates raw start/end matches for each search word in the target text, with case-sensitivity and auto-escape optionscombineChunks()- merges overlapping match ranges into single chunksfillInChunks()- fills the gaps between matches with non-highlighted chunks covering the full string
Common Use Cases
- Powering search-term highlighting in
react-highlight-wordsandreact-native-highlight-wordscomponents - Building custom highlight-rendering components for other UI frameworks that need the same chunking logic
- Implementing search-result snippet highlighting in non-React JavaScript applications
- Sanitizing and highlighting user-supplied search terms in text without a full regex-highlighting library
Under The Hood
Architecture: The library is a thin, single-purpose module — src/utils.js implements the four core functions (findChunks, combineChunks, fillInChunks, findAll) as pure functions operating on plain arrays and objects, with src/index.js simply re-exporting them; there is no class hierarchy, state, or DOM dependency anywhere in the package. Tech Stack: Written in Flow-typed ES modules, built via Webpack/Babel into a UMD/CommonJS distribution, with Karma configured for browser-based test runs; it has zero runtime dependencies. Code Quality: src/utils.test.js covers the matching, combining, and gap-filling logic with unit tests run through Karma; the codebase is small (roughly one module) and has changed little since its initial extraction from react-highlight-words, reflecting a stable, feature-complete utility rather than an actively evolving one. API Design: A single findAll({ searchWords, textToHighlight, ... }) call covers the common case, returning ready-to-render chunk objects, which makes it easy to wire into a custom highlighter component with minimal boilerplate.