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

@ychebotaev/trie

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ychebotaev/trie

A prefix tree class

  • 0.1.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

trie

A modern, generic trie (prefix tree) data structure on TypeScript

Support anything (including objects) as a prefix

Initally built as function memoization cache but may be used anyhow

Usage

import { Trie } from '@ychebotaev/trie'

const t = new Trie([
  [['a', 'b', 'c'], 1], // Initialize from pairs (optional)
])

t.insert(['d', 'e', 'f'], 2)

t.find(['a', 'b', 'c']) // => 1
t.find(['d', 'e', 'f']) // => 1

t.delete(['d'])

t.find(['d', 'e', 'f']) // => null

As a function memoization cache (example)

import { Trie } from './Trie'

export const memoize = <Params extends unknown[], Result, Fn = (...params: Params) => Result>(fn: Fn): Fn => {
  const cache = new Trie<Params, Result>()

  return (...params: Params): Result => {
    const cachedResult = cache.find(params)

    if (cachedResult) return cachedResult

    const result = fn(...params) as Result

    cache.insert(params, result)

    return result
  }
}

Contribution

Built with vite and vitest

Run tests

npm test

Keywords

FAQs

Package last updated on 26 Sep 2022

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