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

tree-factory

v3.0.0

Published

Powerful functions for various tree operations.

Downloads

502

Readme

Tree Factory

Powerful functions for various tree operations.

Installation

Using npm

npm install tree-factory
import * as treeFactory from 'tree-factory';
const treeFactory = require('tree-factory');

Using CDN

<script src="https://unpkg.com/tree-factory/dist/index.umd.cjs"></script>

You can find the library on window.treeFactory.

Usage

treeFromList

Create a tree structure data from a flat list.

  • Arguments

    source - The flat list of data to convert into a tree.

    [options] - Customization options.

    [options.id] - The key representing the unique identifier for each node.

    [options.pid] - The key representing the parent identifier for each node.

  • Returns

    The root nodes of the resulting tree structure.

  • Example

    treeFromList([{ id: 0, pid: 1 }, { id: 1 }]);
    treeFromList([{ roleId: 0, parentId: 2 }, { roleId: 2 }], {
      id: 'roleId',
      pid: 'parentId',
    });

treeToList

Pushes all nodes from the provided tree to the specified list or creates a new list.

  • Arguments

    tree - The tree from which to extract nodes.

    [options] - Customization options.

    [options.children] - The key representing children nodes in each tree node.

    [options.list] - The list to which the nodes are pushed.

  • Returns

    The flat list containing all nodes from the tree.

  • Example

    treeToList([{ value: 0, children: [{ value: 1 }] }]);
    treeToList([{ value: 0, subTree: [{ value: 1 }] }], {
      children: 'subTree',
      list: [],
    });

treeCleaner

Removes nodes from the tree that do not satisfy the predicate function.

  • Arguments

    tree - The tree to be cleaned.

    predicate - The function used to determine if a node should be removed.

    [options] - Customization options.

    [options.children] - The key representing children nodes in each tree node.

    [options.parent] - The parent node of the current node being processed.

  • Returns

    The cleaned tree.

  • Example

    treeCleaner(
      [{ value: 0, children: [{ value: 1 }, { value: 2 }] }, { value: 2 }],
      (node, parent) => node.value > 1 && parent,
    );
    treeCleaner(
      [{ value: 0, subTree: [{ value: 1 }, { value: 2 }] }],
      (node) => node.value > 1,
      { children: 'subTree' },
    );

treeTraverse

Executes a provided function once for each node in a tree.

  • Arguments

    tree - The tree to be traversed.

    handle - Function to execute for each node.

    [options] - Customization options.

    [options.children] - The key representing children nodes in each tree node.

    [options.parent] - The parent node of the current node being processed.

    [options.isPreorder] - Specifies whether to use preorder traversal (true) or postorder traversal (false)

  • Returns

    void

  • Example

    treeTraverse(
      [{ value: 0, children: [{ value: 1 }, { value: 2 }] }, { value: 2 }],
      (node, parent) => (node.value += parent.value ?? 0),
    );
    treeTraverse(
      [{ value: 0, subTree: [{ value: 1 }, { value: 2 }] }],
      (node) => node.value++,
      { children: 'subTree', isPreorder: true },
    );

treeFind

Finds the first node in the tree that satisfies the predicate function.

  • Arguments

    tree - The tree to be searched.

    predicate - The function used to determine if a node matches the search criteria.

    [options] - An object containing optional parameters.

    [options.children] - The property name representing the children nodes in each tree node.

    [options.parent] - The parent node of the current node being processed.

  • Returns

    The first node that matches the search criteria, or undefined if no match is found.

  • Example

    treeFind(
      [{ value: 0, children: [{ value: 1 }, { value: 2 }] }, { value: 2 }],
      (node, parent) => node.value > 1 && parent,
    );
    treeFind(
      [{ value: 0, subTree: [{ value: 1 }, { value: 2 }] }],
      (node) => node.value > 1,
      { children: 'subTree' },
    );