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

treezy

v0.0.5

Published

treezy is a package for storing and manipulating hierarchical data. For example, comments on Reddit or YouTube.

Downloads

294

Readme

treezy

Downloads Size

🌲 treezy is a tiny and fast Node.js package for creating and manipulating hierarchal (tree-shaped) data.

Installation

In Node.js (version 16+), install with npm:

npm install treezy

Example usage

A tree can be any JavaScript object that contains an array of children, where each child is another JavaScript object that matches the structure of its parent.

For example, here's a tree with three nodes 👇

//   A
//  / \
// B   C

const myTree = {
  id: "A",
  children: [
    { id: "B", children: [] },
    { id: "C", children: [] }
  ],
}

treezy makes it easy to do things like...

Count the number of nodes in the tree

import { getSize } from "treezy"

getSize(myTree) 
// Returns: 3

Check if the tree contains at least one node with id equal to "Q"

import { contains } from "treezy"

contains(myTree, { testFn: (x) => x.id === "Q" }) 
// Returns: false

Extract the subtree starting at the node with id equal to "B"

import { getSubtree } from "treezy"

getSubtree(myTree, { testFn: (x) => x.id === "B" }) 
// Returns: { id: "B", children: [] }

Notes

  • By default, treezy functions never modify their inputs by reference
  • treezy expects every node in a tree to include an array with child nodes
  • treezy always scans in depth-first search, pre-order
  • treezy expects every tree to have exactly one root node (at depth 0)
  • The parent of a root node is null

API

  • apply(tree, options) - apply some function to nodes in a tree
  • bifurcate(tree, options) - split a tree into two subtrees
  • contains(tree, options) - check if a tree contains a node that passes some test
  • getDepth(tree, options?) - get the number of nodes in a tree
  • getParent(tree, options) - get the parent node of some other node
  • getSignature(tree, options) - combine the node ids and structure into a unique id
  • getSize(tree, options?) - count the number of nodes in a tree
  • getSubtree(tree, options) - retrieve the subtree of a tree starting at some node
  • getValues(tree, options?) - retrieve the nodes or node properties as an array
  • insert(tree, options) - insert one tree into another
  • prune(tree, options) - delete nodes matching some criteria
  • reduce(tree, options) - apply a reducer function to a tree

All functions come with an options parameter that let you specify things such as

  • copy: should the input tree be copied prior to modification?
  • childrenProp: name of the array property in the tree storing child nodes
  • testFn: a function applied to each node, to test whether it matches some criteria

See each function's type definitions for its comprehensive documentation.

Examples

Suppose you have data for a comment thread on a YouTube video..

const comment = {
  id: 234424,
  userId: 489294,
  text: "I like dogs",
  likes: 2,
  replies: [
    {
      id: 248210,
      userId: 403928,
      text: "So do I!",
      likes: 1,
      replies: [],
    },
    {
      id: 211104,
      userId: 407718,
      text: "Meh, cats are better",
      likes: 0,
      replies: [
        {
          id: 248210,
          userId: 489294,
          text: "Kick rocks, dummy head",
          likes: 3,
          replies: [],
        },
      ],
    }
  ],
}

How do I count the total number of comments?

import { getSize } from "treezy"

getSize(comment) // 4

How do I count the number of comments by a particular user?

import { getSize } from "treezy"

getSize(comment, { testFn: (node) => node.userId === 489294 }) // 2

How do I determine the max number of likes given to any single comment?

import { reduce } from "treezy"

const reducer = (node, initVal) => Math.max(node.likes, initVal)
reduce(comment, { reduceFn: reducer, initialVal: 0 }) // 3

How do I flatten the comments into a 1-D array?

import { getValues } from "treezy"

getValues(comment) // [{id: 234424, ...}, {id: 248210, ...}, ...]

How do I retrieve all the comment values?

import { getValues } from "treezy"

getValues(comment, { getFn: (node) => node.text })
// ["I like dogs", "So do I!", ...]

How do I remove comments which are replies to replies, or deeper?

import { prune } from "treezy"

prune(comment, { testFn: (node, parent, depth) => depth >= 2 })