Skip to content

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

Notifications You must be signed in to change notification settings

gacord23/d-forest

 
 

Repository files navigation

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

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

Install

npm install d-forest --save

Usage

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

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

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

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

// it is useful when you know that the object you want to find is a leaf
// it has better performance over "findNode" as it skips unnecessary comparisons
// note that every leaf is a node but not every node is a leaf

Methods

  • findNode
  • findNodes
  • findLeaf
  • findLeaves
  • forEachNode
  • forEachLeaf
  • mapLeaves
  • removeNode
  • removeLeaf
  • nodesByLevel
// returns an array containing all nodes at given level
const res = df(data).nodesByLevel(1); // should be greater than 0
console.log(res);
// [
//   { 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
const res = df(data).reduce(
    (acc, cur) => (acc + '/' + cur.name), '' // initial value must be provided
);
console.log(res);
// [
//   '/categories/category1',
//   '/categories/category2/products/product21',
//   '/categories/category2/products/product22',
//   '/categories/category2/products/product23',
//   '/categories/category3/products/product31',
//   '/categories/category3/products/product32'
// ]

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%