-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
185 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# 列表溯源节点 | ||
# 列表溯源主键 | ||
|
||
```js | ||
arrayNode(array, id, [options]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# 列表溯源 | ||
# 列表溯源对象 | ||
|
||
```js | ||
arrayTrace(array, id, [options]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# 树溯源节点 | ||
# 树溯源主键 | ||
|
||
```js | ||
treeNode(tree, id, [options]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# 树溯源对象 | ||
|
||
```js | ||
treeTrace(tree, id, [options]) | ||
``` | ||
|
||
根据主键的值从树中溯源,返回**从根到自身**顺序的对象数组,如:`[object1, object12, object121]`。 | ||
|
||
## 参数 | ||
|
||
| 参数 | 类型 | 默认 | 说明 | | ||
| --------- | -------- | ---- | -------------------------------------------------- | | ||
| `tree` | `array` | | [树状结构数据](./param.md#tree) | | ||
| `id` | `any` | | [主键](./param.md#id)的值,如:`chartProjectMyTwo` | | ||
| `options` | `object` | | [配置选项](./param.md#options) | | ||
|
||
## 返回 | ||
|
||
| 参数 | 类型 | 说明 | | ||
| ---- | ------- | ---------------------------- | | ||
| * | `array` | 从根到自身顺序溯源的对象数组 | | ||
|
||
## 示例 | ||
|
||
::: code-group | ||
```js [调用] | ||
import { treeTrace } from '@axolo/tree-array' | ||
|
||
const tree = [{ | ||
id: 'home', | ||
path: '/home', | ||
parentId: null | ||
}, { | ||
id: 'chart', | ||
path: '/chart', | ||
parentId: null, | ||
children: [{ | ||
id: 'chartIndex', | ||
path: '/chart/index', | ||
parentId: 'chart', | ||
children: [{ | ||
id: 'chartIndexTop', | ||
path: '/chart/index/top', | ||
parentId: 'chartIndex' | ||
}, { | ||
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', | ||
children: [{ | ||
id: 'chartProjectYou', | ||
path: '/chart/project/you', | ||
parentId: 'chartProject', | ||
test: true | ||
}, { | ||
id: 'chartProjectMy', | ||
path: '/chart/project/my', | ||
parentId: 'chartProject', | ||
children: [{ | ||
id: 'chartProjectMyOne', | ||
path: '/chart/project/my/one', | ||
parentId: 'chartProjectMy' | ||
}, { | ||
id: 'chartProjectMyTwo', | ||
path: '/chart/project/my/two', | ||
parentId: 'chartProjectMy' | ||
}] | ||
}] | ||
}] | ||
}, { | ||
id: 'smile', | ||
path: '/smile', | ||
parentId: null, | ||
children: [{ | ||
id: 'smileIndex', | ||
path: '/smile/index', | ||
parentId: 'smile', | ||
test: true | ||
}] | ||
}] | ||
|
||
treeTrace(tree, 'chartProjectMyTwo') // array of id | ||
``` | ||
|
||
```json [结果] | ||
[ | ||
{ | ||
"id": "chart", | ||
"path": "/chart", | ||
"parentId": null, | ||
"children": null | ||
}, | ||
{ | ||
"id": "chartProject", | ||
"path": "/chart/project", | ||
"parentId": "chart", | ||
"children": null | ||
}, | ||
{ | ||
"id": "chartProjectMy", | ||
"path": "/chart/project/my", | ||
"parentId": "chartProject", | ||
"children": null | ||
}, | ||
{ | ||
"id": "chartProjectMyTwo", | ||
"path": "/chart/project/my/two", | ||
"parentId": "chartProjectMy", | ||
"children": null | ||
} | ||
] | ||
``` | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import config from './config' | ||
|
||
const treeTrace = (tree, id, options, currentTrace = []) => { | ||
options = { ...config, ...options } | ||
const { idKey, childrenKey } = options | ||
|
||
for (const node of tree) { | ||
currentTrace.push({ ...node, [childrenKey]: null }) // 将当前节点加入路径 | ||
if (node[idKey] === id) return currentTrace // 找到目标ID,返回路径 | ||
if (node[childrenKey]) { | ||
const result = treeTrace(node[childrenKey], id, options, currentTrace) // 递归遍历子节点 | ||
if (result) return result // 找到目标ID,返回路径 | ||
} | ||
currentTrace.pop() // 回溯,将当前节点从路径中移除 | ||
} | ||
return null // 遍历完所有节点,未找到目标ID,返回null | ||
} | ||
|
||
export default treeTrace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters