highlight-words-core

Framework-agnostic utilities for finding and chunking text matches for search-term highlighting

Library
npm
v1.2.3
255stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
34/100Needs Attention
Development Activity0
Maintenance0
Community56
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
67/100Good
Architecture68
Code Quality65
Innovation45
Learning Curve90

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 words
  • findChunks() - locates raw start/end matches for each search word in the target text, with case-sensitivity and auto-escape options
  • combineChunks() - merges overlapping match ranges into single chunks
  • fillInChunks() - fills the gaps between matches with non-highlighted chunks covering the full string

Common Use Cases

  • Powering search-term highlighting in react-highlight-words and react-native-highlight-words components
  • 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search