tree-processor 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/dist/index.d.ts +0 -112
- package/dist/index.d.ts.map +1 -1
- package/dist/stats.html +1 -1
- package/dist/tree-processor.cjs.js +54 -1
- package/dist/tree-processor.esm.js +52 -1
- package/dist/tree-processor.umd.js +53 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
# tree-processor
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
一个轻量级的树结构数据处理工具库,使用 TypeScript 编写,支持 tree-shaking,每个格式打包体积约 **3-4 KB**(ESM: 3.25 KB,CJS: 3.42 KB,UMD: 3.56 KB)。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
目前已支持 mapTree、filterTree、findTree、pushTree、unshiftTree、popTree、shiftTree、someTree、everyTree、atTree、indexOfTree、atIndexOfTree、nodeDepthMap 和 dedupTree。每个方法的最后一个参数可以自定义 children 和 id 的属性名。
|
|
6
|
+
|
|
7
|
+
## ✨ 特性
|
|
8
|
+
|
|
9
|
+
- 🚀 **轻量级** - 每个格式约 3-4 KB(ESM: 3.25 KB,CJS: 3.42 KB,UMD: 3.56 KB)
|
|
10
|
+
- 📦 **支持 Tree-shaking** - 按需导入,只打包使用的代码
|
|
11
|
+
- 🔧 **TypeScript 支持** - 完整的类型定义和类型提示
|
|
12
|
+
- 🎯 **类似数组 API** - 提供 map、filter、find 等熟悉的数组方法
|
|
13
|
+
- ⚙️ **自定义字段名** - 支持自定义 children 和 id 字段名
|
|
14
|
+
- ✅ **零依赖** - 无外部依赖,开箱即用
|
|
15
|
+
|
|
16
|
+
## 📦 安装
|
|
6
17
|
|
|
7
18
|
```bash
|
|
8
19
|
yarn add tree-processor
|
package/dist/index.d.ts
CHANGED
|
@@ -1,135 +1,23 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 树结构数据处理的字段名配置
|
|
3
|
-
*/
|
|
4
1
|
export interface FieldNames {
|
|
5
2
|
children: string;
|
|
6
3
|
id: string;
|
|
7
4
|
}
|
|
8
|
-
/**
|
|
9
|
-
* 树节点类型
|
|
10
|
-
*/
|
|
11
5
|
export type TreeNode = Record<string, any>;
|
|
12
|
-
/**
|
|
13
|
-
* 树结构数据(数组形式)
|
|
14
|
-
*/
|
|
15
6
|
export type TreeData = TreeNode[];
|
|
16
|
-
/**
|
|
17
|
-
* 遍历树结构数据,对每个节点执行回调函数
|
|
18
|
-
* @param tree 树结构数据
|
|
19
|
-
* @param callback 回调函数,接收节点作为参数
|
|
20
|
-
* @param fieldNames 自定义字段名配置
|
|
21
|
-
* @returns 返回映射后的扁平数组
|
|
22
|
-
*/
|
|
23
7
|
export declare function mapTree(tree: TreeData, callback: (node: TreeNode) => any, fieldNames?: FieldNames): any[];
|
|
24
|
-
/**
|
|
25
|
-
* 过滤树结构数据,返回满足条件的节点
|
|
26
|
-
* @param tree 树结构数据
|
|
27
|
-
* @param filterFn 过滤函数
|
|
28
|
-
* @param fieldNames 自定义字段名配置
|
|
29
|
-
* @returns 返回满足条件的节点数组
|
|
30
|
-
*/
|
|
31
8
|
export declare function filterTree(tree: TreeData, filterFn: (node: TreeNode, index: number) => boolean, fieldNames?: FieldNames): TreeData;
|
|
32
|
-
/**
|
|
33
|
-
* 查找树结构数据中满足条件的第一个节点
|
|
34
|
-
* @param tree 树结构数据
|
|
35
|
-
* @param conditionFn 条件函数
|
|
36
|
-
* @param fieldNames 自定义字段名配置
|
|
37
|
-
* @returns 返回找到的节点,未找到返回 null
|
|
38
|
-
*/
|
|
39
9
|
export declare function findTree(tree: TreeData, conditionFn: (node: TreeNode) => boolean, fieldNames?: FieldNames): TreeNode | null;
|
|
40
|
-
/**
|
|
41
|
-
* 在指定父节点下添加子节点(添加到末尾)
|
|
42
|
-
* @param tree 树结构数据
|
|
43
|
-
* @param targetParentId 目标父节点ID
|
|
44
|
-
* @param newNode 新节点
|
|
45
|
-
* @param fieldNames 自定义字段名配置
|
|
46
|
-
* @returns 是否成功添加
|
|
47
|
-
*/
|
|
48
10
|
export declare function pushTree(tree: TreeData, targetParentId: any, newNode: TreeNode, fieldNames?: FieldNames): boolean;
|
|
49
|
-
/**
|
|
50
|
-
* 在指定父节点下添加子节点(添加到开头)
|
|
51
|
-
* @param tree 树结构数据
|
|
52
|
-
* @param targetParentId 目标父节点ID
|
|
53
|
-
* @param newNode 新节点
|
|
54
|
-
* @param fieldNames 自定义字段名配置
|
|
55
|
-
* @returns 是否成功添加
|
|
56
|
-
*/
|
|
57
11
|
export declare function unshiftTree(tree: TreeData, targetParentId: any, newNode: TreeNode, fieldNames?: FieldNames): boolean;
|
|
58
|
-
/**
|
|
59
|
-
* 删除指定节点下的最后一个子节点
|
|
60
|
-
* @param tree 树结构数据
|
|
61
|
-
* @param rootId 目标节点ID
|
|
62
|
-
* @param fieldNames 自定义字段名配置
|
|
63
|
-
* @returns 是否成功删除
|
|
64
|
-
*/
|
|
65
12
|
export declare function popTree(tree: TreeData, rootId: any, fieldNames?: FieldNames): boolean;
|
|
66
|
-
/**
|
|
67
|
-
* 删除指定节点下的第一个子节点
|
|
68
|
-
* @param tree 树结构数据
|
|
69
|
-
* @param rootId 目标节点ID
|
|
70
|
-
* @param fieldNames 自定义字段名配置
|
|
71
|
-
* @returns 是否成功删除
|
|
72
|
-
*/
|
|
73
13
|
export declare function shiftTree(tree: TreeData, rootId: any, fieldNames?: FieldNames): boolean;
|
|
74
|
-
/**
|
|
75
|
-
* 检查树结构数据中是否存在满足条件的节点
|
|
76
|
-
* @param tree 树结构数据
|
|
77
|
-
* @param filterFn 过滤函数
|
|
78
|
-
* @param fieldNames 自定义字段名配置
|
|
79
|
-
* @returns 如果存在满足条件的节点返回 true,否则返回 false
|
|
80
|
-
*/
|
|
81
14
|
export declare function someTree(tree: TreeData, filterFn: (node: TreeNode) => boolean, fieldNames?: FieldNames): boolean;
|
|
82
|
-
/**
|
|
83
|
-
* 检查树结构数据中是否所有节点都满足条件
|
|
84
|
-
* @param tree 树结构数据
|
|
85
|
-
* @param filterFn 过滤函数
|
|
86
|
-
* @param fieldNames 自定义字段名配置
|
|
87
|
-
* @returns 如果所有节点都满足条件返回 true,否则返回 false
|
|
88
|
-
*/
|
|
89
15
|
export declare function everyTree(tree: TreeData, filterFn: (node: TreeNode) => boolean, fieldNames?: FieldNames): boolean;
|
|
90
|
-
/**
|
|
91
|
-
* 根据父节点ID和子节点索引获取节点(支持负数索引)
|
|
92
|
-
* @param tree 树结构数据
|
|
93
|
-
* @param parentId 父节点ID
|
|
94
|
-
* @param nodeIndex 子节点索引(支持负数)
|
|
95
|
-
* @param fieldNames 自定义字段名配置
|
|
96
|
-
* @returns 返回找到的节点,未找到返回 null
|
|
97
|
-
*/
|
|
98
16
|
export declare function atTree(tree: TreeData, parentId: any, nodeIndex: number, fieldNames?: FieldNames): TreeNode | null;
|
|
99
|
-
/**
|
|
100
|
-
* 返回从根节点到目标节点的索引路径
|
|
101
|
-
* @param tree 树结构数据
|
|
102
|
-
* @param targetId 目标节点ID
|
|
103
|
-
* @param fieldNames 自定义字段名配置
|
|
104
|
-
* @returns 返回索引路径数组,未找到返回 null
|
|
105
|
-
*/
|
|
106
17
|
export declare function indexOfTree(tree: TreeData, targetId: any, fieldNames?: FieldNames): number[] | null;
|
|
107
|
-
/**
|
|
108
|
-
* 根据索引路径获取节点
|
|
109
|
-
* @param tree 树结构数据
|
|
110
|
-
* @param path 索引路径数组
|
|
111
|
-
* @param fieldNames 自定义字段名配置
|
|
112
|
-
* @returns 返回找到的节点,未找到返回 null
|
|
113
|
-
*/
|
|
114
18
|
export declare function atIndexOfTree(tree: TreeData, path: number[], fieldNames?: FieldNames): TreeNode | null;
|
|
115
|
-
/**
|
|
116
|
-
* 返回节点ID到深度的映射字典
|
|
117
|
-
* @param tree 树结构数据
|
|
118
|
-
* @param fieldNames 自定义字段名配置
|
|
119
|
-
* @returns 返回节点ID到深度的映射对象
|
|
120
|
-
*/
|
|
121
19
|
export declare function nodeDepthMap(tree: TreeData, fieldNames?: FieldNames): Record<string, number>;
|
|
122
|
-
/**
|
|
123
|
-
* 树结构数据去重
|
|
124
|
-
* @param tree 树结构数据
|
|
125
|
-
* @param key 用于去重的键名
|
|
126
|
-
* @param fieldNames 自定义字段名配置
|
|
127
|
-
* @returns 返回去重后的树结构数据
|
|
128
|
-
*/
|
|
129
20
|
export declare function dedupTree(tree: TreeData, key: string, fieldNames?: FieldNames): TreeData;
|
|
130
|
-
/**
|
|
131
|
-
* 默认导出对象,包含所有方法
|
|
132
|
-
*/
|
|
133
21
|
declare const treeProcessor: {
|
|
134
22
|
mapTree: typeof mapTree;
|
|
135
23
|
filterTree: typeof filterTree;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,UAAU;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,EAAE,EAAE,MAAM,CAAC;CACZ;AAaD,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAK3C,MAAM,MAAM,QAAQ,GAAG,QAAQ,EAAE,CAAC;AASlC,wBAAgB,OAAO,CACrB,IAAI,EAAE,QAAQ,EACd,QAAQ,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,GAAG,EACjC,UAAU,GAAE,UAAgC,GAC3C,GAAG,EAAE,CAeP;AASD,wBAAgB,UAAU,CACxB,IAAI,EAAE,QAAQ,EACd,QAAQ,EAAE,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,EACpD,UAAU,GAAE,UAAgC,GAC3C,QAAQ,CAiBV;AASD,wBAAgB,QAAQ,CACtB,IAAI,EAAE,QAAQ,EACd,WAAW,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,OAAO,EACxC,UAAU,GAAE,UAAgC,GAC3C,QAAQ,GAAG,IAAI,CAcjB;AAUD,wBAAgB,QAAQ,CACtB,IAAI,EAAE,QAAQ,EACd,cAAc,EAAE,GAAG,EACnB,OAAO,EAAE,QAAQ,EACjB,UAAU,GAAE,UAAgC,GAC3C,OAAO,CAqBT;AAUD,wBAAgB,WAAW,CACzB,IAAI,EAAE,QAAQ,EACd,cAAc,EAAE,GAAG,EACnB,OAAO,EAAE,QAAQ,EACjB,UAAU,GAAE,UAAgC,GAC3C,OAAO,CAqBT;AASD,wBAAgB,OAAO,CACrB,IAAI,EAAE,QAAQ,EACd,MAAM,EAAE,GAAG,EACX,UAAU,GAAE,UAAgC,GAC3C,OAAO,CAsBT;AASD,wBAAgB,SAAS,CACvB,IAAI,EAAE,QAAQ,EACd,MAAM,EAAE,GAAG,EACX,UAAU,GAAE,UAAgC,GAC3C,OAAO,CAsBT;AASD,wBAAgB,QAAQ,CACtB,IAAI,EAAE,QAAQ,EACd,QAAQ,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,OAAO,EACrC,UAAU,GAAE,UAAgC,GAC3C,OAAO,CAaT;AASD,wBAAgB,SAAS,CACvB,IAAI,EAAE,QAAQ,EACd,QAAQ,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,OAAO,EACrC,UAAU,GAAE,UAAgC,GAC3C,OAAO,CAaT;AAUD,wBAAgB,MAAM,CACpB,IAAI,EAAE,QAAQ,EACd,QAAQ,EAAE,GAAG,EACb,SAAS,EAAE,MAAM,EACjB,UAAU,GAAE,UAAgC,GAC3C,QAAQ,GAAG,IAAI,CAqCjB;AASD,wBAAgB,WAAW,CACzB,IAAI,EAAE,QAAQ,EACd,QAAQ,EAAE,GAAG,EACb,UAAU,GAAE,UAAgC,GAC3C,MAAM,EAAE,GAAG,IAAI,CAsBjB;AASD,wBAAgB,aAAa,CAC3B,IAAI,EAAE,QAAQ,EACd,IAAI,EAAE,MAAM,EAAE,EACd,UAAU,GAAE,UAAgC,GAC3C,QAAQ,GAAG,IAAI,CA4BjB;AAQD,wBAAgB,YAAY,CAC1B,IAAI,EAAE,QAAQ,EACd,UAAU,GAAE,UAAgC,GAC3C,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAexB;AASD,wBAAgB,SAAS,CACvB,IAAI,EAAE,QAAQ,EACd,GAAG,EAAE,MAAM,EACX,UAAU,GAAE,UAAgC,GAC3C,QAAQ,CA+BV;AAKD,QAAA,MAAM,aAAa;;;;;;;;;;;;;;;CAelB,CAAC;AAEF,eAAe,aAAa,CAAC"}
|
package/dist/stats.html
CHANGED
|
@@ -4929,7 +4929,7 @@ var drawChart = (function (exports) {
|
|
|
4929
4929
|
</script>
|
|
4930
4930
|
<script>
|
|
4931
4931
|
/*<!--*/
|
|
4932
|
-
const data = {"version":2,"tree":{"name":"root","children":[{"name":"tree-processor.
|
|
4932
|
+
const data = {"version":2,"tree":{"name":"root","children":[{"name":"tree-processor.cjs.js","children":[{"name":"src/index.ts","uid":"ee24d5a8-1"}]}],"isRoot":true},"nodeParts":{"ee24d5a8-1":{"renderedLength":9165,"gzipLength":1358,"brotliLength":1184,"metaUid":"ee24d5a8-0"}},"nodeMetas":{"ee24d5a8-0":{"id":"\\src\\index.ts","moduleParts":{"tree-processor.cjs.js":"ee24d5a8-1"},"imported":[],"importedBy":[],"isEntry":true}},"env":{"rollup":"4.56.0"},"options":{"gzip":true,"brotli":true,"sourcemap":false}};
|
|
4933
4933
|
|
|
4934
4934
|
const run = () => {
|
|
4935
4935
|
const width = window.innerWidth;
|
|
@@ -1 +1,54 @@
|
|
|
1
|
-
"use strict"
|
|
1
|
+
"use strict"
|
|
2
|
+
Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})
|
|
3
|
+
const r={children:"children",id:"id"}
|
|
4
|
+
function n(n,e,t=r){const i=[]
|
|
5
|
+
return function r(n){for(const o of n){i.push(e(o))
|
|
6
|
+
const n=o[t.children]
|
|
7
|
+
Array.isArray(n)&&n.length>0&&r(n)}}(n),i}function e(n,e,t=r){const i=[]
|
|
8
|
+
return function r(n){n.forEach((n,o)=>{e(n,o)&&i.push(n)
|
|
9
|
+
const c=n[t.children]
|
|
10
|
+
Array.isArray(c)&&c.length>0&&r(c,o)})}(n),i}function t(n,e,i=r){for(const r of n){if(e(r))return r
|
|
11
|
+
const n=r[i.children]
|
|
12
|
+
if(Array.isArray(n)&&n.length>0){const r=t(n,e,i)
|
|
13
|
+
if(r)return r}}return null}function i(n,e,t,i=r){return function r(n){for(const o of n){if(o[i.id]===e)return o[i.children]||(o[i.children]=[]),o[i.children].push(t),!0
|
|
14
|
+
const n=o[i.children]
|
|
15
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(n)}function o(n,e,t,i=r){return function r(n){for(const o of n){if(o[i.id]===e)return o[i.children]||(o[i.children]=[]),o[i.children].unshift(t),!0
|
|
16
|
+
const n=o[i.children]
|
|
17
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(n)}function c(n,e,t=r){return function r(n){for(const i of n){if(i[t.id]===e){const r=i[t.children]
|
|
18
|
+
return!!(Array.isArray(r)&&r.length>0)&&(r.pop(),!0)}const n=i[t.children]
|
|
19
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(n)}function s(n,e,t=r){return function r(n){for(const i of n){if(i[t.id]===e){const r=i[t.children]
|
|
20
|
+
return!!(Array.isArray(r)&&r.length>0)&&(r.shift(),!0)}const n=i[t.children]
|
|
21
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(n)}function u(n,e,t=r){for(const r of n){if(e(r))return!0
|
|
22
|
+
const n=r[t.children]
|
|
23
|
+
if(Array.isArray(n)&&n.length>0&&u(n,e,t))return!0}return!1}function f(n,e,t=r){for(const r of n){if(!e(r))return!1
|
|
24
|
+
const n=r[t.children]
|
|
25
|
+
if(Array.isArray(n)&&n.length>0&&!f(n,e,t))return!1}return!0}function l(n,e,t,i=r){const o=function r(n){for(const t of n){if(t[i.id]===e)return t
|
|
26
|
+
const n=t[i.children]
|
|
27
|
+
if(Array.isArray(n)&&n.length>0){const e=r(n)
|
|
28
|
+
if(e)return e}}return null}(n)
|
|
29
|
+
if(!o)return null
|
|
30
|
+
const c=o[i.children]
|
|
31
|
+
if(!Array.isArray(c)||0===c.length)return null
|
|
32
|
+
const s=t>=0?t:c.length+t
|
|
33
|
+
return s>=0&&s<c.length?c[s]:null}function h(n,e,t=r){return function r(n,i=[]){for(let o=0;o<n.length;o++){const c=n[o],s=[...i,o]
|
|
34
|
+
if(c[t.id]===e)return s
|
|
35
|
+
const u=c[t.children]
|
|
36
|
+
if(Array.isArray(u)&&u.length>0){const n=r(u,s)
|
|
37
|
+
if(n)return n}}return null}(n)}function a(n,e,t=r){if(!Array.isArray(e)||0===e.length)return null
|
|
38
|
+
let i=n
|
|
39
|
+
for(let r=0;r<e.length;r++){const n=e[r]
|
|
40
|
+
if(!Array.isArray(i)||n<0||n>=i.length)return null
|
|
41
|
+
const o=i[n]
|
|
42
|
+
if(r===e.length-1)return o
|
|
43
|
+
const c=o[t.children]
|
|
44
|
+
if(!Array.isArray(c))return null
|
|
45
|
+
i=c}return null}function d(n,e=r){const t={}
|
|
46
|
+
return function r(n,i=1){for(const o of n){t[o[e.id]]=i
|
|
47
|
+
const n=o[e.children]
|
|
48
|
+
Array.isArray(n)&&n.length>0&&r(n,i+1)}}(n),t}function y(n,e,t=r){const i=new Set
|
|
49
|
+
return function r(n){const o=[]
|
|
50
|
+
for(const c of n){const n=c[e]
|
|
51
|
+
if(null!=n){if(i.has(n))continue
|
|
52
|
+
i.add(n)}const s={...c},u=c[t.children]
|
|
53
|
+
Array.isArray(u)&&u.length>0&&(s[t.children]=r(u)),o.push(s)}return o}(n)}const A={mapTree:n,filterTree:e,findTree:t,pushTree:i,unshiftTree:o,popTree:c,shiftTree:s,someTree:u,everyTree:f,atTree:l,indexOfTree:h,atIndexOfTree:a,nodeDepthMap:d,dedupTree:y}
|
|
54
|
+
exports.atIndexOfTree=a,exports.atTree=l,exports.dedupTree=y,exports.default=A,exports.everyTree=f,exports.filterTree=e,exports.findTree=t,exports.indexOfTree=h,exports.mapTree=n,exports.nodeDepthMap=d,exports.popTree=c,exports.pushTree=i,exports.shiftTree=s,exports.someTree=u,exports.unshiftTree=o
|
|
@@ -1 +1,52 @@
|
|
|
1
|
-
const r={children:"children",id:"id"}
|
|
1
|
+
const r={children:"children",id:"id"}
|
|
2
|
+
function n(n,t,e=r){const i=[]
|
|
3
|
+
return function r(n){for(const o of n){i.push(t(o))
|
|
4
|
+
const n=o[e.children]
|
|
5
|
+
Array.isArray(n)&&n.length>0&&r(n)}}(n),i}function t(n,t,e=r){const i=[]
|
|
6
|
+
return function r(n){n.forEach((n,o)=>{t(n,o)&&i.push(n)
|
|
7
|
+
const c=n[e.children]
|
|
8
|
+
Array.isArray(c)&&c.length>0&&r(c,o)})}(n),i}function e(n,t,i=r){for(const r of n){if(t(r))return r
|
|
9
|
+
const n=r[i.children]
|
|
10
|
+
if(Array.isArray(n)&&n.length>0){const r=e(n,t,i)
|
|
11
|
+
if(r)return r}}return null}function i(n,t,e,i=r){return function r(n){for(const o of n){if(o[i.id]===t)return o[i.children]||(o[i.children]=[]),o[i.children].push(e),!0
|
|
12
|
+
const n=o[i.children]
|
|
13
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(n)}function o(n,t,e,i=r){return function r(n){for(const o of n){if(o[i.id]===t)return o[i.children]||(o[i.children]=[]),o[i.children].unshift(e),!0
|
|
14
|
+
const n=o[i.children]
|
|
15
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(n)}function c(n,t,e=r){return function r(n){for(const i of n){if(i[e.id]===t){const r=i[e.children]
|
|
16
|
+
return!!(Array.isArray(r)&&r.length>0)&&(r.pop(),!0)}const n=i[e.children]
|
|
17
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(n)}function u(n,t,e=r){return function r(n){for(const i of n){if(i[e.id]===t){const r=i[e.children]
|
|
18
|
+
return!!(Array.isArray(r)&&r.length>0)&&(r.shift(),!0)}const n=i[e.children]
|
|
19
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(n)}function f(n,t,e=r){for(const r of n){if(t(r))return!0
|
|
20
|
+
const n=r[e.children]
|
|
21
|
+
if(Array.isArray(n)&&n.length>0&&f(n,t,e))return!0}return!1}function l(n,t,e=r){for(const r of n){if(!t(r))return!1
|
|
22
|
+
const n=r[e.children]
|
|
23
|
+
if(Array.isArray(n)&&n.length>0&&!l(n,t,e))return!1}return!0}function s(n,t,e,i=r){const o=function r(n){for(const e of n){if(e[i.id]===t)return e
|
|
24
|
+
const n=e[i.children]
|
|
25
|
+
if(Array.isArray(n)&&n.length>0){const t=r(n)
|
|
26
|
+
if(t)return t}}return null}(n)
|
|
27
|
+
if(!o)return null
|
|
28
|
+
const c=o[i.children]
|
|
29
|
+
if(!Array.isArray(c)||0===c.length)return null
|
|
30
|
+
const u=e>=0?e:c.length+e
|
|
31
|
+
return u>=0&&u<c.length?c[u]:null}function h(n,t,e=r){return function r(n,i=[]){for(let o=0;o<n.length;o++){const c=n[o],u=[...i,o]
|
|
32
|
+
if(c[e.id]===t)return u
|
|
33
|
+
const f=c[e.children]
|
|
34
|
+
if(Array.isArray(f)&&f.length>0){const n=r(f,u)
|
|
35
|
+
if(n)return n}}return null}(n)}function a(n,t,e=r){if(!Array.isArray(t)||0===t.length)return null
|
|
36
|
+
let i=n
|
|
37
|
+
for(let r=0;r<t.length;r++){const n=t[r]
|
|
38
|
+
if(!Array.isArray(i)||n<0||n>=i.length)return null
|
|
39
|
+
const o=i[n]
|
|
40
|
+
if(r===t.length-1)return o
|
|
41
|
+
const c=o[e.children]
|
|
42
|
+
if(!Array.isArray(c))return null
|
|
43
|
+
i=c}return null}function d(n,t=r){const e={}
|
|
44
|
+
return function r(n,i=1){for(const o of n){e[o[t.id]]=i
|
|
45
|
+
const n=o[t.children]
|
|
46
|
+
Array.isArray(n)&&n.length>0&&r(n,i+1)}}(n),e}function y(n,t,e=r){const i=new Set
|
|
47
|
+
return function r(n){const o=[]
|
|
48
|
+
for(const c of n){const n=c[t]
|
|
49
|
+
if(null!=n){if(i.has(n))continue
|
|
50
|
+
i.add(n)}const u={...c},f=c[e.children]
|
|
51
|
+
Array.isArray(f)&&f.length>0&&(u[e.children]=r(f)),o.push(u)}return o}(n)}const A={mapTree:n,filterTree:t,findTree:e,pushTree:i,unshiftTree:o,popTree:c,shiftTree:u,someTree:f,everyTree:l,atTree:s,indexOfTree:h,atIndexOfTree:a,nodeDepthMap:d,dedupTree:y}
|
|
52
|
+
export{a as atIndexOfTree,s as atTree,y as dedupTree,A as default,l as everyTree,t as filterTree,e as findTree,h as indexOfTree,n as mapTree,d as nodeDepthMap,c as popTree,i as pushTree,u as shiftTree,f as someTree,o as unshiftTree}
|
|
@@ -1 +1,53 @@
|
|
|
1
|
-
!function(r,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((r="undefined"!=typeof globalThis?globalThis:r||self).TreeProcessor={})}(this,function(r){"use strict"
|
|
1
|
+
!function(r,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((r="undefined"!=typeof globalThis?globalThis:r||self).TreeProcessor={})}(this,function(r){"use strict"
|
|
2
|
+
const n={children:"children",id:"id"}
|
|
3
|
+
function e(r,e,t=n){const i=[]
|
|
4
|
+
return function r(n){for(const o of n){i.push(e(o))
|
|
5
|
+
const n=o[t.children]
|
|
6
|
+
Array.isArray(n)&&n.length>0&&r(n)}}(r),i}function t(r,e,t=n){const i=[]
|
|
7
|
+
return function r(n){n.forEach((n,o)=>{e(n,o)&&i.push(n)
|
|
8
|
+
const f=n[t.children]
|
|
9
|
+
Array.isArray(f)&&f.length>0&&r(f,o)})}(r),i}function i(r,e,t=n){for(const n of r){if(e(n))return n
|
|
10
|
+
const r=n[t.children]
|
|
11
|
+
if(Array.isArray(r)&&r.length>0){const n=i(r,e,t)
|
|
12
|
+
if(n)return n}}return null}function o(r,e,t,i=n){return function r(n){for(const o of n){if(o[i.id]===e)return o[i.children]||(o[i.children]=[]),o[i.children].push(t),!0
|
|
13
|
+
const n=o[i.children]
|
|
14
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(r)}function f(r,e,t,i=n){return function r(n){for(const o of n){if(o[i.id]===e)return o[i.children]||(o[i.children]=[]),o[i.children].unshift(t),!0
|
|
15
|
+
const n=o[i.children]
|
|
16
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(r)}function u(r,e,t=n){return function r(n){for(const i of n){if(i[t.id]===e){const r=i[t.children]
|
|
17
|
+
return!!(Array.isArray(r)&&r.length>0)&&(r.pop(),!0)}const n=i[t.children]
|
|
18
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(r)}function c(r,e,t=n){return function r(n){for(const i of n){if(i[t.id]===e){const r=i[t.children]
|
|
19
|
+
return!!(Array.isArray(r)&&r.length>0)&&(r.shift(),!0)}const n=i[t.children]
|
|
20
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(r)}function s(r,e,t=n){for(const n of r){if(e(n))return!0
|
|
21
|
+
const r=n[t.children]
|
|
22
|
+
if(Array.isArray(r)&&r.length>0&&s(r,e,t))return!0}return!1}function l(r,e,t=n){for(const n of r){if(!e(n))return!1
|
|
23
|
+
const r=n[t.children]
|
|
24
|
+
if(Array.isArray(r)&&r.length>0&&!l(r,e,t))return!1}return!0}function h(r,e,t,i=n){const o=function r(n){for(const t of n){if(t[i.id]===e)return t
|
|
25
|
+
const n=t[i.children]
|
|
26
|
+
if(Array.isArray(n)&&n.length>0){const e=r(n)
|
|
27
|
+
if(e)return e}}return null}(r)
|
|
28
|
+
if(!o)return null
|
|
29
|
+
const f=o[i.children]
|
|
30
|
+
if(!Array.isArray(f)||0===f.length)return null
|
|
31
|
+
const u=t>=0?t:f.length+t
|
|
32
|
+
return u>=0&&u<f.length?f[u]:null}function d(r,e,t=n){return function r(n,i=[]){for(let o=0;o<n.length;o++){const f=n[o],u=[...i,o]
|
|
33
|
+
if(f[t.id]===e)return u
|
|
34
|
+
const c=f[t.children]
|
|
35
|
+
if(Array.isArray(c)&&c.length>0){const n=r(c,u)
|
|
36
|
+
if(n)return n}}return null}(r)}function a(r,e,t=n){if(!Array.isArray(e)||0===e.length)return null
|
|
37
|
+
let i=r
|
|
38
|
+
for(let r=0;r<e.length;r++){const n=e[r]
|
|
39
|
+
if(!Array.isArray(i)||n<0||n>=i.length)return null
|
|
40
|
+
const o=i[n]
|
|
41
|
+
if(r===e.length-1)return o
|
|
42
|
+
const f=o[t.children]
|
|
43
|
+
if(!Array.isArray(f))return null
|
|
44
|
+
i=f}return null}function y(r,e=n){const t={}
|
|
45
|
+
return function r(n,i=1){for(const o of n){t[o[e.id]]=i
|
|
46
|
+
const n=o[e.children]
|
|
47
|
+
Array.isArray(n)&&n.length>0&&r(n,i+1)}}(r),t}function A(r,e,t=n){const i=new Set
|
|
48
|
+
return function r(n){const o=[]
|
|
49
|
+
for(const f of n){const n=f[e]
|
|
50
|
+
if(null!=n){if(i.has(n))continue
|
|
51
|
+
i.add(n)}const u={...f},c=f[t.children]
|
|
52
|
+
Array.isArray(c)&&c.length>0&&(u[t.children]=r(c)),o.push(u)}return o}(r)}const T={mapTree:e,filterTree:t,findTree:i,pushTree:o,unshiftTree:f,popTree:u,shiftTree:c,someTree:s,everyTree:l,atTree:h,indexOfTree:d,atIndexOfTree:a,nodeDepthMap:y,dedupTree:A}
|
|
53
|
+
r.atIndexOfTree=a,r.atTree=h,r.dedupTree=A,r.default=T,r.everyTree=l,r.filterTree=t,r.findTree=i,r.indexOfTree=d,r.mapTree=e,r.nodeDepthMap=y,r.popTree=u,r.pushTree=o,r.shiftTree=c,r.someTree=s,r.unshiftTree=f,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})})
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tree-processor",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "A lightweight TypeScript library for processing tree-structured data with array-like methods (map, filter, find, push, pop, etc.), supporting tree-shaking and custom field names",
|
|
5
5
|
"main": "dist/tree-processor.cjs.js",
|
|
6
6
|
"module": "dist/tree-processor.esm.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|