Gathering detailed insights and metrics for @tecsinapse/highlight-words-core
Gathering detailed insights and metrics for @tecsinapse/highlight-words-core
Gathering detailed insights and metrics for @tecsinapse/highlight-words-core
Gathering detailed insights and metrics for @tecsinapse/highlight-words-core
npm install @tecsinapse/highlight-words-core
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
419
Utility functions shared by react-highlight-words
and react-native-highlight-words
.
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:
1import { findAll } from "highlight-words-core"; 2 3const textToHighlight = "This is some text to highlight."; 4const searchWords = ["This", "i"]; 5 6const chunks = findAll({ 7 searchWords, 8 textToHighlight 9}); 10 11const highlightedText = chunks 12 .map(chunk => { 13 const { end, highlight, start } = chunk; 14 const text = textToHighlight.substr(start, end - start); 15 if (highlight) { 16 return `<mark>${text}</mark>`; 17 } else { 18 return text; 19 } 20 }) 21 .join("");
Run this example on Code Sandbox.
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 |
MIT License - fork, modify and use however you want.
No vulnerabilities found.
No security vulnerabilities found.