Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ptrie

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ptrie

Lightweight Path Trie for TypeScript and JavaScript

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
252
decreased by-46.5%
Maintainers
1
Weekly downloads
 
Created
Source

Lightweight Path Trie for TypeScript and JavaScript

Path trie uses a path (array of tokens) as an index and provides fast insertion, lookup, deletion and iteration operations based on the paths and path prefixes.

Path trie can be used to store and manipulate hierarchical structures (objects, records).

The package has no external dependencies and is available under Apache License Version 2.0.

Usage examples

Get, set and delete by path. Deletion of the parent path value deletes child subtree.
import {Trie} from 'ptrie';

// Build data hierarchy.
const trie = new Trie<string, unknown>();
const org1Path = ['organization-id-1'];
const user1Path = ['organization-id-1', 'users', 'user-id-1'];
const user2Path = ['organization-id-1', 'users', 'user-id-2'];
trie.set(org1Path, organization1Info);
trie.set(user1Path, user1Info);
trie.set(user1Path, user2Info);

// Query it by path.
expect(trie.get(user1Path)).toBe(user1Info);
expect(trie.count(['organization-id-1', 'users'])).toBe(2);

// Iterate a sub-tree.
trie.visitDfs('pre-order', value => console.log('User', value), ['organization-id-1', 'users']);

// Delete a parent node (by path prefix) to delete the whole sub-tree.
expect(trie.delete(org1Path));
expect(trie.get(org1Path)).toBeUndefined();
expect(trie.get(user1Path)).toBeUndefined();
expect(trie.get(user2Path)).toBeUndefined();

See documentation for each available method in the source code and usage examples in unit tests.

Keywords

FAQs

Package last updated on 09 Sep 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc