Giter Club home page Giter Club logo

ptrie's Introduction

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.

ptrie's People

Contributors

mfursov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.