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

treebase

v1.4.22

Published

TreeBase is a class designed to provide a convenient tree structure manipulation using CRUD operations and utility methods

Downloads

17

Readme

TreeBase

Treebase library designed to manage and manipulate tree structures. With a host of powerful utilities and a straightforward API, working with hierarchical data structures has never been easier.

Features

  • Easy Manipulation: Add, delete, update, or move nodes with ease.
  • Search: Search for nodes based on a specific property.
  • Hierarchical Tree: Construct a hierarchical tree structure from a flat list of items.
  • Dictionary: Get a key-value dictionary of all items in the tree structure.

Installation

yarn add treebase
npm install treebase

Usage

import { TreeBase } from "treebase";

const props = {
  data: {
    // this can also be tree sctructured data
    oak: { title: "Oak Item", pid: "root" }, //
    apple: { title: "Apple title", pid: "oak" },
    banana: { title: "Banan title", pid: "oak" },
    cucumber: { title: "Cucumber title", pid: "banana" },
  },
  // OPTIONS (optional)
  options: {
    pid: "pid", // key for parent id
    children: "children", // key for children
    isDir: null, // function to check if item is a directory
    defaultRoot: "root", // default root id
  },
};

const treebase = new TreeBase(props);

treebase.add({ title: "Watermelon title", pid: "apple" }); // If id is not provided, it will be generated

// tree sctructured data
let treeData = treebase.getTree();
// key value dictionary
let treeDictionary = treebase.getDictionary();

// Get tree of a specific root
let treeData = treebase.getTree("aa");

console.log(treeDictionary, treeData);

Result

// treeDictionary
{
    oak: { title: "Oak title", pid: "root" },
    apple: { title: "Apple title", pid: "oak" },
    banana: { title: "Banan title", pid: "oak" },
    cucumber: { title: "Cucumber title", pid: "banana" },
    watermelon: { title: "Watermelon title", pid: "apple" },
}

// treeData
[
  {
    id: "oak",
    title: "Oak title",
    pid: "root",
    children: [
      {
        id: "apple",
        title: "Apple title",
        pid: "oak",
        children: [
          {
            id: "watermelon",
            title: "Watermelon title",
            pid: "apple",
            children: [],
          },
        ],
      },
      {
        id: "banana",
        title: "Banana title",
        pid: "oak",
        children: [
          {
            id: "cucumber",
            title: "Cucumber title",
            pid: "banana",
            children: [],
          },
        ],
      },
    ],
  },
];

Methods

getTree(rootId, keepIndex)

Constructs a hierarchical tree structure starting from the specified root.

  • Parameters:
    • rootId: The identifier for the root of the tree (defaults to this.options.defaultRoot).
    • keepIndex: Whether to preserve the order of items based on their index (defaults to true).
  • Returns: A tree structure where each item has a list of its children.

add(item)

Adds a new item to the tree structure.

  • Parameters:
    • item: The item to be added.
  • Returns: The newly added item or the existing item if a duplicate is found.

delete(id, options?)

Removes an item from the tree structure.

  • Parameters:
    • id: The ID of the target item to delete.
    • options: {moveToRoot?, moveTo?} - If moveToRoot is set to true, the item will be moved to the root of the tree. If moveTo is set to the ID of another item, the item will be moved to the specified item's children.
  • Returns: Updated dictionary.

update(id, payload)

Updates properties of a specified item in the tree structure.

  • Parameters:
    • id: The unique identifier of the item to be updated.
    • payload: An object containing the properties to be updated.
  • Returns: The updated item with the new properties.

move(id, options)

Moves a child to a different parent or position within the tree structure.

  • Parameters:

    • id: The unique identifier of the child to be moved.
    • options: {pid?, index?} - If pid is set to the ID of another item, the child will be moved to the specified item's children. If index is set to a number, the child will be moved to the specified position within the parent's children.
  • Returns: The updated dictionary after the move operation.


search(path, value)

Search for an item in the tree structure.

  • Parameters:
    • path: The path to the property to search for.
    • value: The value to search for.
  • Returns: A list of items that match the search criteria.

getDirectChildren(id)

Retrieves the direct children of a specified parent item.

  • Parameters:
    • id: The unique identifier of the parent item.
  • Returns: List containing all the direct children of the specified parent item.

getDeepChildren(pid)

Retrieves all descendants (deep children) of a specified parent item.

  • Parameters:
    • pid: The unique identifier of the parent item.
  • Returns: A flattened list containing all descendants of the specified parent item.

haveChildren(id)

Checks if an item has any children.

  • Parameters:
    • id: target item.
  • Returns: true if the item has children, false otherwise.

getParents(id, rootId)

Get all parents of a child until the root item.

  • Parameters:
    • id: target child.
    • rootId: root item id.
  • Returns: Array of parent ids.

isDeepParent(id, pid)

Checks if an item is a deep parent of another item.

  • Parameters:
    • id: child item ID.
    • pid: potential parent item ID.
  • Returns: true if pid is a parent of id, false otherwise.

reindexDirectChildren(pid)

Re-indexes the direct children of a specified parent item.

  • Parameters:
    • pid: The unique identifier of the parent item.
  • Returns: The updated dictionary after reindexing the children.

updateDictionaryFromList(list)

Update dictionary object from an array of items.

  • Parameters:
    • list: list items.
  • Returns: None.