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

operation-tree-node

v1.1.4

Published

Operation method collection for tree node(like operation array).

Downloads

137

Readme

Operation method collection for tree node(like operation array).

Usage

  • Install
npm install --save operation-tree-node

API

common arguments:

| name | type | description | | ---------- | --------------------------------------------------- | ---------------------------------------------------------------------------------------------- | | data | object[] | example: [{ id: 1, name: "1", children: [{ id: 2, name: "2" }] }] | | callback | (node, index, arr, parent) => boolean/object/void | node: tree node, index: parent children's index, arr: parent children, parent: parent node | | props | { id: string, children: string, parent: string } | tree node's 'children', 'parent' and 'id' key name |

Base

treeEach

treeEach(data, callback, props) tree node each(like Array.prototype.forEach).

recursive will break until callback is false.

import { treeEach } from "operation-tree-node";

const treeData1 = [{ id: 1, name: "1", children: [{ id: 2, name: "2" }] }];

treeEach(treeData1, (node, index, arr, parent) => {
  console.log(node.name, index, arr, parent);
});
// '1', 0, [treeData], undefined
// '2', 0, [treeData[0].children], [treeData[0]]

// use children props
const treeData2 = [{ id: 1, name: "1", child: [{ id: 2, name: "2" }] }];
treeEach(treeData2, console.log, { children: "child" });

treeFilter

treeFilter(data, callback, props, isStrictly) tree node filter(like Array.prototype.filter).

get a new data instead of change source.

import { treeFilter } from "operation-tree-node";

const treeData = [
  { id: 1, name: "1", child: [{ id: 2, name: "2" }] },
  { id: 3, name: "3" },
];

const result = treeFilter(treeData, (node) => node.id === 2, {
  children: "child",
});
console.log(result);
// [{ id: 1, name: '1', child: [{ id: 2, name: '2' }] }]

treeMap

treeMap(data, callback, props) tree node map(like Array.prototype.map).

get a new data instead of change source.

import { treeMap } from "operation-tree-node";

const treeData = [{ id: 1, name: "1", children: [{ id: 2, name: "2" }] }];

// tree node's +1
const newData = treeMap(treeData, (node) => ({
  id: node.id + 1,
  name: node.name,
  ...(node.children ? { children: node.children } : {}),
}));
console.log(newData);
// [{ id: 2, name: '1', children: [{ id: 3, name: '2' }] }]

treeFind

treeFind(data, callback, props) tree node find(like Array.prototype.find).

recursive will break until found.

import { treeFind } from "operation-tree-node";

const treeData = [
  { id: 1, name: "1", children: [{ id: 2, name: "2" }] },
  { id: 1, name: "1", children: [{ id: 2, name: "2" }] },
];

const find = treeFind(treeData, (node) => node.id === 2);
console.log(find);
// { id: 2, name: '2' }

treeToFlatArray

treeToFlatArray(data, callback, props) tree to flat array.

get a flat array and source data structure is not change.

import { treeToFlatArray } from "operation-tree-node";

const treeData = [{ id: 1, name: "1", children: [{ id: 2, name: "2" }] }];

const result = treeToFlatArray(treeData);
console.log(result);
// [
//   { id: 1, name: '1', children: [{ id: 2, name: '2' }] },
//   { id: 2, name: '2' }
// ]

Advance

treeSort

treeSort(data, callback, props) tree node sort(like Array.prototype.sort).

get a new data instead of change source.

| name | type | description | | ---------- | ----------------------------------- | -------------------------------------------------- | | data | (same) | (same) | | callback | (currentNode, nextNode) => number | currentNode/nextNode: tree node, compare with them | | props | (same) | (same) |

const treeData = [
  {
    id: 1,
    name: "1",
    children: [
      { id: 3, name: "3" },
      { id: 2, name: "2" },
    ],
  },
];

// 1,3,2 => 1,2,3
const newData = treeSort(
  treeData,
  (currentNode, nextNode) => currentNode.id - nextNode.id
);
console.log(newData);
// [
//   {
//     id: 1,
//     name: '1',
//     children: [{ id: 2, name: '2' }, { id: 3, name: '3' }]
//   }
// ]);

