Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
highlight-words-core
Advanced tools
Utility functions shared by react-highlight-words and react-native-highlight-words
The highlight-words-core npm package is a utility for finding and highlighting words within a string. It is particularly useful for search functionalities where you need to emphasize the search terms within the results.
Highlight Words
This feature allows you to find and highlight specific words within a given text. The `findAll` function takes an object with `searchWords` and `textToHighlight` properties and returns an array of chunks indicating the positions of the highlighted words.
const { findAll } = require('highlight-words-core');
const textToHighlight = 'This is a sample text to highlight certain words.';
const searchWords = ['sample', 'highlight'];
const chunks = findAll({
searchWords,
textToHighlight
});
console.log(chunks);
Case Insensitive Search
This feature allows you to perform a case-insensitive search. By setting the `caseSensitive` option to `false`, the search will ignore case differences between the search words and the text.
const { findAll } = require('highlight-words-core');
const textToHighlight = 'This is a Sample text to Highlight certain words.';
const searchWords = ['sample', 'highlight'];
const chunks = findAll({
searchWords,
textToHighlight,
autoEscape: true,
caseSensitive: false
});
console.log(chunks);
Escape Special Characters
This feature allows you to escape special characters in the search words. By setting the `autoEscape` option to `true`, special characters in the search words will be escaped, preventing them from being interpreted as regular expressions.
const { findAll } = require('highlight-words-core');
const textToHighlight = 'This is a sample text with special characters like * and ?.';
const searchWords = ['*', '?'];
const chunks = findAll({
searchWords,
textToHighlight,
autoEscape: true
});
console.log(chunks);
The react-highlight-words package provides similar functionality but is specifically designed for React applications. It allows you to highlight words within a React component, making it more suitable for front-end development compared to highlight-words-core, which is a more general utility.
The highlight.js package is a syntax highlighter for code, which can also be used to highlight words within a text. However, it is more focused on code syntax highlighting rather than general text highlighting, making it a bit different in scope compared to highlight-words-core.
The mark.js package is a versatile library for highlighting text. It offers more advanced features like custom element creation, regular expression support, and more. It is more feature-rich compared to highlight-words-core, which is simpler and more focused on basic word highlighting.
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:
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.
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.
FAQs
Utility functions shared by react-highlight-words and react-native-highlight-words
The npm package highlight-words-core receives a total of 347,214 weekly downloads. As such, highlight-words-core popularity was classified as popular.
We found that highlight-words-core demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.