highlight-words-core
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -97,2 +97,4 @@ module.exports = | ||
}); | ||
/** | ||
@@ -129,2 +131,4 @@ * Creates an array of chunk objects representing both higlightable and non highlightable pieces of text that match each search word. | ||
*/ | ||
var combineChunks = exports.combineChunks = function combineChunks(_ref2) { | ||
@@ -146,3 +150,3 @@ var chunks = _ref2.chunks; | ||
var endIndex = Math.max(prevChunk.end, nextChunk.end); | ||
processedChunks.push({ start: prevChunk.start, end: endIndex }); | ||
processedChunks.push({ highlight: false, start: prevChunk.start, end: endIndex }); | ||
} else { | ||
@@ -167,3 +171,3 @@ processedChunks.push(prevChunk, nextChunk); | ||
_ref3$sanitize = _ref3.sanitize, | ||
sanitize = _ref3$sanitize === undefined ? identity : _ref3$sanitize, | ||
sanitize = _ref3$sanitize === undefined ? defaultSanitize : _ref3$sanitize, | ||
searchWords = _ref3.searchWords, | ||
@@ -188,7 +192,7 @@ textToHighlight = _ref3.textToHighlight; | ||
while (match = regex.exec(textToHighlight)) { | ||
var start = match.index; | ||
var end = regex.lastIndex; | ||
var _start = match.index; | ||
var _end = regex.lastIndex; | ||
// We do not return zero-length matches | ||
if (end > start) { | ||
chunks.push({ start: start, end: end }); | ||
if (_end > _start) { | ||
chunks.push({ highlight: false, start: _start, end: _end }); | ||
} | ||
@@ -198,3 +202,3 @@ | ||
// See http://www.regexguru.com/2008/04/watch-out-for-zero-length-matches/ | ||
if (match.index == regex.lastIndex) { | ||
if (match.index === regex.lastIndex) { | ||
regex.lastIndex++; | ||
@@ -248,8 +252,8 @@ } | ||
function identity(value) { | ||
return value; | ||
function defaultSanitize(string) { | ||
return string; | ||
} | ||
function escapeRegExpFn(str) { | ||
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); | ||
function escapeRegExpFn(string) { | ||
return string.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); | ||
} | ||
@@ -256,0 +260,0 @@ |
{ | ||
"name": "highlight-words-core", | ||
"description": "Utility functions shared by react-highlight-words and react-native-highlight-words", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"author": "Brian Vaughn <brian.david.vaughn@gmail.com>", | ||
@@ -9,3 +9,5 @@ "license": "MIT", | ||
"scripts": { | ||
"build": "webpack --config webpack.config.dist.js --bail", | ||
"build": "yarn build:source && yarn build:flow", | ||
"build:flow": "cp flow-template dist/index.js.flow", | ||
"build:source": "webpack --config webpack.config.dist.js --bail", | ||
"lint": "standard", | ||
@@ -16,2 +18,6 @@ "prebuild": "rimraf dist", | ||
}, | ||
"files": [ | ||
"dist", | ||
"src/*.js" | ||
], | ||
"keywords": [ | ||
@@ -60,3 +66,6 @@ "highlighter", | ||
"webpack": "^1.9.6" | ||
}, | ||
"dependencies": { | ||
"babel-preset-flow": "^6" | ||
} | ||
} |
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). | ||
## 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. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
519
51
31374
1
+ Addedbabel-preset-flow@^6
+ Addedbabel-plugin-syntax-flow@6.18.0(transitive)
+ Addedbabel-plugin-transform-flow-strip-types@6.22.0(transitive)
+ Addedbabel-preset-flow@6.23.0(transitive)
+ Addedbabel-runtime@6.26.0(transitive)
+ Addedcore-js@2.6.12(transitive)
+ Addedregenerator-runtime@0.11.1(transitive)