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 🙏

© 2025 – Pkg Stats / Ryan Hefner

treesy

v0.2.33

Published

An API for creating and manipulating tree-node structures

Downloads

14

Readme

Treesy - An API for creating and manipulating tree-node structures

This library is meant to be used for creating and manipulating tree-node structures.

The API is designed with ease of use in mind, hence the name Treesy (Tree + Easy - get it? 😂 - I'm sorry).

I created it for a side project I'm working on because I couldn't find a library that has a simple API and is "just" for creating tree structures. Oh and it doesn't have any dependencies :)

Warning! The API is not final and is most likely to change!

Installation

yarn add treesy
npm install treesy

Or through a CDN

<script src="https://unpkg.com/treesy"></script>

Usage

Basic usage

import { Tree } from "treesy";

const tree = new Tree();

tree.add({ name: "bob" });

console.log(tree.toJson());
[
  {
    "id": "UUID",
    "parentId": null,
    "data": {
      "name": "bob"
    },
    "children": []
  }
]

Change the ID

tree.add("MY_UNIQUE_ID", { name: "bob" });

console.log(tree.toJSON());
[
  {
    "id": "MY_UNIQUE_ID",
    "parentId": null,
    "data": {
      "name": "bob"
    },
    "children": []
  }
]

API

Tree

constructor

Creates the tree.

You can pass an array of JsonNode's to create a tree with default data. It can be done like this:

const jsonTree = [
  {
    id: "MY_ID",
    data: {
      greeting: "Hello",
    },
    children: [],
  },
];

const tree = new Tree(jsonTree);
console.log(tree.toJson()[0].id); // MY_ID

_tree: Array<Node>

The internal tree itself but exposed.

add | (idOrData: string | AddData | Node, data?: Data): Node

This is used to add a root node.

find | (criteria: SearchCriteria, options?: SearchOptions, node?: Node): Node | undefined

Used to search the whole tree for a given node based on the search criteria.

SearchCriteria - There are 3 possible ways to use the search criteria.

  • By specifying a string which will look for a node by its ID.
  • By passing an object which will look at the data property of the Node. It does a subset comparison therefore the whole data object doesn't have to match exactly, only a subset of the fields.
  • By a callback if you wanted more control. The callback needs to return a boolean and the first argument of the callback is the current node.

The callback can be used like this:

const node = tree.find(
  node => node.data.count === 2)
);

SearchOptions - possible options include:

export type SearchOptions = {
  deep?: boolean;
};

If deep is set to tree, the finder will do a recursive search. If it's false, it'll only look at the direct children of the tree (root nodes). The default value is true

Node - Used to set the "from" point on the search. If a node is passed, it'll start the search from there and search its children only.

toJson | (): Array<JsonNode>

Generates a JSON representation of the tree.

Node

constructor | (idOrData: AddData | string | null, data?: Record<string, unknown>)

Creates the new node.

id: string

Return the ID of the node

parentId: string | null

Return the parent ID of the node. Can be null if it's a root node or if it's not part of a tree object yet.

data: Record<string, unknown>;

Data object of the node. Can be used to store anything such as state.

children: Array<Node>

Node's children.

tree: Tree

The tree itself. This can be a quick way to access the methods on the tree object.

parent: Node | undefined

Return the direct parent of a given Node.

add | (idOrData: string | AddData | Node, data?: Data): Node

Same as the add method on the Tree but this time, you are adding a new node as a child of the current node.

find | (criteria: SearchCriteria, options?: SearchOptions): Node | undefined

Same as the find method on the Tree but without the Node argument.

update | (dataToUpdate: Record<string, unknown>): Node

Changes the data object of the current Node. This method overrides the current data object so make sure you use something like Object.assign if you don't want to lose any data.

move | (criteria: SearchCriteria, index?: number): Node | undefined

This method can be used if you want to move the current node somewhere else in the tree. criteria works exactly the same as find but this time, you want to use this to find the target node - so the node that you want your current node to be a child of. If you wanted to move it to the root of the tree, you can pass root as the criteria (Note: this is not possible on the find method).

By default, the moved node is added at the bottom of the children but if you wanted to target a specific index in the children's array, you can pass it as the second argument.

This can be used like this:

const rootNode = tree.add({ greeting: "hello" });
const childNode = rootNode.add({ greeting: "hi" });

childNode.move("root");

delete | (): void

Can be used to remove the node completely.

const rootNode = tree.add({ greeting: "hello" });

rootNode.remove();

toJson | (): Array<JsonNode>

Generates a JSON representation of the node and it's children.