npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

autosuggest-trie

v2.1.1

Published

Minimalistic trie implementation for autosuggest and autocomplete components.

Downloads

3,759

Readme

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

| Function | Description | | :--- | :--- | | 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.

| Parameter | Type | Required | Description | | :--- | :--- | :---: | :--- | | items | Array | ✓ | Array of items. Every item must be an object. | | textKey | String | ✓ | Key that every item in items must have.item will be inserted to the trie based on item[textKey]. | | options | Object | | Additional options |

Possible options:

| Option | Type | Default | Description | | :--- | :--- | :---: | :--- | | comparator | Function | none | Items 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. | | splitRegex | RegExp | /\s+/ | Used to split items' textKey into words. |

getMatches(query, options)

Returns items that match the given query.

| Parameter | Type | Required | Description | | :--- | :--- | :---: | :--- | | query | String | ✓ | Non-blank query string.If query is blank, [] is returned. | | options | Object | | Additional query options. |

Possible options:

| Option | Type | Default | Description | | :--- | :--- | :---: | :--- | | limit | Number | Infinity | Integer >= 1For example: getMatches('me', { limit: 3 }) will return no more than 3 items that match 'me'. | | splitRegex | RegExp | /\s+/ | Used to split the query into words. |

Running Tests

npm test

License

MIT