Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

operation-tree-node

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

operation-tree-node

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

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
57
increased by200%
Maintainers
1
Weekly downloads
 
Created
Source

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

Usage

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

Methods

common arguments:

nametypedescription
dataobject[]example: [{ id: 1, name: "1", children: [{ id: 2, name: "2" }] }]
callback(node, index, arr, parent) => object/voidnode: tree node, index: parent children's index,
arr: parent children, parent: parent node
props{ children: string, parent: string }tree node's 'children' and 'parent' name

examples:

  • treeEach(data, callback, props) tree node each
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" });
  • treeMap(data, callback, props) tree node map
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' }] }]
  • treeFilter(data, callback, props) tree node filter
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' }] }]
  • treeToFlatArray(data, callback, props) tree to flat array
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' }
// ]
  • treeMerge(data, callback, props) tree node merge(level)

arguments:

nametypedescription
data(same)(same)
callback(currentNode, nextNode) => booleancurrentNode/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' }]
//   }
// ]
  • treeSort(developing)

Keywords

FAQs

Package last updated on 29 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc