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

@tkg808/trie-search

v1.1.0

Published

A flexible implementation of a trie (prefix tree) to let you efficiently find partial and complete matches of stored values. A trie is a tree-like data structure, also referred to as a prefix tree, that enables optimized searching for specific keys from a

Downloads

2

Readme

Introduction

A flexible implementation of a trie (prefix tree) to let you efficiently find partial and complete matches of stored values. A trie is a tree-like data structure, also referred to as a prefix tree, that enables optimized searching for specific keys from a set.

Why use a trie?

If you are unfamiliar with tries, this implementation can seem excessive considering there are other options for searching or filtering strings. So here I'll attempt to analyze the efficiency of using a trie.

String-to-String

Consider two strings of length n, str1 and str2. To determine whether these two strings are equal, typically one would iterate each index and compare the characters at each index until a mismatch is found or until all characters are proven equal. Therefore, iteratively comparing the equality of two strings is linear O(n).

String-to-Array

Now consider an array of length m, arr, containing strings of length n, and we want to determine if a string, str, exists in arr. In other words, for each element in arr, one might perform a string-to-string comparison with str until a match is found or until all elements of arr have been checked. Therefore, comparing the equality of a string to an array of strings has a time complexity of O(m * n).

Trie

Now instead of using the string-to-array approach, which can be costly with frequent searching, we can perform the same search in linear time by using a trie.

So let's say we have the following array:

let arr = ["team", "tree", "trie"];

The trie for arr would look something like the following tree:

trie-example

Now we can perform a search for a key by traversing the nodes of the tree for each character of the key. The result is either we are able to find a path that contains all characters in the key, or the key does not exist in the trie. Therefore, we have a search with a linear time complexity O(n).

Installation

npm i @tkg808/trie-search

Usage

Importing Trie-Search gives you the Trie class:

const Trie = require("@tkg808/trie-search");

Methods

getAll()

  • Get all keys stored in the trie
  • No parameters
  • Returns an array of strings

hasKey(key)

  • Check if a given string completely matches any keys in the trie
  • key -- required string parameter
  • Returns a boolean

hasPrefix(prefix)

  • Check if a given string partially matches any keys in the trie
  • prefix -- required string parameter
  • Returns a boolean

getMatches(prefix, limit)

  • Get all keys that partially match a given string with ability to limit the number of matches returned
  • prefix -- required string parameter
  • limit -- optional integer parameter
  • Returns an array of strings

Example

const Trie = require("@tkg808/trie-search");
const states = ["Colorado", "Texas", "California", "Florida"];
const statesTrie = new Trie(states);

statesTrie.getAll(); // returns ["Colorado", "Texas", "California", "Florida"]

statesTrie.hasKey("California"); // complete match -> returns true
statesTrie.hasKey("Cal"); // only a partial match -> returns false
statesTrie.hasKey("california"); // case-sensitive -> returns false
statesTrie.hasKey(""); // empty string -> returns false

statesTrie.hasPrefix("California"); // partial and complete match -> returns true
statesTrie.hasPrefix("Cal"); // partial match -> returns true
statesTrie.hasPrefix("cal"); // case-sensitive -> returns false
statesTrie.hasPrefix(""); // empty string -> returns true

statesTrie.getMatches(""); // no limit -> returns ["California, "Colorado", "Florida", "Texas"]
statesTrie.getMatches("", 3) // limit included -> returns ["California, "Colorado", "Florida"]
statesTrie.getMatches("C"); // returns ["California, "Colorado"]
statesTrie.getMatches("Co"); // returns ["Colorado"]
statesTrie.getMatches("Coa"); // returns []

Contribution Guidelines

How to Contribute

Feel free to contribute to this package with code or suggestions. If you would like to contribute code - install the package, checkout to a new branch from dev, play with the code, then submit a pull request. Otherwise, you can submit an issue on the repo detailing your suggestion.

How to Submit Bugs

You can submit an issue on the git repo. You can also checkout to a new branch from dev, implement a solution, and then submit a pull request. Please include detailed description of the bug and your proposed solution as best as possible.

Future Features

  • Support for optional case-sensitivity
  • Update parameter structure to allow multiple options