JSPM

  • Created
  • Published
  • Downloads 1434
  • Score
    100M100P100Q103418F
  • License MIT

Package Exports

  • d-forest

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (d-forest) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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
  • 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'
// ]