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

trie-dictionary

v0.0.7

Published

Trie implemented dictionary

Downloads

5

Readme

Trie Dictionary

A TypeScript implementation of a Trie (prefix tree) data structure, allowing efficient insertion, deletion, lookup, and traversal of words. This package is suitable for various applications such as autocomplete systems, spell checkers, and more.

Features

  • Insert Words: Insert words with optional associated data.
  • Find Words: Search for words and retrieve associated data.
  • Delete Words: Remove words from the Trie.
  • Traverse: Traverse and retrieve all words stored in the Trie along with their associated data.
  • Autocomplete: Get autocomplete suggestions based on a given prefix.

Installation

Install the package via npm:

npm install trie-dictionary

Usage

Here’s how you can use the trie-dictionary in your TypeScript project:

import initTrie from 'trie-dictionary';

interface MyDataType {
  someProperty: string;
}

const trie = initTrie<MyDataType>({ isAppend: false });

// Insert words
trie.insert('hello', { someProperty: 'world' });
trie.insert('hell', { someProperty: 'fire' });

// Find words
const foundHello = trie.find('hello');
console.log(foundHello); // { exists: true, data: { someProperty: 'world' } }

const foundHeaven = trie.find('heaven');
console.log(foundHeaven); // { exists: false }

// Delete words
const deleted = trie.delete('hell');
console.log(deleted); // true

// Traverse the Trie
const allWords = trie.traverse();
console.log(allWords); // [{ key: 'hello', data: { someProperty: 'world' } }]

// Autocomplete suggestions
const suggestions = trie.complete('he');
console.log(suggestions); // [{ key: 'hello', data: { someProperty: 'world' } }, { key: 'hell', data: { someProperty: 'fire' } }]

API

initTrie<Type>(options: TrieInitOptions): Trie<Type>

Initializes a new Trie instance.

  • options: An object with the following property:
    • isAppend: A boolean indicating whether to overwrite data for existing words or append new data. Default is false.

Returns an instance of Trie.

Trie<Type>

insert(key: string, data?: Type): boolean

Inserts a word into the Trie with optional associated data.

  • key: The word to insert.
  • data: Optional data associated with the word.

Returns true if the word was successfully inserted, otherwise false.

find(key: string): FindOutput<Type>

Finds a word in the Trie.

  • key: The word to find.

Returns an object with exists set to true if the word exists, and data containing the associated data if it exists.

delete(key: string): boolean

Deletes a word from the Trie.

  • key: The word to delete.

Returns true if the word was successfully deleted, otherwise false.

traverse(): TraverseOutput<Type>

Traverses the Trie and returns an array of objects containing all stored words along with their associated data.

complete(prefix: string): CompleteOutput<Type>

Gets autocomplete suggestions based on a given prefix.

  • prefix: The prefix to search for autocomplete suggestions.

Returns : An array of objects, where each object contains a key (suggested word) and its associated data.

Use Cases

The Trie data structure can be used in various applications, including but not limited to:

  • Autocomplete Systems: Provide real-time suggestions as users type.
  • Spell Checkers: Efficiently check the validity of words and offer corrections.
  • Search Engine Indexing: Quickly retrieve relevant data based on prefixes.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Bugs

If you find any issues, please report them here.