Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

autosuggest-trie

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

autosuggest-trie

Minimalistic trie implementation for autosuggest and autocomplete components.

  • 2.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Build Status Coverage Status npm Version gzip size

Autosuggest Trie

Minimalistic trie implementation for autosuggest and autocomplete components.

Installation

npm install autosuggest-trie --save

Basic Usage

import createTrie from 'autosuggest-trie';

const locations = [
  {
    id: 1,
    name: 'East Richmond 1234 VIC',
    population: 10000
  },
  {
    id: 2,
    name: 'East Eagle 1235 VIC',
    population: 5000
  },
  {
    id: 3,
    name: 'Richmond West 5678 VIC',
    population: 4000
  },
  {
    id: 4,
    name: 'Cheltenham 3192 Melbourne VIC',
    population: 7000
  },
  {
    id: 5,
    name: 'Richmond 6776 VIC',
    population: 3000
  }
];

const trie = createTrie(locations, 'name');

console.log(trie.getMatches('richmond e'));
/*
  [ { id: 1, name: 'East Richmond 1234 VIC', population: 10000 } ]
*/

console.log(trie.getMatches('ri', { limit: 2 }));
/*
  [ { id: 3, name: 'Richmond West 5678 VIC', population: 4000 },
    { id: 5, name: 'Richmond 6776 VIC', population: 3000 } ]
*/

API

FunctionDescription
createTrie(items, textKey, options)Creates a trie containing the given items.
getMatches(query, options)Returns items that match the given query.

createTrie(items, textKey, options)

Creates a trie containing the given items.

ParameterTypeRequiredDescription
itemsArrayArray of items. Every item must be an object.
textKeyStringKey that every item in items must have.
item will be inserted to the trie based on item[textKey].
optionsObjectAdditional options

Possible options:

OptionTypeDefaultDescription
comparatorFunctionnoneItems comparator, similar to Array#sort's compareFunction.
It gets two items, and should return a number.

Note: Matches in the first word (let's call it "group 1") are prioritized over matches in the second word ("group 2"), which are prioritized over matches in the third word ("group 3"), and so on.
comparator will only sort the matches within each group.

When comparator is not specified, items within each group will preserve their order in items.
splitRegexRegExp/\s+/Used to split items' textKey into words.

getMatches(query, options)

Returns items that match the given query.

ParameterTypeRequiredDescription
queryStringNon-blank query string.

If query is blank, [] is returned.
optionsObjectAdditional query options.

Possible options:

OptionTypeDefaultDescription
limitNumberInfinityInteger >= 1

For example: getMatches('me', { limit: 3 }) will return no more than 3 items that match 'me'.
splitRegexRegExp/\s+/Used to split the query into words.

Running Tests

npm test

License

MIT

Keywords

FAQs

Package last updated on 02 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc