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

dendrologist

v0.1.5

Published

A simple, fast, and lightweight tree modification library for JavaScript.

Downloads

6

Readme

Dendrologist 🌲

This library provides a set of functions to manipulate a tree data structure where each node has an id and a list of children. The tree is represented by a Node interface:

interface Node {
  id: string;
  [key: string]: any;
}

Installation

npm install dendrologist
yarn install dendrologist
pnpm install dendrologist

Functions

export function getNodeById(
  node: Node | null | undefined,
  id: string,
  childrenKey: string = "children"
): Node | null {
  // Implementation details...
}

This function takes a node (the root of the tree), an id to search for, and an optional childrenKey that specifies the property name for the children array in each node. It performs a depth-first search (DFS) to find the node with the matching id and returns it. If the node is not found, it returns null.

export function updateNode(
  node: Node,
  id: string,
  newData: Node,
  childrenKey: string = "children"
): Node | null {
  // Implementation details...
}

This function takes a node, an id to identify the node to update, newData containing the updated properties, and an optional childrenKey to specify the property name for the children array. It performs a DFS to find the node with the matching id, updates its properties with newData, and returns the updated node. If the node is not found, it returns null.

export function deleteNode(
  node: Node | null | undefined,
  id: string,
  childrenKey: string = "children"
): Node | null {
  // Implementation details...
}

This function takes a node, an id to identify the node to delete, and an optional childrenKey to specify the property name for the children array. It performs a DFS to find the node with the matching id, removes it from its parent's children array, and returns the modified tree. If the node is not found, it returns null.

export function addNode(
  tree: Node | null | undefined,
  parentId: string,
  newNode: Node,
  position: number,
  childrenKey: string = "children"
): Node {
  // Implementation details...
}

This function takes a tree (the root of the tree), a parentId to identify the parent node where the new node will be added, newNode containing the properties of the new node, position specifying the index at which the new node should be inserted, and an optional childrenKey to specify the property name for the children array. It performs a DFS to find the parent node with the matching parentId, inserts the newNode at the specified position in the parent's children array, and returns the modified tree.

export function moveNode(
  node: Node | null | undefined,
  nodeId: string,
  newParentId: string,
  childrenKey: string = "children"
): Node | null {
  // Implementation details...
}

This function takes a node (the root of the tree), a nodeId to identify the node to move, a newParentId to identify the new parent node, and an optional childrenKey to specify the property name for the children array. It performs a DFS to find the node with the matching nodeId, removes it from its current parent's children array, and adds it to the children array of the node with the matching newParentId. It returns the modified tree. If either the node or the new parent is not found, it throws an error.

Error Handling

All functions throw an error if any of the input parameters are null or undefined. The moveNode and addNode functions also throw an error if the target parent node is not found in the tree.

Performance

All functions use an iterative DFS approach with a stack for tree traversal, which is efficient in terms of memory usage. They also create a deep copy of the tree before making modifications, ensuring that the original tree is not mutated.