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

structree

v1.1.1

Published

A versatile module for managing hierarchical tree structures in JavaScript. Supports adding branches and data, retrieving, and converting between tree and linear structures.

Downloads

16

Readme

structree

structree is a module for managing hierarchical tree structures in JavaScript.

Installation

Install the module via npm:

npm install structree

Usage

import Tree from 'structree';

// Alternatively, for client-side usage
import Tree from './path/to/node_modules/structree/structree.js';

// Create a new Tree instance
const myTree = new Tree();

// Set initial branches
myTree.setBranch([1]);
myTree.setBranch([2]);

// Add data
myTree.setData([1, 1], 'Data at 1/1');
myTree.setData([2, 1], 'Data at 2/1');

// Retrieve data
const data = myTree.get([1, 1]); // 'Data at 1/1'

// Convert to linear structure
const linearStructure = Tree.toLinearStructure(myTree.getTree());

// Convert back to tree structure
const treeStructure = Tree.toTreeStructure(linearStructure);

API

class Tree

Constructor

new Tree(tree = [], maxId = 0)
  • tree (array) — Initial tree structure.
  • maxId (number) — Maximum identifier.

Methods

  • getTree()
    • Returns the current tree.
  • getLinear()
    • Returns the current tree as a linear structure.
  • setBranch(path, content = [])
    • Adds a new branch at the specified path.
    • path (array) — Path to the new branch.
    • content (array) — Content of the branch.
  • setData(path, data)
    • Adds new data at the specified path.
    • path (array) — Path to the data.
    • data (any) — Data.
  • get(path)
    • Retrieves data or branch at the specified path.
    • path (array) — Path to the element.
  • getUnparsed(path)
    • Gets all data or branch properties at the specified path.
    • path (array) — Beginning of the path.
  • remove(path)
    • Removes the element at the specified path.
    • path (array) — Path to the element to be removed.
  • contains(path)
    • Checks if the path is contained in the tree.
    • path (array) — Path to check.

Static Methods

  • static toLinearStructure(tree)
    • Converts the tree to a linear structure.
    • tree (array) — Tree to be converted.
  • static toTreeStructure(array)
    • Converts the linear structure back to a tree.
    • array (array) — Linear structure to be converted.
  • static getContent(el, tree)
    • Returns the content of the element.
    • el (object) — Element.
    • tree (array) — Tree.
  • static getPath(el)
    • Returns the path of the element.
    • el (object) — Element.
  • static getType(el)
    • Returns the type of the element.
    • el (object) — Element.

Examples

// Create a new tree
const myTree = new Tree();

// Add branches and data
myTree.setBranch([1]);
myTree.setBranch([1, 2]);
myTree.setData([1, 2, 3], 'Data at 1/2/3');

// Retrieve data
const data = myTree.get([1, 2, 3]); // 'Data at 1/2/3'

// Remove an element
myTree.remove([1, 2, 3]);

// Check if path exists
const containsPath = myTree.contains([1, 2]); // true

// Convert tree to linear structure
const linearStructure = Tree.toLinearStructure(myTree.getTree());

// Convert back to tree structure
const treeStructure = Tree.toTreeStructure(linearStructure);

License

This project is licensed under the MIT License.