Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
ternary-search-trie
Advanced tools
Readme
A ternary search trie implementation in TypeScript.
You can install the package via npm or yarn.
npm install ternary-search-trie
yarn add ternary-search-trie
Represents a ternary search trie.
import { Trie } from 'ternary-search-trie';
interface Value {
data: string;
}
const trie = new Trie<Value>();
Returns true if the tree contains any nodes, otherwise false.
console.log(trie.isEmpty);
//=> true
Gets the size of the tree in terms of the number of nodes present within the tree.
console.log(trie.size);
//=> 0
set(key: string, value: Value): Trie<Value>
Adds the specified key/value pair to the tree.
Example:
const value = { data: 'test' };
trie.set('data', value);
get(key: string): Value | null
Returns the value of the node with the specified key.
Example:
const value = { data: 'test' };
trie.set('data', value);
console.log(trie.get('data'));
//=> { data: 'test' }
del(key: string): Trie<Value>
Deletes the node with the specified key.
Example:
const value = { data: 'test' };
trie.set('data', value);
console.log(trie.get('data'));
//=> { data: 'test' }
trie.del('data');
console.log(trie.get('data'));
//=> null;
contains(key: string): boolean
Checks if a node with the specified key exists in the tree.
Example:
console.log(trie.contains('foo'));
//=> false
keys(): string[]
Returns an array of all keys present in the tree.
Example:
const value = { data: 'test' };
trie.set('foo', value);
trie.set('bar', value);
trie.set('baz', value);
console.log(trie.keys());
//=> [ 'bar', 'baz', 'foo' ]
keysWithPrefix(prefix: string): string[]
Returns all keys present in the tree that begin with the specified prefix.
Example:
const value = { data: 'test' };
trie.set('foo', value);
trie.set('bar', value);
trie.set('baz', value);
console.log(trie.keysWithPrefix('ba'));
//=> [ 'bar', 'baz' ]
searchWithPrefix(prefix: string, callback: (key: string, value: Value) => void): void;
Executes the specified callback at each node in the tree whose key begins with the specified prefix.
Example:
const value = { data: 'test' };
trie.set('foo', value);
trie.set('bar', value);
trie.set('baz', value);
trie.searchWithPrefix('ba', (key, value) => console.log({ key, value }));
//=> { key: 'bar', value: { data: 'test' } }
//=> { key: 'baz', value: { data: 'test' } }
dfs(callback: (key: string, value: Value | null) => void): void;
Performs a depth-first search of the tree beginning from the root node. Executes the specified callback at each visited node.
Example:
const value = { data: 'test' };
trie.set('foo', value);
trie.dfs((key, value) => console.log({ key, value }));
//=> { key: 'f', value: null }
//=> { key: 'o', value: null }
//=> { key: 'o', value: { data: 'test' } }
Returns the tree as a string.
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
This project is licensed under the MIT License - see the LICENSE file for details
FAQs
A ternary search tree implementation in TypeScript.
The npm package ternary-search-trie receives a total of 7 weekly downloads. As such, ternary-search-trie popularity was classified as not popular.
We found that ternary-search-trie demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.