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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@axolo/tree

v0.6.2

Published

A lightweight JavaScript library for tree data structure operations.

Readme

Tree

A lightweight JavaScript library for tree data structure operations.

Installation

npm i @axolo/tree

Usage

import Tree from '@axolo/tree'

// Sample data
const array = [
  { id: 1, parentId: null, name: 'Root' },
  { id: 2, parentId: 1, name: 'Child 1' },
  { id: 3, parentId: 1, name: 'Child 2' },
  { id: 4, parentId: 2, name: 'Grandchild' }
]

// Convert array to Tree instance
// tree.value is tree structure data
const tree = Tree.from(array)

// Convert tree to array
const resultArray = tree.toArray()

// Get path by id
const path = tree.path(4) // [{ id: 1, ... }, { id: 2, ... }, { id: 4, ... }]
const pathIds = tree.path(4, 'id') // [1, 2, 4]
const pathIndices = tree.path(4, null) // [0, 0, 0]

// Get parent node by id
const parent = tree.parent(4) // { id: 2, ... }

// Get myself node by id
const myself = tree.myself(4) // { id: 4, ... }

// Filter tree with condition function
const filtered = tree.filter(node => node.name.includes('Child'))

// Get subtree by id
const subtree = tree.sub(2) // [{ id: 2, children: [...] }]

// Get tree depth
const depth = tree.getDepth() // 3

Configuration

| Property | Type | Default | Description | | -------- | ------ | ---------- | ------------------------------------ | | id | string | 'id' | Unique identifier property name | | parentId | string | 'parentId' | Parent node identifier property name | | children | string | 'children' | Children property name | | depth | string | 'depth' | Depth property name | | leaf | string | 'leaf' | Leaf node flag property name |

API

Tree.from(array, config = {})

Convert an array to a Tree instance.

Param:

  • array {Array} - Flattened tree data array
  • config {Object} - Optional configuration

Return: {Tree} - Tree instance

new Tree(tree, config = {})

Create a new Tree instance.

Param:

  • tree {Array} - Tree structure data array
  • config {Object} - Optional configuration

Return: {Tree} - Tree instance

tree.toArray()

Convert tree back to flattened array.

Return: {Array} - Flattened tree array

tree.path(id, key = undefined)

Get the path from root to target node.

Param:

  • id {String|Number} - Target node ID
  • key {String|null|undefined} - Property key to return

Return: {Array} - Path array containing nodes, specified keys, or indices

  • key exists: Return keys like [root[key], ..., parent[key], self[key]]
  • key is null: Return indices like [root.index, ..., parent.index, self.index]
  • key is undefined: Return nodes like [root, ..., parent, self]

tree.parent(id)

Get the parent node of the target node.

Param:

  • id {String|Number} - Target node ID

Return: {Object|null} - Parent node or null

tree.myself(id)

Get the myself node of the target node.

Param:

  • id {String|Number} - Target node ID

Return: {Object|null} - Myself node or null

tree.filter(condition)

Filter tree nodes by condition.

Param:

  • condition {Function} - Filter function (receives node, return boolean)

Return: {Tree} - Tree instance

tree.sub(id)

Get the subtree rooted at target node.

Param:

  • id {String|Number} - Target node ID

Return: {Tree|null} - Tree instance or null

tree.getDepth()

Get the maximum depth of the tree.

Return: {Number} - tree depth