You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

trie-autocomplete

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trie-autocomplete

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

0.2.0
latest
Source
npmnpm
Version published
Maintainers
1
Created
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

autocomplete

FAQs

Package last updated on 09 Apr 2019

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.