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

d-forest

v3.2.4

Published

Find nested object in a tree-like structure

Downloads

3,355

Readme

d-forest

npm version Build Status Coverage Status Known Vulnerabilities npm downloads/month

A lightweight JavaScript library for searching object in a tree-like structure.

Install

npm install d-forest --save

Usage

const df = require('d-forest');

// data can have array of objects
const data = {
    c1: { name: 'category1', active: false },
    c2: {
        name: 'category2',
        active: true,
        products: {
            p1: { name: 'product21', active: false },
            p2: { name: 'product22', active: true },
            p3: { name: 'product23', active: false },
        },
    },
    c3: {
        name: 'category3',
        active: true,
        products: {
            p1: { name: 'product31', active: false },
            p2: { name: 'product32', active: true },
        },
    },
};

// "node" can be any object on the tree
df.findNode(data, (node) => node.name === 'category3');
// { name: 'category3', active: true, products: [Object] }

// "leaf" can be any object which don't have children i.e. bottom nodes
df.findLeaf(data, (leaf) => leaf.name === 'product22');
// { name: 'product22', active: true }

// this method is useful when you know that the object you want to find is a leaf
// note that every leaf is a node but not every node is a leaf

Methods

  • findNode | findLeaf

  • findNodes | findLeaves

  • forEachNode | forEachLeaf

  • mapLeaves

let level = df.maxHeight(data) - 1;
// level argument (optional) can be used to map leaves at specific level
df.mapLeaves(data, (leaf) => leaf.name, level);
// ['product21', 'product22', 'product23', 'product31', 'product32']
  • everyNode | everyLeaf

df.everyNode(data, (node) => node.hasOwnProperty('active'));
// false
df.everyLeaf(data, (leaf) => leaf.hasOwnProperty('active'));
// true
  • minHeight | maxHeight

df.minHeight(data); // 2
df.maxHeight(data); // 4
  • nodesByLevel

// returns an array containing all nodes at given level
df.nodesByLevel(data, 1); // level >= 0
// [
//   { name: 'category1', active: false },
//   { name: 'category2', active: true, products: [Object] },
//   { name: 'category3', active: true, products: [Object] }
// ]
  • reduce

// returns single output value for each path from top to bottom
// initial value must be provided
df.reduce(data, (acc, cur) => (cur.name ? `${acc}/${cur.name}` : acc), '');
// [
//    '/category1',
//    '/category2/product21',
//    '/category2/product22',
//    '/category2/product23',
//    '/category3/product31',
//    '/category3/product32'
// ]
  • hierarchy

// returns object hierarchy from root
const nodes = df.hierarchy(data, (node) => node.name === 'product31');
nodes.map((node) => node.name).filter(Boolean);
// ['category3', 'product31']
  • findLevel

df.findLevel(data, (node) => node.name === 'category2'); // 1
df.findLevel(data, (node) => node.name === 'product21'); // 3
  • findPath | findByPath

df.findPath(data, (node) => node.name === 'product22');
// [ 'c2', 'products', 'p2' ]
df.findByPath(data, ['c2', 'products', 'p2']);
// { name: 'product22', active: true }

Following methods don't mutate data, instead return new one with shared mutable state.

  • removeByPath

  • removeNodes | removeLeaves

df.removeLeaves(data, (leaf) => leaf.active === false);
// {
//   c2: {
//     name: 'category2',
//     active: true,
//     products: { p2: [Object] }
//   },
//   c3: {
//     name: 'category3',
//     active: true,
//     products: { p2: [Object] }
//   }
// }
  • updateByPath

  • updateNodes | updateLeaves

df.updateNodes(
    data,
    (node, depth) => depth === 1 && node.active,
    (node) => ({ ...node, products: null })
);
// {
//   c1: { name: 'category1', active: false },
//   c2: { name: 'category2', active: true, products: null },
//   c3: { name: 'category3', active: true, products: null }
// }
  • removeByLevel

df.removeByLevel(data, 2);
// {
//   c1: { name: 'category1', active: false },
//   c2: { name: 'category2', active: true },
//   c3: { name: 'category3', active: true }
// }