# highlight-words-core

> Utility functions shared by react-highlight-words and react-native-highlight-words

Latest version **1.2.3** (published 2024-09-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install highlight-words-core
pnpm add highlight-words-core
yarn add highlight-words-core
bun add highlight-words-core
```

## Health

**Score 33/100 (F)** — status: maintenance-mode.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.3 |
| Published | 2024-09-28 |
| First published | 2016-09-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/highlight-words-core) |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 30.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 254 |
| Author | Brian Vaughn |
| Maintainers | ryanflorence, brianvaughn, clauderic |
| Keywords | highlighter, highlight, text, words, matches, substring, occurrences, search |

## Links

- npm: https://www.npmjs.com/package/highlight-words-core
- Repository: github.com/bvaughn/highlight-words-core
- npm.io page: https://npm.io/package/highlight-words-core

## Alternatives

- [@oh-my-pi/pi-natives](https://npm.io/package/@oh-my-pi/pi-natives.md) — 51.8K weekly downloads
- [@capgo/capacitor-light-sensor](https://npm.io/package/@capgo/capacitor-light-sensor.md) — 3.0K weekly downloads
- [@heyhuynhgiabuu/pi-diff](https://npm.io/package/@heyhuynhgiabuu/pi-diff.md) — 492 weekly downloads
- [@lotsa/verdant-lang-asm](https://npm.io/package/@lotsa/verdant-lang-asm.md) — 38 weekly downloads
- [new-era-syntax](https://npm.io/package/new-era-syntax.md) — 20 weekly downloads

## Recent versions

- 1.2.3 (latest) — 2024-09-28
- 1.2.2 — 2018-11-12
- 1.2.1 — 2018-11-11
- 1.2.0 — 2018-02-19
- 1.1.2 — 2017-12-13
- 1.1.1 — 2017-12-13
- 1.1.0 — 2017-10-15
- 1.0.3 — 2016-09-22
- 1.0.2 — 2016-09-22
- 1.0.1 — 2016-09-22
- 1.0.0 — 2016-09-22

## README

Utility functions shared by [`react-highlight-words`](https://github.com/bvaughn/react-highlight-words) and [`react-native-highlight-words`](https://github.com/clauderic/react-native-highlight-words).

---
### 🎉 [Become a sponsor](https://github.com/sponsors/bvaughn/) or ☕ [Buy me a coffee](http://givebrian.coffee/)
---

## API

The primary API for this package is a function exported as `findAll`. This method searches a string of text for a set of search terms and returns an array of "chunks" that describe the matches found.

Each "chunk" is an object consisting of a pair of indices (`chunk.start` and `chunk.end`) and a boolean specfifying whether the chunk is a match (`chunk.highlight`). For example:

```js
import { findAll } from "highlight-words-core";

const textToHighlight = "This is some text to highlight.";
const searchWords = ["This", "i"];

const chunks = findAll({
  searchWords,
  textToHighlight
});

const highlightedText = chunks
  .map(chunk => {
    const { end, highlight, start } = chunk;
    const text = textToHighlight.substr(start, end - start);
    if (highlight) {
      return `<mark>${text}</mark>`;
    } else {
      return text;
    }
  })
  .join("");
```

[Run this example on Code Sandbox.](https://codesandbox.io/s/ykwrzrl6wx)

### `findAll`

The `findAll` function accepts several parameters, although only the `searchWords` array and `textToHighlight` string are required.

| Parameter | Required? | Type | Description |
| --- | :---: | --- | --- |
| autoEscape |  | `boolean` | Escape special regular expression characters |
| caseSensitive |  | `boolean` | Search should be case sensitive |
| findChunks |  | `Function` | Custom find function (advanced) |
| sanitize |  | `Function` | Custom sanitize function (advanced) |
| searchWords | ✅ | `Array<string>` | Array of words to search for |
| textToHighlight | ✅ | `string` | Text to search and highlight |


## License
MIT License - fork, modify and use however you want.

---
_Source: https://npm.io/package/highlight-words-core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
