Napoleon's Penguin Mascot
Publish your own packages to the world's most popular software ecosystem with npm Pro.Get started »

substring-trie

1.0.2 • Public • Published

substring-trie

Minimalistic trie implementation for prefix searches. It finds the longest string from the dictionary that matches the beginning of the input string, or undefined if no matching string can be found.

npm install substring-trie
var Trie = require('substring-trie');
 
var trie = new Trie(['banana', 'grape', 'grapefruit'])
 
trie.search('banana'); // 'banana'
trie.search('banan'); // undefined
trie.search('bananas'); // 'banana'
trie.search('grape'); // 'grape'
trie.search('grapefruit'); // 'grapefruit'
trie.search('grapefruit and other fruit'); // 'grapefruit'

Note this is not designed for autosuggestions. Instead this is designed as a replacement for regex-based prefix searching:

var regex = /^(banana|grapefruit|grape)/
 
'banana'.match(regex); // ['banana']
'banan'.match(regex); // null
'bananas'.match(regex); // ['banana']
'grape'.match(regex); // ['grapefruit']
'grapefruit'.match(regex); // ['grapefruit']
'grapefruit and other fruit'.match(regex); // ['grapefruit']

Keywords

Install

npm i substring-trie

DownloadsWeekly Downloads

714

Version

1.0.2

License

Apache-2.0

Last publish

Collaborators

  • avatar