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

id-tree

v2.4.1

Published

id based ordered tree, with support for moving nodes and marking leaf only nodes

Downloads

241

Readme

id-tree

id-tree is a lightweight TypeScript library for managing tree data structures where nodes contain only a unique numerical identifier (ID). The library provides an easy-to-use API to create, modify, and traverse tree structures, as well as methods for serializing and deserializing trees in compact formats.

Features

  • Create and manage tree structures with ease
  • Add and remove nodes using unique IDs
  • Find and move nodes within the tree
  • Serialize and deserialize tree structures in compact array and object formats
  • Set and manage leaf-only nodes
  • Compatible with TypeScript and JavaScript projects

Installation

To install the id-tree library, run the following command:

npm install id-tree

Usage

Importing the library

Import the Tree class from the id-tree package:

import { Tree } from 'id-tree';

Creating a tree

Create a new instance of the Tree class:

const tree = new Tree();

Setting the root node

To set the root node of the tree, use the setRoot method:

const rootNode = tree.setRoot(1);

Adding nodes to the tree

To add a new node to the tree, use the addNode method:

const newNode = tree.addNode(parentId, nodeId);

Finding nodes in the tree

To find a node with a specific ID, use the findNode method:

const node = tree.findNode(nodeId);

Moving nodes within the tree

To move a node (and its subtree) to a new parent at a specified index, use the move method:

tree.move(nodeId, newParentId, index);

Setting leaf-only nodes

To set leaf-only nodes that cannot have children, use the setLeafs method:

tree.setLeafs([leafId1, leafId2, ...]);

Serializing the tree

To serialize the tree in a compact array format, use the toArray method:

const compactArray = tree.toArray();

To serialize the tree in a compact object format, use the toObj method:

const compactObj = tree.toObj();

Deserializing the tree

To deserialize a tree from a compact array format, use the fromArray method:

const restoredTree = Tree.fromArray(compactArray);

To deserialize a tree from a compact object format, use the fromObj method:

const restoredTree = Tree.fromObj(compactObj);

To deserialize a tree from a map of nodes, use the fromMap method:

let rootId = 100;
let pairs = [[100, 1], [1, 2]]
const tree = Tree.fromMap([rootId, pairs]);

Contributing

Contributions are welcome! Please submit a pull request or create an issue on the project's GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Support

If you need assistance or have any questions, please create an issue on the project's GitHub repository.

Attribution

This library - the code, test cases, and this documentation - were all written by ChatGPT. Atleast the v1.

ChatGPT Prompts

I used the following prompts to build this library (the main ones) -

can you write an ordered tree implementation in typescript where the only data contained in each node is the "id" of type number?

For the above implementation, how to restore a tree from a JSON?

Now in the above implementation, include the following methods -

  1. setLeafs - here you pass and array of id's that cannot have children, this is leaf only nodes
  2. fixTree - if the tree has any node with id in the leaf only array, it should move all children to the parent Add validation in addNode to throw error if it is being added to a lead only node

I need couple of methods to store this tree and restore this tree -

  • toCompact - this provides a compact representation of this tree to store in database
  • fromCompact - this recreates the tree from a compact data representation

include following method - move(node_id, parent_id, index) - this method moves the entire sub tree of node with id node_id to the node with id=parent_id at the index position of its children. If index is undefined, add as last child.

Combine all the functionality mentioned above and write out the complete code

Following is the complete code of what you implemented above, please write a comprehensive set of test cases using the jest library

continue writing the missing test cases

The test case "Compact array representation with leaf-only nodes" is failing because the leafs are not being stored when compacted. Can you find the problem in my code?

Can you fix the same issue with this test case - 'Compact object representation with leaf-only nodes'

seems I don't need to store the leafs when i compact. can you write the toArray, fromArray, toObj and fromObj so it does not store that

if i want to publish this tree implementation as an npm package, list few good names for it

I am going to call this id-tree. Write me a README.md documentation for this library