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

cng-tree-utils

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cng-tree-utils

for javascript convert array to tree and revert

  • 0.0.2
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

Tree utils for javascript.

In most projects, the use of tree structures to simplify data structures, expand or collapse menus. This library gives you tools to convert an array to a tree, and vice versa. In addition, also supports the arrangement by tree structure, calculate the values of subtracting leaves on tree branches and stumps ...

to use in javascript in browser or angluar or react or nodejs


npm install cng-tree-utils


# test for array to tree or array sort by tree
node ./tree-utils/test/test-array-2-tree.js

# test for array to order by tree
node ./tree-utils/test/test-array-2-order-by-tree.js 


# test for tree 2 array
node ./tree-utils/test/test-tree-2-array.js 

Make array to tree

const test = require("cng-tree-utils");

let arr = [
    {
        id: 1
        , name: "A"
        , total: 0
    },
    {
        id: 2
        , name: "B"
        , total: 1
    },
    {
        id: 3
        , parent: 2
        , name: "B.1"
        , total: 2
    },
    {
        id: 4
        , parent: 1
        , name: "A.1"
        , total: 3
    },
    {
        id: 5
        , parent: 2
        , name: "B.2"
        , total: 4
    },
    {
        id: 6
        , parent: 3
        , name: "B.1.1"
        , total: 5
    },
];

// print orgin array
console.log("Origin ARRAY ***>\n", JSON.stringify(arr, null, 2));

// convert array to tree
let arr2Tree = test.array2Tree(arr, "id", "parent");
console.log("array to TREE -->\n", JSON.stringify(arr2Tree, null, 2));

Sort Array by Tree structure:

const test = require("cng-tree-utils");

let arr = [
    {
        id: 1
        , name: "A"
        , total: 0
    },
    {
        id: 2
        , name: "B"
        , total: 1
    },
    {
        id: 3
        , parent: 2
        , name: "B.1"
        , total: 2
    },
    {
        id: 4
        , parent: 1
        , name: "A.1"
        , total: 3
    },
    {
        id: 5
        , parent: 2
        , name: "B.2"
        , total: 4
    },
    {
        id: 6
        , parent: 3
        , name: "B.1.1"
        , total: 5
    },
];

// print origin Array
console.log("Origin ARRAY ***>\n", JSON.stringify(arr, null, 2));

// convert order by tree
let arr2Order = test.array2SortByTree(arr, "id", "parent");
console.log("array to Order -->\n", JSON.stringify(arr2Order, null, 2));

// sum and percent weigth in tree 
let arrOrderWeight = test.array2SortAndWeight(arr, "id", "parent", "total");
console.log("array to Order -->\n", JSON.stringify(arrOrderWeight, null, 2));

Convert tree structure into Array:

const test = require("cng-tree-utils");

let arrOrigin = 
    [
        {
            "id": 1,
            "name": "A",
            "total": 0,
            "subs": [
                {
                    "id": 4,
                    "parent": 1,
                    "name": "A.1",
                    "total": 3
                }
            ]
        },
        {
            "id": 2,
            "name": "B",
            "total": 1,
            "subs": [
                {
                    "id": 3,
                    "parent": 2,
                    "name": "B.1",
                    "total": 2,
                    "subs": [
                        {
                            "id": 6,
                            "parent": 3,
                            "name": "B.1.1",
                            "total": 5
                        }
                    ]
                },
                {
                    "id": 5,
                    "parent": 2,
                    "name": "B.2",
                    "total": 4
                }
            ]
        }
    ]

// Print out Origin tree
console.log("Origin ARRAY ***>\n", JSON.stringify(arrOrigin, null, 2));

// Convert tree to array
let tree2Arr = test.tree2Array(arrOrigin, "subs");
console.log("\n*************>\ntree to ARRAY -->\n", tree2Arr);

Hope to help you in handling flat tree data types like in SQL and NoSQL database structures, like sqlite2, mongodb, oracle, mySql without having to use sql clauses inside database.

Keywords

FAQs

Package last updated on 08 Oct 2020

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