Skip to content

Repository files navigation

tree-array

Array to tree, tree to array and so on.

install

npm i @axolo/tree-array

usage

import{tree2array,array2tree}from'@axolo/tree-array'consttree=array2tree([/* array */])constarray=tree2array([/* tree */])

Vue CLI

By default babel-loader ignores all files inside node_modules. You can enable transpileDependencies to avoid unexpected untranspiled code from third-party dependencies.

// vue.config.jsmodule.exports={transpileDependencies: ['@axolo/tree-array']}

CommonJS

ES Modules is not supported in CommonJS. You can use UMD version.

const{ tree2array, array2tree }=require('@axolo/tree-array/dist/index.umd.cjs')consttree=array2tree(tree2array([/* tree */]))

api

functionreturndescription
randomId(prefix = '')stringgenerate random id with prefix, like i_9fang05da21
array2tree(array, [options])arrayarray to tree with leaf and deep
arrayDeep(array, [options], [deep = 1])intget max deep of array
arrayTrace(array, id, [options])arraytrace source of node by id from array, like [object1, object12, object121]
arrayNode(array, id, [options])arrayget path of id from array by id, like [id1, id12, id121]
tree2array(tree, [options])arraytree to array and generate id, parentId and leaf when undefined
treeDeep(tree, [options], [deep = 1])intget max deep of tree
treeFilter(tree, condition, [options])arrayget new tree filter by condition function like node => !node.hide
treePath(tree, id, [options])arrayget path of index from tree by id, like [0, 2, 1]
treeNode(tree, id, [options])arrayget path of id from tree by id, like [id1, id12, id121]
treeTrace(tree, id, [options])arrayget path of node as object from tree by id, like [object1, object12, object121]
treeSub(tree, id, [options])objectget sub tree by id, like { id, parentId, children: [] }

options

paramstypedefaultdescription
idKeystringidkey of id
parentKeystringparentIdkey of parentId
childrenKeystringchildrenkey of children
leafKeystringleafkey of leaf
leafValueanytruevalue of leaf
deepKeystringdeepkey of deep
deepValuenumber0value of deep

data

The data format MUST like example with key defined in options.

array

[{
"id": "chart",
"path": "/chart",
"parentId": null
}, {
"id": "chartIndex",
"path": "/chart/index",
"parentId": "chart"
}, {
"id": "chartIndexActive",
"path": "/chart/index/active",
"parentId": "chartIndex"
}, {
"id": "chartIndexActiveMy",
"path": "/chart/index/active/my",
"parentId": "chartIndexActive"
}, {
"id": "chartReview",
"path": "/chart/review",
"parentId": "chart"
}, {
"id": "chartProject",
"path": "/chart/project",
"parentId": "chart"
}, {
"id": "smile",
"path": "/smile",
"parentId": null
}, {
"id": "smileIndex",
"path": "/smile/index",
"test": true,
"parentId": "smile"
}]

tree

[{
"id": "chart",
"path": "/chart",
"parentId": null,
"children": [{
"id": "chartIndex",
"path": "/chart/index",
"parentId": "chart",
"children": [{
"id": "chartIndexActive",
"path": "/chart/index/active",
"parentId": "chartIndex",
"children": [{
"id": "chartIndexActiveMy",
"path": "/chart/index/active/my",
"parentId": "chartIndexActive"
}]
}]
}, {
"id": "chartReview",
"path": "/chart/review",
"parentId": "chart"
}, {
"id": "chartProject",
"path": "/chart/project",
"parentId": "chart"
}]
}, {
"id": "smile",
"path": "/smile",
"parentId": null,
"children": [{
"id": "smileIndex",
"path": "/smile/index",
"test": true,
"parentId": "smile"
}]
}]

Releases

Packages

Used by

Contributors

Languages