• Stars
    star
    300
  • Rank 138,870 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Utilities for highlighting text in autosuggest and autocomplete components

Build Status Contributors Coverage Status

npm Downloads npm Version

Autosuggest Highlight

Utilities for highlighting text in autosuggest and autocomplete components.

Installation

yarn add autosuggest-highlight

or

npm install autosuggest-highlight --save

API

Function Description
match(text, query, options) Calculates the characters to highlight in text based on query.
parse(text, matches) Breaks the given text to parts based on matches.

match(text, query, options)

Calculates the characters to highlight in text based on query.

It returns an array of pairs. Every pair [a, b] means that text.slice(a, b) should be highlighted.

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 at the beginning of a word by default:

var match = require('autosuggest-highlight/match');

// text indices:     012345678
// highlighting:          vv
var matches = match('some text', 'te'); // [[5, 7]]
// text indices:     012345678
// highlighting:
var matches = match('some text', 'e'); // []

Enable search inside words:

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:

// text indices:     012345678901234
// highlighting:     v
var matches = match('some sweet text', 's'); // [[0, 1]]

You'll get the second match, if query contains multiple words:

// text indices:     012345678901234
// highlighting:     v    v
var matches = match('some sweet text', 's s'); // [[0, 1], [5, 6]]

Or using the findAllOccurrences option:

// text indices:     012345678901234
// highlighting:     v    v
var matches = match('some sweet text', 's', { findAllOccurrences: true }); // [[0, 1], [5, 6]]

Matches are case insensitive:

// text indices:     012345678
// highlighting:          v
var matches = match('Some Text', 't'); // [[5, 6]]

and diacritics are removed:

// text indices:     0123456
// highlighting:     vvvv
var matches = match('Déjà vu', 'deja'); // [[0, 4]]

When query has multiple words, the order doesn't matter:

// text indices:     012345678901234
// highlighting:     v      v
var matches = match('Albert Einstein', 'a e'); // [[0, 1], [7, 8]]
// text indices:     012345678901234
// highlighting:     v      v
var matches = match('Albert Einstein', 'e a'); // [[0, 1], [7, 8]]

parse(text, matches)

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:

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
    }
  ]
*/

Usage with old browsers

For using this library with old browsers such as IE11 you must change import to

var match = require('autosuggest-highlight/ie11/match');
var parse = require('autosuggest-highlight/ie11/parse');

License

MIT

More Repositories

1

react-autosuggest

WAI-ARIA compliant React autosuggest component
JavaScript
5,967
star
2

rxviz

Rx Visualizer - Animated playground for Rx Observables
JavaScript
1,574
star
3

react-scanner

Extract React components and props usage from code.
JavaScript
577
star
4

accessible-colors

Automatically find the closest accessible color combination
JavaScript
471
star
5

react-autowhatever

Accessible rendering layer for Autosuggest and Autocomplete components
JavaScript
163
star
6

bs-blabla

BuckleScript `[@bs.blabla]` attributes explained with examples
154
star
7

shallow-equal

Minimalistic shallow equality check for arrays/objects
TypeScript
75
star
8

ionic-firebase-auth

Ionic app with Firebase user management
JavaScript
59
star
9

sliding-puzzle

Sliding puzzle built in Elm
JavaScript
52
star
10

elo.js

Minimalistic Javascript implementation of a Elo rating system
JavaScript
51
star
11

giant-piano

Minimal Javascript pagination utility
JavaScript
47
star
12

autosuggest-trie

Minimalistic trie implementation for autosuggest and autocomplete components
JavaScript
25
star
13

chessboard

Minimalistic Javascript implementation of a chess board UI.
JavaScript
13
star
14

analyze-deps-cli

Compare dependencies in package.json to the latest available versions.
JavaScript
13
star
15

react-baseline

Add baseline overlay to your React components
JavaScript
11
star
16

react-chessboard

React chess board component
JavaScript
9
star
17

ionic-firebase

Ionic app with Firebase user management
JavaScript
4
star
18

beepy

Easily track your Blood Pressure.
JavaScript
4
star
19

rotating-circle-illusion

JavaScript
3
star
20

extract-params

Extract parameters from a string based on a pattern
JavaScript
3
star
21

analyze-deps

Compare dependencies in package.json to the latest available versions.
JavaScript
2
star
22

interpolate-params

Interpolate params in a pattern
JavaScript
2
star
23

reason-react-tutorial

2
star
24

numbers-and-flowers

Visualization of rational numbers
JavaScript
2
star
25

misha-moroshko

Personal website
TypeScript
2
star
26

myplanner

JavaScript
1
star
27

aws-mock-store

Mock for moroshko/aws-s3-store and moroshko/aws-dynamodb-store
JavaScript
1
star
28

combination

JavaScript
1
star
29

color-contrast

JavaScript
1
star
30

zones-controller

TypeScript
1
star
31

2masters.com.au

TypeScript
1
star
32

cnnviz

Convolutional Neural Networks Playground
JavaScript
1
star