autosuggest-highlight
Advanced tools
Comparing version 2.1.1 to 3.0.0
{ | ||
"name": "autosuggest-highlight", | ||
"version": "2.1.1", | ||
"description": "Utilities for highlighting in autosuggest components", | ||
"main": "dist/autosuggest-highlight.js", | ||
"version": "3.0.0", | ||
"description": "Utilities for highlighting text in autosuggest and autocomplete components", | ||
"repository": { | ||
@@ -11,12 +10,26 @@ "type": "git", | ||
"author": "Misha Moroshko <michael.moroshko@gmail.com>", | ||
"bugs": { | ||
"url": "https://github.com/moroshko/autosuggest-highlight/issues" | ||
}, | ||
"homepage": "https://github.com/moroshko/autosuggest-highlight", | ||
"scripts": { | ||
"lint": "eslint src test webpack.standalone.config.js", | ||
"test": "npm run lint && mocha test --opts mocha.opts", | ||
"standalone": "webpack --config webpack.standalone.config.js", | ||
"build": "npm run lint && npm test && babel src --out-dir dist && npm run standalone" | ||
"lint": "eslint src", | ||
"test": "nyc mocha \"src/*.test.js\"", | ||
"dist": "rm -rf match parse && mkdir match parse && cp src/match.js match/index.js && cp src/parse.js parse/index.js", | ||
"prebuild": "npm run lint && npm test", | ||
"build": "npm run dist", | ||
"postversion": "git push && git push --tags", | ||
"prepublish": "npm run dist" | ||
}, | ||
"dependencies": { | ||
"diacritic": "0.0.2" | ||
}, | ||
"devDependencies": { | ||
"bithound": "^1.7.0", | ||
"chai": "^3.5.0", | ||
"eslint": "^3.7.0", | ||
"eslint-plugin-mocha": "^4.5.1", | ||
"mocha": "^3.1.0", | ||
"nyc": "^8.3.0" | ||
}, | ||
"files": [ | ||
"match", | ||
"parse" | ||
], | ||
"keywords": [ | ||
@@ -36,17 +49,20 @@ "autosuggest", | ||
], | ||
"dependencies": { | ||
"lodash.repeat": "^3.0.1" | ||
"nyc": { | ||
"lines": 100, | ||
"statements": 100, | ||
"functions": 100, | ||
"branches": 100, | ||
"include": [ | ||
"src/*.js" | ||
], | ||
"exclude": [ | ||
"src/*.test.js" | ||
], | ||
"reporter": [ | ||
"lcov", | ||
"text-summary" | ||
], | ||
"check-coverage": true | ||
}, | ||
"devDependencies": { | ||
"babel": "^5.8.23", | ||
"babel-core": "^5.8.25", | ||
"babel-eslint": "^4.1.3", | ||
"babel-loader": "^5.3.2", | ||
"chai": "^3.2.0", | ||
"eslint": "^1.5.0", | ||
"mocha": "^2.3.2", | ||
"node-libs-browser": "^0.5.3", | ||
"webpack": "^1.12.2" | ||
}, | ||
"license": "MIT" | ||
} |
181
README.md
@@ -1,7 +0,21 @@ | ||
[![Build Status][status-image]][status-url] | ||
[![NPM Version][npm-image]][npm-url] | ||
<a href="https://codeship.com/projects/78168" target="_blank"> | ||
<img src="https://img.shields.io/codeship/99ce0dd0-d5d5-0132-ce75-1e0a7d4d648e/master.svg?style=flat-square" | ||
alt="Build Status" /> | ||
</a> | ||
<a href="https://codecov.io/gh/moroshko/autosuggest-highlight" target="_blank"> | ||
<img src="https://img.shields.io/codecov/c/github/moroshko/autosuggest-highlight/master.svg?style=flat-square" | ||
alt="Coverage Status"> | ||
</a> | ||
<a href="https://www.bithound.io/github/moroshko/autosuggest-highlight" target="_blank"> | ||
<img src="https://www.bithound.io/github/moroshko/autosuggest-highlight/badges/score.svg" | ||
alt="bitHound Overall Score"> | ||
</a> | ||
<a href="https://npmjs.org/package/autosuggest-highlight" target="_blank"> | ||
<img src="https://img.shields.io/npm/v/autosuggest-highlight.svg?style=flat-square" | ||
alt="NPM Version" /> | ||
</a> | ||
# Autosuggest Highlight | ||
This library contains utilities for highlighting in autosuggest components. | ||
Utilities for highlighting text in autosuggest and autocomplete components. | ||
@@ -14,13 +28,8 @@ ## Installation | ||
Then, in your app: | ||
```js | ||
var highlight = require('autosuggest-highlight'); | ||
``` | ||
## API | ||
* [`match(text, query)`](#match) | ||
* [`parse(text, matches)`](#parse) | ||
* [`parseHTML(text, tag)`](#parseHTML) | ||
| Function | Description | | ||
| :--- | :--- | | ||
| [`match(text, query)`](#match) | Calculates the characters to highlight in `text` based on `query`. | | ||
| [`parse(text, matches)`](#parse) | Breaks the given `text` to parts based on `matches`. | | ||
@@ -30,82 +39,109 @@ <a name="match"></a> | ||
This function calculates the highlighting bits in `text` based on the `query`. | ||
Calculates the characters to highlight in `text` based on `query`. | ||
It returns an array of pairs. Every `[a, b]` pair means that `text.slice(a, b)` should be highlighted. | ||
It returns an array of pairs. Every pair `[a, b]` means that `text.slice(a, b)` should be highlighted. | ||
For example: | ||
#### Examples | ||
We match only at the beginning of a word: | ||
```js | ||
// 012345678901234567 (text indices) | ||
// vvvv vvv (characters to highlight) | ||
var text = 'Mill Park 3082 VIC'; | ||
var query = 'mill 308'; | ||
var match = require('autosuggest-highlight/match'); | ||
var matches = highlight.match(text, query); | ||
// text indices: 012345678 | ||
// highlighting: vv | ||
var matches = match('some text', 'te'); // [[5, 7]] | ||
``` | ||
// Returns: | ||
// [[0, 4], [10, 13]] | ||
```js | ||
// text indices: 012345678 | ||
// highlighting: | ||
var matches = match('some text', 'e'); // [] | ||
``` | ||
<a name="parse"></a> | ||
### parse(text, matches) | ||
When `query` is a single word, only the first match is returned: | ||
This function breaks the given `text` to parts according to `matches` (the output of [`match()`](#match)). | ||
```js | ||
// text indices: 012345678901234 | ||
// highlighting: v | ||
var matches = match('some sweet text', 's'); // [[0, 1]] | ||
``` | ||
Best way to explain how it works is using an example: | ||
You'll get the second match, if `query` contains multiple words: | ||
```js | ||
var parts = highlight.parse('Mill Park 3082 VIC', [[0, 4], [10, 13]]); | ||
// text indices: 012345678901234 | ||
// highlighting: v v | ||
var matches = match('some sweet text', 's s'); // [[0, 1], [5, 6]] | ||
``` | ||
// Returns: | ||
// [ | ||
// { | ||
// text: 'Mill', | ||
// highlight: true | ||
// }, | ||
// { | ||
// text: ' Park ', | ||
// highlight: false | ||
// }, | ||
// { | ||
// text: '308', | ||
// highlight: true | ||
// }, | ||
// { | ||
// text: '2 VIC', | ||
// highlight: false | ||
// } | ||
// ] | ||
Matches are case insensitive: | ||
```js | ||
// text indices: 012345678 | ||
// highlighting: v | ||
var matches = match('Some Text', 't'); // [[5, 6]] | ||
``` | ||
<a name="parseHTML"></a> | ||
### parseHTML(text, tag) | ||
and [diacritics](https://en.wikipedia.org/wiki/Diacritic) are removed: | ||
This function breaks the given `text` to parts according to the given `tag`. | ||
```js | ||
// text indices: 0123456 | ||
// highlighting: vvvv | ||
var matches = match('Déjà vu', 'deja'); // [[0, 4]] | ||
``` | ||
Best way to explain how it works is using an example: | ||
When `query` has multiple words, the order doesn't matter: | ||
```js | ||
var parts = highlight.parseHTML('<strong>Mill Park</strong> 3082 <strong>VIC</strong>', 'strong'); | ||
// text indices: 012345678901234 | ||
// highlighting: v v | ||
var matches = match('Albert Einstein', 'a e'); // [[0, 1], [7, 8]] | ||
``` | ||
// Returns: | ||
// [ | ||
// { | ||
// text: 'Mill Park', | ||
// highlight: true | ||
// }, | ||
// { | ||
// text: ' 3082 ', | ||
// highlight: false | ||
// }, | ||
// { | ||
// text: 'VIC', | ||
// highlight: true | ||
// } | ||
// ] | ||
```js | ||
// text indices: 012345678901234 | ||
// highlighting: v v | ||
var matches = match('Albert Einstein', 'e a'); // [[0, 1], [7, 8]] | ||
``` | ||
## Running Tests | ||
<a name="parse"></a> | ||
### parse(text, matches) | ||
```shell | ||
npm test | ||
Breaks the given `text` to parts based on `matches`. | ||
It returns an array of `text` parts by specifying whether each part should be highlighted or not. | ||
For example: | ||
```js | ||
var parse = require('autosuggest-highlight/parse'); | ||
// text indices: 0123456789012345 | ||
// highlighting: vv v | ||
var parts = parse('Pretty cool text', [[7, 9], [12, 13]]); | ||
/* | ||
[ | ||
{ | ||
text: 'Pretty ', | ||
highlight: false | ||
}, | ||
{ | ||
text: 'co', | ||
highlight: true | ||
}, | ||
{ | ||
text: 'ol ', | ||
highlight: false | ||
}, | ||
{ | ||
text: 't', | ||
highlight: true | ||
}, | ||
{ | ||
text: 'ext', | ||
highlight: false | ||
} | ||
] | ||
*/ | ||
``` | ||
@@ -115,7 +151,2 @@ | ||
[MIT](http://moroshko.mit-license.org) | ||
[status-image]: https://img.shields.io/codeship/99ce0dd0-d5d5-0132-ce75-1e0a7d4d648e/master.svg | ||
[status-url]: https://codeship.com/projects/78168 | ||
[npm-image]: https://img.shields.io/npm/v/autosuggest-highlight.svg | ||
[npm-url]: https://npmjs.org/package/autosuggest-highlight | ||
<a href="http://moroshko.mit-license.org" target="_blank">MIT</a> |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
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
6
150
8649
5
75
1
1
1
+ Addeddiacritic@0.0.2
+ Addeddiacritic@0.0.2(transitive)
- Removedlodash.repeat@^3.0.1
- Removedlodash._root@3.0.1(transitive)
- Removedlodash.repeat@3.2.0(transitive)