Socket
Socket
Sign inDemoInstall

trie-autocomplete

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    trie-autocomplete

A fast and lightweight autocomplete library using a trie data structure.


Version published
Weekly downloads
82
increased by2.5%
Maintainers
1
Install size
6.38 kB
Created
Weekly downloads
 

Readme

Source

autocomplete-trie

This library uses a prefix trie data structure to efficiently generate autocomplete suggestions.

Usage

const AutoComplete = require('trie-autocomplete');
const trie = new AutoComplete();

API

.add( word: String )

Adds a word to the trie. Words inside the trie can be return as suggestions later.

Example:

// Create a new AutoComplete trie
const trie = new AutoComplete();

// Add "hello" to the trie
trie.add('hello');

// AutoComplete trie now contains "hello"

.contains( word: String )

Tests if the AutoComplete trie contains a given word. REturns a boolean.

Example:

// Create an empty AutoComplete trie
const trie = new AutoComplete();

// Trie shouldn't contain anything yet. This returns false
let containsHello = trie.contains('hello')

trie.add('hello');

// This returns true now
containsHello = trie.contains('hello')

.suggest( prefix: String )

Takes a word prefix and returns an array of all words in the tree which begin with the prefix.

Rather than searching the whole tree, .suggest() will search the subtree rooted at the last character in the prefix, eliminating most of the nodes from the search.

Example:

// Create empty AutoComplete trie
const trie = new AutoComplete();

// Add some words
trie.add('hello');
trie.add('helium');
trie.add('help');
trie.add('happy');
trie.add('cat');
trie.add('catastrophe');

// Get suggestions for the prefix 'he'.
const suggestions = trie.suggest('he'); // ['hello', 'helium', 'help']

Keywords

FAQs

Last updated on 09 Apr 2019

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc