JSPM

  • Created
  • Published
  • Downloads 1152
  • Score
    100M100P100Q103209F
  • License MIT

Find nested object in a tree-like structure

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 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 }

// 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 | findLeaf

  • findNodes | findLeaves

  • forEachNode | forEachLeaf

  • mapLeaves

df.mapLeaves(data, (leaf) => leaf.name);
// ['category1', '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 === 'product22');
nodes.map((node) => node.name).filter(Boolean);
// ['category2', 'product22']
  • findPath

df.findPath(data, (node) => node.name === 'product22');
// [ 'c2', 'products', 'p2' ]