autosuggest-highlight
Advanced tools
Comparing version 3.1.1 to 3.2.0
@@ -15,3 +15,17 @@ var removeDiacritics = require('diacritic').clean; | ||
module.exports = function match(text, query) { | ||
function extend(subject, baseObject) { | ||
subject = subject || {}; | ||
Object.keys(subject).forEach(function(key) { | ||
baseObject[key] = !!subject[key]; | ||
}); | ||
return baseObject; | ||
} | ||
module.exports = function match(text, query, options) { | ||
options = extend(options, { | ||
insideWords: false, | ||
findAllOccurrences: false, | ||
requireMatchAll: false | ||
}); | ||
text = removeDiacritics(text); | ||
@@ -30,7 +44,15 @@ query = removeDiacritics(query); | ||
var wordLen = word.length; | ||
var prefix = wordCharacterRegex.test(word[0]) ? '\\b' : ''; | ||
var prefix = | ||
!options.insideWords && wordCharacterRegex.test(word[0]) ? '\\b' : ''; | ||
var regex = new RegExp(prefix + escapeRegexCharacters(word), 'i'); | ||
var index = text.search(regex); | ||
var occurrence, index; | ||
if (index > -1) { | ||
occurrence = regex.exec(text); | ||
if (options.requireMatchAll && occurrence === null) { | ||
text = ''; | ||
return []; | ||
} | ||
while (occurrence) { | ||
index = occurrence.index; | ||
result.push([index, index + wordLen]); | ||
@@ -43,2 +65,8 @@ | ||
text.slice(index + wordLen); | ||
if (!options.findAllOccurrences) { | ||
break; | ||
} | ||
occurrence = regex.exec(text); | ||
} | ||
@@ -45,0 +73,0 @@ |
{ | ||
"name": "autosuggest-highlight", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"description": "Utilities for highlighting text in autosuggest and autocomplete components", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -12,2 +12,8 @@ [![Build Status](https://img.shields.io/codeship/99ce0dd0-d5d5-0132-ce75-1e0a7d4d648e/master.svg?style=flat-square)](https://codeship.com/projects/78168) | ||
## Project status | ||
Looking for maintainers! | ||
Unfortunately, I don't have the time to maintain this project anymore. If you are interested to help, please reach out to me on Twitter [@moroshko](https://twitter.com/moroshko). | ||
## Installation | ||
@@ -29,8 +35,9 @@ | ||
| :--- | :--- | | ||
| [`match(text, query)`](#match) | Calculates the characters to highlight in `text` based on `query`. | | ||
| [`match(text, query, options)`](#match) | Calculates the characters to highlight in `text` based on `query`. | | ||
| [`parse(text, matches)`](#parse) | Breaks the given `text` to parts based on `matches`. | | ||
<a name="match"></a> | ||
### match(text, query) | ||
### match(text, query, options) | ||
Calculates the characters to highlight in `text` based on `query`. | ||
@@ -40,5 +47,13 @@ | ||
Options are passed as JSON. | ||
| Option | Description | | ||
| :--- | :--- | | ||
| insideWords | `boolean` false by default. Searches inside words | | ||
| findAllOccurrences | `boolean` false by default. Finds all occurrences of each match | | ||
| requireMatchAll | `boolean` false by default. Requires each word of `query` to be found in `text` or else returns an empty set | | ||
#### Examples | ||
We match only at the beginning of a word: | ||
We match at the beginning of a word by default: | ||
@@ -59,5 +74,15 @@ ```js | ||
When `query` is a single word, only the first match is returned: | ||
Enable search inside words: | ||
```js | ||
var match = require('autosuggest-highlight/match'); | ||
// text indices: 012345678 | ||
// highlighting: v | ||
var matches = match('some text', 'm', { insideWords: true }); // [[2, 3]] | ||
``` | ||
When `query` is a single word, only the first match is returned by default: | ||
```js | ||
// text indices: 012345678901234 | ||
@@ -76,2 +101,10 @@ // highlighting: v | ||
Or using the findAllOccurrences option: | ||
```js | ||
// text indices: 012345678901234 | ||
// highlighting: v v | ||
var matches = match('some sweet text', 's', { findAllOccurrences: true }); // [[0, 1], [5, 6]] | ||
``` | ||
Matches are case insensitive: | ||
@@ -108,2 +141,3 @@ | ||
<a name="parse"></a> | ||
### parse(text, matches) | ||
@@ -110,0 +144,0 @@ |
@@ -94,3 +94,17 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
module.exports = function match(text, query) { | ||
function extend(subject, baseObject) { | ||
subject = subject || {}; | ||
Object.keys(subject).forEach(function(key) { | ||
baseObject[key] = !!subject[key]; | ||
}); | ||
return baseObject; | ||
} | ||
module.exports = function match(text, query, options) { | ||
options = extend(options, { | ||
insideWords: false, | ||
findAllOccurrences: false, | ||
requireMatchAll: false | ||
}); | ||
text = removeDiacritics(text); | ||
@@ -109,7 +123,15 @@ query = removeDiacritics(query); | ||
var wordLen = word.length; | ||
var prefix = wordCharacterRegex.test(word[0]) ? '\\b' : ''; | ||
var prefix = | ||
!options.insideWords && wordCharacterRegex.test(word[0]) ? '\\b' : ''; | ||
var regex = new RegExp(prefix + escapeRegexCharacters(word), 'i'); | ||
var index = text.search(regex); | ||
var occurrence, index; | ||
if (index > -1) { | ||
occurrence = regex.exec(text); | ||
if (options.requireMatchAll && occurrence === null) { | ||
text = ''; | ||
return []; | ||
} | ||
while (occurrence) { | ||
index = occurrence.index; | ||
result.push([index, index + wordLen]); | ||
@@ -122,2 +144,8 @@ | ||
text.slice(index + wordLen); | ||
if (!options.findAllOccurrences) { | ||
break; | ||
} | ||
occurrence = regex.exec(text); | ||
} | ||
@@ -124,0 +152,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
30287
515
180
1