Skip to content

Commit f1d5209

Browse files
committed
feat: add treeTrace()
1 parent dd9cdfd commit f1d5209

File tree

15 files changed

+185
-34
lines changed

15 files changed

+185
-34
lines changed

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,20 @@ const tree = array2tree(tree2array([/* tree */]))
4343

4444
## api
4545

46-
| function | return | description |
47-
| ----------------------------------------- | ------ | ------------------------------------------------------------------- |
48-
| `randomId(prefix = '')` | string | generate random id with prefix, like `i_9fang05da21` |
49-
| `array2tree(array, [options])` | array | array to tree with leaf and deep |
50-
| `arrayTrace(array, id, [options])` | array | trace source of id from array |
51-
| `arrayDeep(array, [options], [deep = 1])` | int | get max deep of array |
52-
| `arrayNode(array, id, [options])` | array | get path of id from array by id, like `[id1, id12, id121]` |
53-
| `tree2array(tree, [options])` | array | tree to array and generate id, parentId and leaf when undefined |
54-
| `treeFilter(tree, condition, [options])` | array | get new tree filter by condition function like `node => !node.hide` |
55-
| `treeDeep(tree, [options], [deep = 1])` | int | get max deep of tree |
56-
| `treeNode(tree, id, [options])` | array | get path of id from tree by id, like `[id1, id12, id121]` |
57-
| `treePath(tree, id, [options])` | array | get path of index from tree by id, like `[0, 2, 1]` |
58-
| `treeSub(tree, id, [options])` | object | get sub tree by id, like `{ id, parentId, children: [] }` |
46+
| function | return | description |
47+
| ----------------------------------------- | ------ | --------------------------------------------------------------------------------- |
48+
| `randomId(prefix = '')` | string | generate random id with prefix, like `i_9fang05da21` |
49+
| `array2tree(array, [options])` | array | array to tree with leaf and deep |
50+
| `arrayDeep(array, [options], [deep = 1])` | int | get max deep of array |
51+
| `arrayTrace(array, id, [options])` | array | trace source of node by id from array, like `[object1, object12, object121]` |
52+
| `arrayNode(array, id, [options])` | array | get path of id from array by id, like `[id1, id12, id121]` |
53+
| `tree2array(tree, [options])` | array | tree to array and generate id, parentId and leaf when undefined |
54+
| `treeDeep(tree, [options], [deep = 1])` | int | get max deep of tree |
55+
| `treeFilter(tree, condition, [options])` | array | get new tree filter by condition function like `node => !node.hide` |
56+
| `treePath(tree, id, [options])` | array | get path of index from tree by id, like `[0, 2, 1]` |
57+
| `treeNode(tree, id, [options])` | array | get path of id from tree by id, like `[id1, id12, id121]` |
58+
| `treeTrace(tree, id, [options])` | array | get path of node as object from tree by id, like `[object1, object12, object121]` |
59+
| `treeSub(tree, id, [options])` | object | get sub tree by id, like `{ id, parentId, children: [] }` |
5960

6061
## options
6162

