Socket
Socket
Sign inDemoInstall

highlight-words-core

Package Overview
Dependencies
0
Maintainers
3
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    highlight-words-core

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


Version published
Weekly downloads
502K
decreased by-8.44%
Maintainers
3
Install size
31.5 kB
Created
Weekly downloads
 

Readme

Source

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

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:

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.

findAll

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

ParameterRequired?TypeDescription
autoEscapebooleanEscape special regular expression characters
caseSensitivebooleanSearch should be case sensitive
findChunksFunctionCustom find function (advanced)
sanitizeFunctionCustom sanitize function (advanced)
searchWordsArray<string>Array of words to search for
textToHighlightstringText to search and highlight

License

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

Keywords

FAQs

Last updated on 12 Nov 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc