easytries
v1.0.6
Published
a ~7kb, typed, zero-dependency trie library
Downloads
4
Maintainers
Readme
Description
A ~7 kb, typed, zero-dependency Tries library.
easyTries is a Trie tree data structure implementation for lookaheads, autocompletes on strings or types that coerce to strings. It is most commonly used for quickly filtering a large set of strings down based on some user input. easyTries is able to filter down a tree of 1,000,000 words down in 0 to 3 ms on our machines. for a quick comparison and benchmark of easyTries, run npm run benchmark
.
Structure
example Trie Tree for the words 'CAT', 'CAN' & 'CATHY':
|Trie 'C'
|_ children
|_ Trie 'A'
|_ children
|_ Trie 'T'
| |_ children
| | |_ Trie 'H'
| | |_ children
| | |_ Trie 'Y'
| | |_ children
| | |_ word
| |_ word
|_ Trie 'N'
|_ children
|_ word
Usage
npm i easytries
Support for Both ES6 imports and CommonJS
import { easyTries } from "easytries";
const { easyTries } = require("easytries");
const trie = easyTries();
trie.set("adam");
trie.set("adam's apple");
trie.set("ads");
trie.set("abe");
trie.get("ad"); // expected [ 'ads', 'adam', 'adam\'s apple']
Options
By default,
- Strings are trimmed before being set into the tree
- Casing is preserved
- No minimum string length required on get operations
You can change One or all of these initial settings by initializing easyTries with options.
Trim
let trie = easyTries({ trim: false }); // default is true
trie.set(" rice");
trie.set("rinse ");
trie.get("ri"); // expected []
trie.get(" "); // expected [" rice"]
Casing
let trie = easyTries(); //default
trie.set("STEW");
trie.set("Stew");
trie.set("Stanley");
trie.get("ST"); // expected ["STEW"]
trie.get("st"); // expected []
let trie = easyTries({ casing: "upper" });
trie.set("STEW");
trie.set("Stew");
trie.set("Stanley");
trie.get("ST"); // expected ["STEW", "STANLEY"]
trie.get("st"); // expected ["STEW", "STANLEY"]
let trie = easyTries({ casing: "lower" });
trie.set("STEW");
trie.set("Stew");
trie.set("Stanley");
trie.get("ST"); // expected ["stew", "stanley"]
trie.get("st"); // expected ["stew", "stanley"]
Get Depth
Only return results after a certain string length is met. Probably only useful for extremely large sets, over-the-network operations, or if you want to filter out strings smaller than a particular length.
let trie = easyTries({ startAt: 3, casing: "lower" });
trie.set("I"); // will never get returned
trie.set("int");
trie.set("inert");
trie.set("infinity");
trie.get("i"); // expected []
trie.get("in"); // expected []
trie.get("inf"); // expected ["infinity"]