my-humble-tests
TypeScript icon, indicating that this package has built-in type declarations

10.0.0 • Public • Published

Tiny util to highlight given string in text.

API

This package contains only one function exported as getChunks. This method searches a string of text for a set of search terms and returns an array of chunks that describe the matches found. For example:

import { getChunks } from 'highlight-string';

const text = 'Have a nice day!';
const substrings = ['a', 'day'];

const chunks = getChunks({
  text,
  substrings,
});

const highlightedText = chunks
  .map(({ highlighted, text }) => (highlighted ? `<mark>${text}</mark>` : text))
  .join('');

getChunks

Parameter Required? Default value Type Description
text - string Text to search and highlight
substrings - Array<string> Array of strings to search for
caseSensitive false boolean Pass true if search should be case sensitive
sanitize - (text: string) => string Sanitize function

License

MIT

Package Sidebar

Install

npm i my-humble-tests

Weekly Downloads

3

Version

10.0.0

License

MIT

Unpacked Size

20.1 kB

Total Files

38

Last publish

Collaborators

  • dimkadenisov