docs/.vitepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export default {
4040
{ text: 'tree2array', link: '/tree2array' },
4141
{ text: 'treeDeep', link: '/treeDeep' },
4242
{ text: 'treeNode', link: '/treeNode' },
43+
{ text: 'treeTrace', link: '/treeTrace' },
4344
{ text: 'treePath', link: '/treePath' },
4445
{ text: 'treeFilter', link: '/treeFilter' },
4546
{ text: 'treeSub', link: '/treeSub' }

docs/arrayNode.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 列表溯源节点
1+
# 列表溯源主键
22

33
```js
44
arrayNode(array, id, [options])

docs/arrayTrace.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 列表溯源
1+
# 列表溯源对象
22

33
```js
44
arrayTrace(array, id, [options])

docs/treeNode.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 树溯源节点
1+
# 树溯源主键
22

33
```js
44
treeNode(tree, id, [options])

docs/treeTrace.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# 树溯源对象
2+
3+
```js
4+
treeTrace(tree, id, [options])
5+
```
6+
7+
根据主键的值从树中溯源,返回**从根到自身**顺序的对象数组,如:`[object1, object12, object121]`
8+
9+
## 参数
10+
11+
| 参数 | 类型 | 默认 | 说明 |
12+
| --------- | -------- | ---- | -------------------------------------------------- |
13+
| `tree` | `array` | | [树状结构数据](./param.md#tree) |
14+
| `id` | `any` | | [主键](./param.md#id)的值,如:`chartProjectMyTwo` |
15+
| `options` | `object` | | [配置选项](./param.md#options) |
16+
17+
## 返回
18+
19+
| 参数 | 类型 | 说明 |
20+
| ---- | ------- | ---------------------------- |
21+
| * | `array` | 从根到自身顺序溯源的对象数组 |
22+
23+
## 示例
24+
25+
::: code-group
26+
```js [调用]
27+
import { treeTrace } from '@axolo/tree-array'
28+
29+
const tree = [{
30+
id: 'home',
31+
path: '/home',
32+
parentId: null
33+
}, {
34+
id: 'chart',
35+
path: '/chart',
36+
parentId: null,
37+
children: [{
38+
id: 'chartIndex',
39+
path: '/chart/index',
40+
parentId: 'chart',
41+
children: [{
42+
id: 'chartIndexTop',
43+
path: '/chart/index/top',
44+
parentId: 'chartIndex'
45+
}, {
46+
id: 'chartIndexActive',
47+
path: '/chart/index/active',
48+
parentId: 'chartIndex',
49+
children: [{
50+
id: 'chartIndexActiveMy',
51+
path: '/chart/index/active/my',
52+
parentId: 'chartIndexActive'
53+
}]
54+
}]
55+
}, {
56+
id: 'chartReview',
57+
path: '/chart/review',
58+
parentId: 'chart'
59+
}, {
60+
id: 'chartProject',
61+
path: '/chart/project',
62+
parentId: 'chart',
63+
children: [{
64+
id: 'chartProjectYou',
65+
path: '/chart/project/you',
66+
parentId: 'chartProject',
67+
test: true
68+
}, {
69+
id: 'chartProjectMy',
70+
path: '/chart/project/my',
71+
parentId: 'chartProject',
72+
children: [{
73+
id: 'chartProjectMyOne',
74+
path: '/chart/project/my/one',
75+
parentId: 'chartProjectMy'
76+
}, {
77+
id: 'chartProjectMyTwo',
78+
path: '/chart/project/my/two',
79+
parentId: 'chartProjectMy'
80+
}]
81+
}]
82+
}]
83+
}, {
84+
id: 'smile',
85+
path: '/smile',
86+
parentId: null,
87+
children: [{
88+
id: 'smileIndex',
89+
path: '/smile/index',
90+
parentId: 'smile',
91+
test: true
92+
}]
93+
}]
94+
95+
treeTrace(tree, 'chartProjectMyTwo') // array of id
96+
```
97+
98+
```json [结果]
99+
[
100+
{
101+
"id": "chart",
102+
"path": "/chart",
103+
"parentId": null,
104+
"children": null
105+
},
106+
{
107+
"id": "chartProject",
108+
"path": "/chart/project",
109+
"parentId": "chart",
110+
"children": null
111+
},
112+
{
113+
"id": "chartProjectMy",
114+
"path": "/chart/project/my",
115+
"parentId": "chartProject",
116+
"children": null
117+
},
118+
{
119+
"id": "chartProjectMyTwo",
120+
"path": "/chart/project/my/two",
121+
"parentId": "chartProjectMy",
122+
"children": null
123+
}
124+
]
125+
```
126+
:::

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@axolo/tree-array",
3-
"version": "0.5.4",
3+
"version": "0.6.0",
44
"description": "An array have children key and parentId key like tree.",
55
"author": "Yueming Fang",
66
"license": "MIT",

src/lib/array-node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const arrayNode = (array, id, options, parents = []) => {
44
options = { ...config, ...options }
55
const { idKey, parentKey } = options
66
const item = array.find(a => a[idKey] === id)
7-
if (!item || !item[idKey]) return parents
7+
if (!item?.[idKey]) return parents
88
parents.unshift(item[idKey]) // only id
99
return arrayNode(array, item[parentKey], options, parents)
1010
}

src/lib/array-trace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const arrayTrace = (array, id, options, parents = []) => {
44
options = { ...config, ...options }
55
const { idKey, parentKey } = options
66
const item = array.find(a => a[idKey] === id)
7-
if (!item || !item[idKey]) return parents
7+
if (!item?.[idKey]) return parents
88
parents.unshift(item)
99
return arrayTrace(array, item[parentKey], options, parents)
1010
}

src/lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import treeFilter from './tree-filter'
99
import treeDeep from './tree-deep'
1010
import treeNode from './tree-node'
1111
import treePath from './tree-path'
12+
import treeTrace from './tree-trace'
1213
import treeSub from './tree-sub'
1314

1415
export {
@@ -23,5 +24,6 @@ export {
2324
treeDeep,
2425
treeNode,
2526
treePath,
27+
treeTrace,
2628
treeSub
2729
}

0 commit comments

Comments
 (0)