treeCheck

treeCheck(data, checkIds, props) tree node check.

get all associated node'id by check one node.

| name | type | description | | ---------- | ------------------- | --------------------- | | data | (same) | (same) | | checkIds | number[]/string[] | will checked node ids | | props | (same) | (same) |

import { treeCheck } from "operation-tree-node";

const treeData = [{ id: 1, name: "123", children: [{ id: 2, name: "2" }] }];
const resultIds = treeCheck(treeData, [2], {
  id: "id",
  children: "children",
  parent: "parent",
});
console.log(resultIds);
// [2, 1]

treeEachParent

treeEachParent(data, callback, props) tree node each parent.

recursive will break until callback is false.

| name | type | description | | ---------- | -------------------------- | ---------------------------------------------------------- | | data | (same) | (same) | | callback | (parent) => void/boolean | parent: parent node, if callback false, skip parent.parent | | props | (same) | (same) |

import { treeEachParent } from "operation-tree-node";

const treeData = [
  { id: 1, name: "123", children: [{ id: 2, name: "2", parent: null }] },
];
treeData[0].children[0].parent = treeData[0];

const names = [];
treeEachParent(treeData[0].children, (parent) => !!names.push(parent.name));
console.log(names);
// ['123']

treeFindParent

treeFindParent(node, callback, props) tree node find parent.

import { treeFindParent } from "operation-tree-node";

const treeData = [
  {
    id: 1,
    name: "123",
    parent: null,
    children: [{ id: 2, name: "2", parent: null, children: [] }],
  },
];
treeData[0].children[0].parent = treeData[0];

const find = treeFindParent(treeData[0].children[0], (node) => node.id === 1);
console.log(find);
// { id: 1, name: '123', children: ... }

treeMerge

treeMerge(data, callback, props) tree node merge(same level).

get a new data instead of change source.

| name | type | description | | ---------- | ------------------------------------ | -------------------------------------------------- | | data | (same) | (same) | | callback | (currentNode, nextNode) => boolean | currentNode/nextNode: tree node, compare with them | | props | (same) | (same) |

import { treeMerge } from "operation-tree-node";

const treeData = [
  { id: 1, name: "1", type: "1", children: [{ id: 2, name: "2" }] },
  { id: 3, name: "3", type: "1", children: [{ id: 4, name: "4" }] },
];

const result = treeMerge(
  treeData,
  (curr, next) => curr.type && curr.type === next.type
);
console.log(result);
// [
//   {
//     id: 1,
//     name: '1',
//     type: '1',
//     children: [{ id: 2, name: '2' }, { id: 4, name: '4' }]
//   }
// ]

treeAppendParent

treeAppendParent(data, props) append parent to each tree node.

the parent is a non-enumerable property, and the method will change the source.

import { treeAppendParent } from "operation-tree-node";

const treeData = [{ id: 1, name: "1", children: [{ id: 2, name: "2" }] }];

treeAppendParent(treeData);
console.log(treeData);
// [{ id: 1, name: '1', parent: null, children: [{ id: 2, name: '2', parent: {} }] }]

treeAppendLevel

treeAppendLevel(data, props) append level to each tree node.

the level is a non-enumerable property, and the method will change the source.

import { treeLevelParent } from "operation-tree-node";

const treeData = [{ id: 1, name: "1", children: [{ id: 2, name: "2" }] }];

treeAppendLevel(treeData);
console.log(treeData);
// [{ id: 1, name: '1', level: 0, children: [{ id: 2, name: '2', level: 1 }] }]

treeNodeLevel

treeNodeLevel(node, props) get tree node level.

the method needs parent link, and level start with 0.

import { treeAppendParent, treeNodeLevel } from "operation-tree-node";

const treeData = [{ id: 1, name: "1", children: [{ id: 2, name: "2" }] }];

// link parent
treeAppendParent(treeData);

const level = treeNodeLevel(treeData[0]); // 0
const level = treeNodeLevel(treeData[0].children[0]); // 1