GithubHelp home page GithubHelp logo

moroshko / autosuggest-highlight Goto Github PK

View Code? Open in Web Editor NEW
293.0 7.0 36.0 1.29 MB

Utilities for highlighting text in autosuggest and autocomplete components

License: MIT License

JavaScript 100.00%
autosuggest autocomplete typeahead javascript

autosuggest-highlight's Introduction

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

autosuggest-highlight's People

Contributors

anubra266 avatar dmitry-kovalev avatar kuhma avatar lancemayer avatar markdalgleish avatar moroshko avatar thetevikrant avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

autosuggest-highlight's Issues

Enhance match options

Hi,
autosuggest-highlight is a great and simple tool. I just need it to be more flexible.

I would like to:

  • search within words, not just at the beginning
  • get all matches, not just the first one
  • have a way to force matching all words in the query

Regards

In dropdown show the exact match on top instead of showing first match on top ?

Hi, thanks for nice component.
I don't know weather it is an issue, or just i am not able to figure it out properly.
When user start typing, most of the time it is the requirement to show the exact match first, then in between matches.
In order to demonstrate the problem i created similar codepan with some modification in Language array by adding entry with name CL.
Problem Example
Now if search for "cl" in input box then first result it shows is "Clojure" instead of "Cl" which is the exact match.
What i want is it must show the exact match first in result if any then those matches which are in between.
Thanks.

Option for highlighting anywhere in the text

I really like the simplicity of this utility.
The only thing missing for me is an option to highlight anywhere in any word.

I have a use case where I need to highlight different type of content in the same results, i.e:

Query: M
Result:
170627-LMP-8Y42
Marc Lecomte

For now I get:
170627-LMP-8Y42
Marc Lecomte

But I'd like:
170627-LMP-8Y42 (Here I want to highlight anywhere in the expression)
Marc Lecomte (Here the default behavior is OK)

Would you consider adding such option?

Cheers

Special chars have unexpected behavior

When using French words containing the letter œ highlighting is not working properly.

Minimal code to reproduce

const myText = "œuvre pompes test";

const mySearch = "pompes";

const m = match(myText, mySearch);
const p = parse(myText, m);

How the code behaves

m = [[7,13]]
p = [{"text":"œuvre p","highlight":false},{"text":"ompes ","highlight":true},{"text":"test","highlight":false}]

How the code is expected to behave

m = [[6,12]]
p = [{"text":"œuvre ","highlight":false},{"text":"pompes","highlight":true},{"text":" test","highlight":false}]

Hypothesis

The character œ is somehow considered as having size two in the code of match which is inconsistent with Javascript's behavior when slicing strings.

Version 3.3.0 breaks IE11

I have done my research and found out that the latest version of this library (v3.3.0) is breaking Internet Explorer.

In our case, IE11.

The problem is that with this latest version there is some ES6 code coming (like const).

Getting ReferenceError: self is not defined with 3.3.3

Hello, I use autosuggest-highlight with Next.js.

I'm getting this error after I bumped up the module to 3.3.3.

error - ReferenceError: self is not defined
    at Object.<anonymous> (/the/path/to/node_modules/autosuggest-highlight/match/index.js:1:232)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.autosuggest-highlight/match (/a/path/to/.next/server/pages/[id].js:1935:18)
    at __webpack_require__ (/a/path/to/.next/server/webpack-runtime.js:33:42)
    at eval (webpack-internal:///./components/modules/a-component/index.tsx:14:85) {
  page: '/[id]'
}

I checked ./node_modules/autosuggest-highlight/match/index.js of 3.3.3 and it has a self like this.

! function(e, t) {
    "object" == typeof exports && "object" == typeof module ? module.exports = t() : "function" == typeof define && define.amd ? define([], t) : "object" == typeof exports ? exports.AutosuggestHighlightMatch = t() : e.AutosuggestHighlightMatch = t()
}(self, (() => {
    return e = {
...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.