d-forest
TypeScript icon, indicating that this package has built-in type declarations

3.2.4 • Public • Published

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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
3.2.4998latest

Version History

VersionDownloads (Last 7 Days)Published
3.2.4998
3.2.20
3.2.10
3.2.00
3.1.00
3.0.10
3.0.00
2.3.21
2.3.11
2.3.00
2.2.71
2.2.60
2.2.52
2.2.41
2.2.30
2.2.12
2.2.00
2.1.90
2.1.81
2.1.70
2.0.111
2.0.90
2.0.70
2.0.61
2.0.50
2.0.11
2.1.60
2.1.53
2.1.11
2.1.33
2.1.01
2.0.02
1.6.44
1.6.32
1.6.22
1.6.03
1.5.63
1.5.51
1.5.41
1.5.31
1.5.22
1.5.10
1.5.00
1.4.71
1.4.61
1.4.50
1.4.40
1.4.31
1.4.21
1.4.12
1.4.02
1.3.61
1.3.50
1.3.40
1.3.30
1.3.21
1.3.10
1.3.02
1.2.51
1.2.40
1.2.31
1.2.23
1.2.10
1.2.00
1.1.91
1.1.80
1.1.71
1.1.61
1.1.50
1.1.41
1.1.30
1.1.21
1.1.10
1.1.01
1.0.170
1.0.162
1.0.150
1.0.142
1.0.131
1.0.120
1.0.110
1.0.100
1.0.91
1.0.80
1.0.71
1.0.60
1.0.50
1.0.40
1.0.32
1.0.20
1.0.10
1.0.01

Package Sidebar

Install

npm i d-forest

Weekly Downloads

1,072

Version

3.2.4

License

MIT

Unpacked Size

45.7 kB

Total Files

28

Last publish

Collaborators

  • akshay9136