3.3.4 • Published 2 years ago

autosuggest-highlight v3.3.4

Weekly downloads
138,566
License
MIT
Repository
github
Last release
2 years ago

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

FunctionDescription
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.

OptionDescription
insideWordsboolean false by default. Searches inside words
findAllOccurrencesboolean false by default. Finds all occurrences of each match
requireMatchAllboolean 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

@catena-x/portal-shared-components-test@mahmudhamid/norsani-components@mochilabs/ra-ui-materialui@cbinsights/fds@carbon/ibmdotcom-reactqasir-ui-alphareactjs-99unojaivanajaivana_dashboardstudio.analytixatarnet-jumbo-reactbmoran-react-mui-mapbox-geocoder@mahmudhamid/norsani-formshtml_define_drop-down_list99uno-react-js299uno-reactjsreact-select-autoinfo_commonframe_pesquisagatsby-theme-eveemagatsby-theme-eveema-dcgatsby-theme-eveema-testspayawe-autocompleteseek-asia-style-guidegrossjsxiv-react-sdkkushkiconnectvttemp-connect-ui-acceltest-connect-bundler@cloudhub-ux/coregally-admin-componentsmy-test-devmp_story_book@everything-registry/sub-chunk-1187caseark-ui@greenspace/greenspace-ds-web@ascendware/mxcrc-ui-1rafsun-core-servicesnpm-platform-test-durulnpm-platform-test-durul1avni-canned-reports@arkejs/mui@alkuip/formsautocomplete-with-api@4geit/rct-search-input-component@australiangreens/ag-internal-components@atlassianlabs/guipi-jira-components@alessio.filippucci/admin-dashboard-core@axa-ch/pod-greenservices-sustainability-analysis-buildings@axa-ch/pod-sustainability-analysis-buildings@catena-x/portal-shared-componentscloudhubuicloudhubui-mincloudhub-componentscloudhub-min@backlight-dev/another-one.bricks-2htydw13pkxg0gsvc@backlight-dev/cent-undress-o9ffc.bricks-l6ngliw7@backlight-dev/power-drive-tgm7r.bricks-lo057i5n@backlight-dev/hdn-test.hdn-bricks@backlight-dev/human-apple-pfrt9.bricks-l0gqrlz3@backlight-dev/icky-boys-h9rid.bricks-lglce3zu@backlight-dev/rapid-sweater-1n8dt.bricks-l3yzvl9gcommon-frontend-components@cwrc/leafwriter-storage-serviceconnctuicarlosgutconnecttestgutierrezconnectui-testconnectdefinitiveconnect-version-testconnect-test-dherreraconnect-test-uibt-ui-materialui@bilgorajskim/ra-ui-materialuibrand-table-listbraid-design-system@backlight-dev/various-trouble-hjmth.bricks-lgq27cpj@backlight-dev/veil-steal-hej64.bricks-l7gtkkiy@backlight-dev/various-trouble-hjmth.bricks-l6ngliw7-fork-lgq24azv@backlight-dev/setbrain.ink-forego-vnavb.bricks-l5gkcnmk@backlight-dev/sillyleo.bricks-lccvkr2p@backlight-dev/soap-distribute-dux20.rjid@byorbit/or-ui-mercurycani-common-react@brickblock/styleguide@bootstrap-styled/ra-uicarbon-ailinkedlaw-reacthematology_clientnova-kitnuxonuxosqubryx-react-componentqasir-uiqasir-ui-devqhc_job_uira-ui-materialuira-ui-materialui-forkedra-ui-materialui-kmara-ui-materialui3platform-demo
3.3.1

2 years ago

3.3.0

2 years ago

3.3.4

2 years ago

3.3.3

2 years ago

3.3.2

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.1.1

6 years ago

3.1.0

7 years ago

3.0.0

7 years ago

2.1.1

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago