tree-processor 0.9.7 → 0.11.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.en.md +759 -39
- package/README.md +759 -40
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +95 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/stats.html +1 -1
- package/dist/tree-processor.cjs.js +116 -38
- package/dist/tree-processor.esm.js +117 -39
- package/dist/tree-processor.umd.js +174 -96
- package/package.json +4 -2
|
@@ -13,25 +13,27 @@ const n=o[i.children]
|
|
|
13
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
14
|
const n=o[i.children]
|
|
15
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
|
|
17
|
-
if(Array.isArray(n)&&n.length>0
|
|
18
|
-
return
|
|
19
|
-
|
|
16
|
+
return Array.isArray(r)&&r.length>0&&r.pop()||null}const n=i[e.children]
|
|
17
|
+
if(Array.isArray(n)&&n.length>0){const t=r(n)
|
|
18
|
+
if(null!==t)return t}}return null}(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]
|
|
19
|
+
return Array.isArray(r)&&r.length>0&&r.shift()||null}const n=i[e.children]
|
|
20
|
+
if(Array.isArray(n)&&n.length>0){const t=r(n)
|
|
21
|
+
if(null!==t)return t}}return null}(n)}function l(n,t,e=r){for(const r of n){if(t(r))return!0
|
|
20
22
|
const n=r[e.children]
|
|
21
|
-
if(Array.isArray(n)&&n.length>0&&
|
|
23
|
+
if(Array.isArray(n)&&n.length>0&&l(n,t,e))return!0}return!1}function s(n,t,e=r){for(const r of n){if(!t(r))return!1
|
|
22
24
|
const n=r[e.children]
|
|
23
|
-
if(Array.isArray(n)&&n.length>0&&!s(n,t,e))return!1}return!0}function
|
|
25
|
+
if(Array.isArray(n)&&n.length>0&&!s(n,t,e))return!1}return!0}function f(n,t,e,i=r){const o=function r(n){for(const e of n){if(e[i.id]===t)return e
|
|
24
26
|
const n=e[i.children]
|
|
25
27
|
if(Array.isArray(n)&&n.length>0){const t=r(n)
|
|
26
28
|
if(t)return t}}return null}(n)
|
|
27
29
|
if(!o)return null
|
|
28
30
|
const c=o[i.children]
|
|
29
31
|
if(!Array.isArray(c)||0===c.length)return null
|
|
30
|
-
const
|
|
31
|
-
return
|
|
32
|
-
if(c[e.id]===t)return
|
|
33
|
-
const
|
|
34
|
-
if(Array.isArray(
|
|
32
|
+
const u=e>=0?e:c.length+e
|
|
33
|
+
return u>=0&&u<c.length?c[u]:null}function a(n,t,e=r){return function r(n,i=[]){for(let o=0;o<n.length;o++){const c=n[o],u=[...i,o]
|
|
34
|
+
if(c[e.id]===t)return u
|
|
35
|
+
const l=c[e.children]
|
|
36
|
+
if(Array.isArray(l)&&l.length>0){const n=r(l,u)
|
|
35
37
|
if(n)return n}}return null}(n)}function h(n,t,e=r){if(!Array.isArray(t)||0===t.length)return null
|
|
36
38
|
let i=n
|
|
37
39
|
for(let r=0;r<t.length;r++){const n=t[r]
|
|
@@ -46,25 +48,30 @@ const n=o[t.children]
|
|
|
46
48
|
Array.isArray(n)&&n.length>0&&r(n,i+1)}}(n),e}function d(n,t,e=r){return function r(n,i=1){for(const o of n){if(o[e.id]===t)return i
|
|
47
49
|
const n=o[e.children]
|
|
48
50
|
if(Array.isArray(n)&&n.length>0){const t=r(n,i+1)
|
|
49
|
-
if(null!==t)return t}}return null}(n)}function A(n,t,e=r){const i=new Set
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
if(null!==t)return t}}return null}(n)}function A(n,t,e=r){const i=new Set,o=r=>{if("function"==typeof t)return t(r)
|
|
52
|
+
if(Array.isArray(t)){const n=[]
|
|
53
|
+
let e=!1
|
|
54
|
+
for(const i of t){const t=r[i]
|
|
55
|
+
null!=t?(e=!0,n.push(String(t))):n.push("")}if(!e)return
|
|
56
|
+
return n.join("")}return r[t]}
|
|
57
|
+
return function r(n){const t=[]
|
|
58
|
+
for(const c of n){const n=o(c)
|
|
52
59
|
if(null!=n){if(i.has(n))continue
|
|
53
|
-
i.add(n)}const
|
|
54
|
-
Array.isArray(
|
|
60
|
+
i.add(n)}const u={...c},l=c[e.children]
|
|
61
|
+
Array.isArray(l)&&l.length>0&&(u[e.children]=r(l)),t.push(u)}return t}(n)}function g(n,t,e=r){return function r(n){for(let i=0;i<n.length;i++){const o=n[i]
|
|
55
62
|
if(o[e.id]===t)return n.splice(i,1),!0
|
|
56
63
|
const c=o[e.children]
|
|
57
64
|
if(Array.isArray(c)&&c.length>0&&r(c))return!0}return!1}(n)}function p(n,t,e=r){!function r(n){for(const i of n){t(i)
|
|
58
65
|
const n=i[e.children]
|
|
59
|
-
Array.isArray(n)&&n.length>0&&r(n)}}(n)}function T(r){return!Array.isArray(r)||0===r.length}function
|
|
66
|
+
Array.isArray(n)&&n.length>0&&r(n)}}(n)}function T(r){return!Array.isArray(r)||0===r.length}function m(n,t=r){if(!N(n,t))return!0
|
|
60
67
|
const e=n[t.children]
|
|
61
|
-
return void 0===e||!Array.isArray(e)||0===e.length}function
|
|
68
|
+
return void 0===e||!Array.isArray(e)||0===e.length}function v(n,t,e=r){const i=function r(n){for(const i of n){if(i[e.id]===t)return i
|
|
62
69
|
const n=i[e.children]
|
|
63
70
|
if(Array.isArray(n)&&n.length>0){const t=r(n)
|
|
64
71
|
if(null!==t)return t}}return null}(n)
|
|
65
72
|
if(!i)return[]
|
|
66
73
|
const o=i[e.children]
|
|
67
|
-
return Array.isArray(o)?o:[]}function
|
|
74
|
+
return Array.isArray(o)?o:[]}function b(n,t,e=r){const i=function r(n,i=null){for(const o of n){if(o[e.id]===t)return{node:o,parent:i}
|
|
68
75
|
const n=o[e.children]
|
|
69
76
|
if(Array.isArray(n)&&n.length>0){const t=r(n,o)
|
|
70
77
|
if(null!==t)return t}}return null}(n)
|
|
@@ -74,45 +81,49 @@ const o=i.parent[e.children]
|
|
|
74
81
|
return Array.isArray(o)?o:[]}function D(n,t,e=r){return function r(n,i=null){for(const o of n){if(o[e.id]===t)return i
|
|
75
82
|
const n=o[e.children]
|
|
76
83
|
if(Array.isArray(n)&&n.length>0){const t=r(n,o)
|
|
77
|
-
if(null!==t)return t}}return null}(n)}function
|
|
84
|
+
if(null!==t)return t}}return null}(n)}function x(n,t,e=r){for(const r of n){if(r[e.id]===t)return!0
|
|
78
85
|
const n=r[e.children]
|
|
79
|
-
if(Array.isArray(n)&&n.length>0&&
|
|
86
|
+
if(Array.isArray(n)&&n.length>0&&x(n,t,e))return!0}return!1}function N(n,t=r){if(!n||"object"!=typeof n||Array.isArray(n))return!1
|
|
80
87
|
const e=n[t.children]
|
|
81
88
|
if(void 0!==e){if(null===e)return!1
|
|
82
89
|
if(!Array.isArray(e))return!1
|
|
83
|
-
for(const r of e)if(!
|
|
84
|
-
for(const r of n)if(!
|
|
85
|
-
return!0}function
|
|
90
|
+
for(const r of e)if(!N(r,t))return!1}return!0}function B(n,t=r){if(!Array.isArray(n))return!1
|
|
91
|
+
for(const r of n)if(!N(r,t))return!1
|
|
92
|
+
return!0}function L(n,t=r){if(!n||"object"!=typeof n||Array.isArray(n))return!1
|
|
86
93
|
const e=n[t.children]
|
|
87
|
-
return void 0===e||Array.isArray(e)}function
|
|
94
|
+
return void 0===e||Array.isArray(e)}function j(n,t=r,e=new WeakSet){if(!n||"object"!=typeof n||Array.isArray(n))return!1
|
|
88
95
|
if(e.has(n))return!1
|
|
89
96
|
e.add(n)
|
|
90
97
|
const i=n[t.children]
|
|
91
98
|
if(void 0!==i){if(null===i)return!1
|
|
92
99
|
if(!Array.isArray(i))return!1
|
|
93
|
-
for(const r of i)if(!
|
|
100
|
+
for(const r of i)if(!j(r,t,e))return!1}return!0}function S(n,t,e=r){return!(t<=0)&&function r(n,i){if(i>t)return!1
|
|
94
101
|
for(const t of n){const n=t[e.children]
|
|
95
102
|
if(Array.isArray(n)&&n.length>0&&!r(n,i+1))return!1}return!0}(n,1)}function w(n,t=r){const e=n[t.children]
|
|
96
|
-
return void 0===e||!Array.isArray(e)||0===e.length}function
|
|
103
|
+
return void 0===e||!Array.isArray(e)||0===e.length}function F(n,t,e=r){const i=function r(n,i=null){for(const o of n){if(o[e.id]===t)return{found:!0,isRoot:null===i}
|
|
104
|
+
const n=o[e.children]
|
|
105
|
+
if(Array.isArray(n)&&n.length>0){const t=r(n,o)
|
|
106
|
+
if(t.found)return t}}return{found:!1,isRoot:!1}}(n)
|
|
107
|
+
return i.found&&i.isRoot}function O(n,t=r){const e=[]
|
|
97
108
|
return function r(n){for(const i of n){const{[t.children]:n,...o}=i
|
|
98
109
|
e.push(o)
|
|
99
110
|
const c=i[t.children]
|
|
100
|
-
Array.isArray(c)&&c.length>0&&r(c)}}(n),e}function
|
|
111
|
+
Array.isArray(c)&&c.length>0&&r(c)}}(n),e}function P(n,t={}){const{rootParentId:e=null,parentIdField:i="parentId",fieldNames:o=r}=t
|
|
101
112
|
if(Array.isArray(n)){if(0===n.length)return[]
|
|
102
113
|
const r=new Map
|
|
103
114
|
for(const t of n){const n=t[o.id]
|
|
104
115
|
if(null==n)continue
|
|
105
116
|
const e={...t,[o.children]:[]}
|
|
106
117
|
r.set(n,e)}const t=[]
|
|
107
|
-
for(const c of n){const n=c[o.id],
|
|
118
|
+
for(const c of n){const n=c[o.id],u=c[i]
|
|
108
119
|
if(null==n)continue
|
|
109
|
-
const
|
|
110
|
-
if(
|
|
111
|
-
else{const n=r.get(
|
|
120
|
+
const l=r.get(n)
|
|
121
|
+
if(l)if(u===e||null==u)t.push(l)
|
|
122
|
+
else{const n=r.get(u)
|
|
112
123
|
if(n){const r=n[o.children]
|
|
113
|
-
Array.isArray(r)?r.push(
|
|
124
|
+
Array.isArray(r)?r.push(l):n[o.children]=[l]}else t.push(l)}}return t}if(n instanceof Map){if(0===n.size)return[]
|
|
114
125
|
const r=[]
|
|
115
|
-
return n.forEach((n,t)=>{r.push({...n,[o.id]:t})}),
|
|
126
|
+
return n.forEach((n,t)=>{r.push({...n,[o.id]:t})}),P(r,t)}if(n&&"object"==typeof n&&!Array.isArray(n)&&!(n instanceof Map)){const r=n[o.children]
|
|
116
127
|
if(Array.isArray(r))return[n]
|
|
117
128
|
const e=Object.keys(n)
|
|
118
129
|
if(e.length>0){let r=!0
|
|
@@ -120,14 +131,81 @@ for(const t of e){const e=n[t]
|
|
|
120
131
|
if(!e||"object"!=typeof e||Array.isArray(e)){r=!1
|
|
121
132
|
break}}if(r){const r=[]
|
|
122
133
|
for(const t of e){const e=n[t]
|
|
123
|
-
r.push({...e,[o.id]:t})}if(r.length>0)return
|
|
134
|
+
r.push({...e,[o.id]:t})}if(r.length>0)return P(r,t)}}return[{...n,[o.children]:[]}]}return[]}function R(n,t=r){const e=new Map
|
|
124
135
|
return function r(n){for(const i of n){const n=i[t.id]
|
|
125
136
|
if(null!=n){const{[t.children]:r,...o}=i
|
|
126
137
|
e.set(n,o)}const o=i[t.children]
|
|
127
|
-
Array.isArray(o)&&o.length>0&&r(o)}}(n),e}function
|
|
138
|
+
Array.isArray(o)&&o.length>0&&r(o)}}(n),e}function E(n,t=r){const e=[]
|
|
128
139
|
return Array.isArray(n)&&0!==n.length?(function r(n,i=0){e[i]||(e[i]=[])
|
|
129
140
|
for(const o of n){const{[t.children]:n,...c}=o
|
|
130
141
|
e[i].push(c)
|
|
131
|
-
const
|
|
132
|
-
Array.isArray(
|
|
133
|
-
|
|
142
|
+
const u=o[t.children]
|
|
143
|
+
Array.isArray(u)&&u.length>0&&r(u,i+1)}}(n),e):[]}function M(r){return Array.isArray(r)&&0!==r.length&&1===r.length?r[0]:null}function W(n,t=r){if(!Array.isArray(n)||0===n.length)return[]
|
|
144
|
+
function e(r){const n={...r},i=r[t.children]
|
|
145
|
+
return Array.isArray(i)&&i.length>0&&(n[t.children]=i.map(r=>e(r))),n}return n.map(r=>e(r))}function k(r){return Array.isArray(r)&&0!==r.length?r.map(r=>({...r})):[]}function C(n,t,e=r){if(!Array.isArray(n)||0===n.length)return[]
|
|
146
|
+
if(!t||"object"!=typeof t||Array.isArray(t)||t.constructor!==Object)throw new Error('cloneSubtree: 必须传入对象,例如 { id: 1 } 或 { name: "xxx" }')
|
|
147
|
+
const i=Object.keys(t)
|
|
148
|
+
if(0===i.length)return[]
|
|
149
|
+
if(i.length>1)throw new Error('cloneSubtree: 查找对象只能包含一个字段,例如 { id: 1 } 或 { name: "xxx" }')
|
|
150
|
+
const o=i[0],c=t[o],u=function r(n){for(const t of n){if(t[o]===c)return t
|
|
151
|
+
const n=t[e.children]
|
|
152
|
+
if(Array.isArray(n)&&n.length>0){const t=r(n)
|
|
153
|
+
if(t)return t}}return null}(n)
|
|
154
|
+
return u?[function r(n){const t={...n},i=n[e.children]
|
|
155
|
+
return Array.isArray(i)&&i.length>0&&(t[e.children]=i.map(n=>r(n))),t}(u)]:[]}function I(n,t,e=r){if(!Array.isArray(n)||0===n.length)return[]
|
|
156
|
+
function i(r){const n={...t(r)},o=r[e.children]
|
|
157
|
+
return Array.isArray(o)&&o.length>0&&(n[e.children]=o.map(r=>i(r))),n}return n.map(r=>i(r))}function V(...n){const t=[]
|
|
158
|
+
for(const e of n)Array.isArray(e)&&e.length>0&&t.push(...W(e,r))
|
|
159
|
+
return t}function z(n,t,e=r){if(!Array.isArray(n)||0===n.length)return[]
|
|
160
|
+
const i=W(n,e)
|
|
161
|
+
return function r(n){n.sort(t)
|
|
162
|
+
for(const t of n){const n=t[e.children]
|
|
163
|
+
Array.isArray(n)&&n.length>0&&r(n)}}(i),i}function q(n,t,e,i=r){let o=e
|
|
164
|
+
return function r(n){for(const e of n){o=t(o,e)
|
|
165
|
+
const n=e[i.children]
|
|
166
|
+
Array.isArray(n)&&n.length>0&&r(n)}}(n),o}function G(n,t,e,i=r){return Array.isArray(n)&&0!==n.length?W(n.slice(t,e),i):[]}function H(n,t,e=r){const i={}
|
|
167
|
+
!function r(n){for(const o of n){const n=String(t.groupBy(o))
|
|
168
|
+
if(!i[n]){i[n]={}
|
|
169
|
+
for(const[r,e]of Object.entries(t.aggregations))"count"===e.operation?i[n][r]=0:"avg"===e.operation?i[n][r]={sum:0,count:0}:"max"===e.operation||"min"===e.operation?i[n][r]=e.field?o[e.field]??null:null:i[n][r]=0}for(const[r,e]of Object.entries(t.aggregations)){const{operation:t,field:c}=e
|
|
170
|
+
if("count"===t)i[n][r]++
|
|
171
|
+
else if("avg"===t){if(c){const t=o[c]??0
|
|
172
|
+
i[n][r].sum+=t,i[n][r].count++}}else if("max"===t){if(c){const t=o[c]
|
|
173
|
+
null!=t&&(null===i[n][r]||t>i[n][r])&&(i[n][r]=t)}}else if("min"===t){if(c){const t=o[c]
|
|
174
|
+
null!=t&&(null===i[n][r]||t<i[n][r])&&(i[n][r]=t)}}else"sum"===t&&c&&(i[n][r]+=o[c]??0)}const c=o[e.children]
|
|
175
|
+
Array.isArray(c)&&c.length>0&&r(c)}}(n)
|
|
176
|
+
for(const r in i)for(const[n,e]of Object.entries(t.aggregations))"avg"===e.operation&&i[r][n].count>0?i[r][n]=i[r][n].sum/i[r][n].count:"avg"===e.operation&&0===i[r][n].count&&(i[r][n]=0)
|
|
177
|
+
return i}function J(n,t,e=r){const i={}
|
|
178
|
+
return function r(n){for(const o of n){const n=String(o[t]??"")
|
|
179
|
+
i[n]||(i[n]=[]),i[n].push(o)
|
|
180
|
+
const c=o[e.children]
|
|
181
|
+
Array.isArray(c)&&c.length>0&&r(c)}}(n),i}function K(n,t,e=r){const i={}
|
|
182
|
+
return function r(n){for(const o of n){const n=String(t(o)??"")
|
|
183
|
+
i[n]||(i[n]=[]),i[n].push(o)
|
|
184
|
+
const c=o[e.children]
|
|
185
|
+
Array.isArray(c)&&c.length>0&&r(c)}}(n),i}function Q(n,t,e=r){return q(n,(r,n)=>r+(n[t]??0),0,e)}function U(n,t,e=r){let i=0,o=0
|
|
186
|
+
return q(n,(r,n)=>{const e=n[t]
|
|
187
|
+
return null!=e&&(i+=e,o++),null},null,e),o>0?i/o:0}function X(n,t,e=r){let i=null,o=!1
|
|
188
|
+
return q(n,(r,n)=>{const e=n[t]
|
|
189
|
+
return null!=e&&"number"==typeof e&&(!o||e>i)&&(i=e,o=!0),null},null,e),o?i:null}function Y(n,t,e=r){let i=null,o=!1
|
|
190
|
+
return q(n,(r,n)=>{const e=n[t]
|
|
191
|
+
return null!=e&&"number"==typeof e&&(!o||e<i)&&(i=e,o=!0),null},null,e),o?i:null}function Z(n,t,e=r){return q(n,t?(r,n)=>r+(t(n)?1:0):r=>r+1,0,e)}function $(n,t=r){if(!Array.isArray(n)||0===n.length)return{totalNodes:0,leafNodes:0,maxDepth:0,minDepth:0,avgDepth:0,levels:0}
|
|
192
|
+
let e=0,i=0,o=0,c=1/0,u=0
|
|
193
|
+
return function r(n,l){for(const s of n){e++,u+=l,l>o&&(o=l),l<c&&(c=l)
|
|
194
|
+
const n=s[t.children]
|
|
195
|
+
Array.isArray(n)&&n.length>0?r(n,l+1):i++}}(n,1),{totalNodes:e,leafNodes:i,maxDepth:o,minDepth:c===1/0?0:c,avgDepth:e>0?u/e:0,levels:o}}function _(n,t={},e=r){const{includeBasic:i=!0,includeLevelAnalysis:o=!0,includeBranchingFactor:c=!0,includeDepthDistribution:u=!0,includeBalanceAnalysis:l=!0,includePathAnalysis:s=!0,includeLeafAnalysis:f=!0}=t
|
|
196
|
+
if(!Array.isArray(n)||0===n.length)return{totalNodes:0,leafNodes:0,internalNodes:0,maxDepth:0,minDepth:0,avgDepth:0,levels:0,byLevel:{},maxWidth:0,avgWidth:0,widthByLevel:{},avgBranchingFactor:0,maxBranchingFactor:0,minBranchingFactor:0,branchingFactorDistribution:{},depthDistribution:{},depthVariance:0,isBalanced:!1,balanceRatio:0,avgPathLength:0,maxPathLength:0,minPathLength:0,leafNodeRatio:0,leafNodesByLevel:{}}
|
|
197
|
+
const a={},h={},y={},d={},A={}
|
|
198
|
+
let g=0,p=0,T=0,m=0,v=1/0,b=0,D=0,x=0,N=0,B=0,L=0,j=0,S=1/0,w=0,F=0,O=1/0
|
|
199
|
+
const P=[]
|
|
200
|
+
!function r(n,t,R){const E=n.length
|
|
201
|
+
o&&(a[t]=(a[t]||0)+E,h[t]=(h[t]||0)+E,E>D&&(D=E),x+=E,N++)
|
|
202
|
+
for(const o of n){g++
|
|
203
|
+
const n=t+1
|
|
204
|
+
if(i&&(b+=n,n>m&&(m=n),n<v&&(v=n)),l&&P.push(n),u&&(y[n]=(y[n]||0)+1),s){const r=R+1
|
|
205
|
+
w+=r,r>F&&(F=r),r<O&&(O=r)}const a=o[e.children],h=Array.isArray(a)?a.length:0
|
|
206
|
+
h>0?(i&&T++,c&&(B+=h,L++,A[h]=(A[h]||0)+1,h>j&&(j=h),h<S&&(S=h)),r(a,n,s?R+1:R)):(i&&p++,f&&(d[n]=(d[n]||0)+1))}}(n,0,0)
|
|
207
|
+
const R=i&&g>0?b/g:0,E=o&&N>0?x/N:0,M=c&&L>0?B/L:0,W=s&&g>0?w/g:0,k=f&&g>0?p/g:0
|
|
208
|
+
let C=0,I=!1,V=0
|
|
209
|
+
if(l){if(P.length>0){const r=R
|
|
210
|
+
C=P.reduce((n,t)=>n+Math.pow(t-r,2),0)/P.length}I=C<2&&m-v<=2,V=m>0?v/m:0}return{totalNodes:i?g:0,leafNodes:i?p:0,internalNodes:i?T:0,maxDepth:i?m:0,minDepth:i?v===1/0?0:v:0,avgDepth:R,levels:i?m:0,byLevel:o?a:{},maxWidth:o?D:0,avgWidth:E,widthByLevel:o?h:{},avgBranchingFactor:M,maxBranchingFactor:c?0===j?0:j:0,minBranchingFactor:c?S===1/0?0:S:0,branchingFactorDistribution:c?A:{},depthDistribution:u?y:{},depthVariance:C,isBalanced:I,balanceRatio:V,avgPathLength:W,maxPathLength:s?F:0,minPathLength:s?O===1/0?0:O:0,leafNodeRatio:k,leafNodesByLevel:f?d:{}}}const rr={mapTree:n,filterTree:t,findTree:e,pushTree:i,unshiftTree:o,popTree:c,shiftTree:u,someTree:l,everyTree:s,atTree:f,indexOfTree:a,atIndexOfTree:h,getNodeDepthMap:y,getNodeDepth:d,dedupTree:A,removeTree:g,forEachTree:p,isEmptyTreeData:T,isEmptySingleTreeData:m,getParentTree:D,getChildrenTree:v,getSiblingsTree:b,includesTree:x,isSingleTreeData:N,isTreeData:B,isValidTreeNode:L,isTreeNodeWithCircularCheck:j,isSafeTreeDepth:S,isLeafNode:w,isRootNode:F,convertToArrayTree:O,convertBackTree:P,convertToMapTree:R,convertToLevelArrayTree:E,convertToObjectTree:M,cloneTree:W,shallowCloneTree:k,cloneSubtree:C,cloneWithTransform:I,concatTree:V,sortTree:z,reduceTree:q,sliceTree:G,aggregateTree:H,groupTree:J,groupByTree:K,sumTree:Q,avgTree:U,maxTree:X,minTree:Y,countTree:Z,getTreeStats:$,analyzeTree:_}
|
|
211
|
+
export{H as aggregateTree,_ as analyzeTree,h as atIndexOfTree,f as atTree,U as avgTree,C as cloneSubtree,W as cloneTree,I as cloneWithTransform,V as concatTree,P as convertBackTree,O as convertToArrayTree,E as convertToLevelArrayTree,R as convertToMapTree,M as convertToObjectTree,Z as countTree,A as dedupTree,rr as default,s as everyTree,t as filterTree,e as findTree,p as forEachTree,v as getChildrenTree,d as getNodeDepth,y as getNodeDepthMap,D as getParentTree,b as getSiblingsTree,$ as getTreeStats,K as groupByTree,J as groupTree,x as includesTree,a as indexOfTree,m as isEmptySingleTreeData,T as isEmptyTreeData,w as isLeafNode,F as isRootNode,S as isSafeTreeDepth,N as isSingleTreeData,B as isTreeData,j as isTreeNodeWithCircularCheck,L as isValidTreeNode,n as mapTree,X as maxTree,Y as minTree,c as popTree,i as pushTree,q as reduceTree,g as removeTree,k as shallowCloneTree,u as shiftTree,G as sliceTree,l as someTree,z as sortTree,Q as sumTree,o as unshiftTree}
|
|
@@ -1,119 +1,130 @@
|
|
|
1
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
2
|
const n={children:"children",id:"id"}
|
|
3
|
-
function e(r,e,t=n){const
|
|
4
|
-
return function r(n){for(const
|
|
5
|
-
const n=
|
|
6
|
-
Array.isArray(n)&&n.length>0&&r(n)}}(r),
|
|
7
|
-
return function r(n){n.forEach((n,
|
|
3
|
+
function e(r,e,t=n){const o=[]
|
|
4
|
+
return function r(n){for(const i of n){o.push(e(i))
|
|
5
|
+
const n=i[t.children]
|
|
6
|
+
Array.isArray(n)&&n.length>0&&r(n)}}(r),o}function t(r,e,t=n){const o=[]
|
|
7
|
+
return function r(n){n.forEach((n,i)=>{e(n,i)&&o.push(n)
|
|
8
8
|
const c=n[t.children]
|
|
9
|
-
Array.isArray(c)&&c.length>0&&r(c)})}(r),
|
|
9
|
+
Array.isArray(c)&&c.length>0&&r(c)})}(r),o}function o(r,e,t=n){for(const n of r){if(e(n))return n
|
|
10
10
|
const r=n[t.children]
|
|
11
|
-
if(Array.isArray(r)&&r.length>0){const n=
|
|
12
|
-
if(n)return n}}return null}function
|
|
13
|
-
const n=o
|
|
14
|
-
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(r)}function c(r,e,t,
|
|
15
|
-
const n=o
|
|
16
|
-
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(r)}function
|
|
17
|
-
return
|
|
18
|
-
if(Array.isArray(n)&&n.length>0
|
|
19
|
-
return
|
|
20
|
-
|
|
11
|
+
if(Array.isArray(r)&&r.length>0){const n=o(r,e,t)
|
|
12
|
+
if(n)return n}}return null}function i(r,e,t,o=n){return function r(n){for(const i of n){if(i[o.id]===e)return i[o.children]||(i[o.children]=[]),i[o.children].push(t),!0
|
|
13
|
+
const n=i[o.children]
|
|
14
|
+
if(Array.isArray(n)&&n.length>0&&r(n))return!0}return!1}(r)}function c(r,e,t,o=n){return function r(n){for(const i of n){if(i[o.id]===e)return i[o.children]||(i[o.children]=[]),i[o.children].unshift(t),!0
|
|
15
|
+
const n=i[o.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 o of n){if(o[t.id]===e){const r=o[t.children]
|
|
17
|
+
return Array.isArray(r)&&r.length>0&&r.pop()||null}const n=o[t.children]
|
|
18
|
+
if(Array.isArray(n)&&n.length>0){const e=r(n)
|
|
19
|
+
if(null!==e)return e}}return null}(r)}function s(r,e,t=n){return function r(n){for(const o of n){if(o[t.id]===e){const r=o[t.children]
|
|
20
|
+
return Array.isArray(r)&&r.length>0&&r.shift()||null}const n=o[t.children]
|
|
21
|
+
if(Array.isArray(n)&&n.length>0){const e=r(n)
|
|
22
|
+
if(null!==e)return e}}return null}(r)}function l(r,e,t=n){for(const n of r){if(e(n))return!0
|
|
21
23
|
const r=n[t.children]
|
|
22
|
-
if(Array.isArray(r)&&r.length>0&&
|
|
24
|
+
if(Array.isArray(r)&&r.length>0&&l(r,e,t))return!0}return!1}function f(r,e,t=n){for(const n of r){if(!e(n))return!1
|
|
23
25
|
const r=n[t.children]
|
|
24
|
-
if(Array.isArray(r)&&r.length>0&&!
|
|
25
|
-
const n=t[
|
|
26
|
+
if(Array.isArray(r)&&r.length>0&&!f(r,e,t))return!1}return!0}function a(r,e,t,o=n){const i=function r(n){for(const t of n){if(t[o.id]===e)return t
|
|
27
|
+
const n=t[o.children]
|
|
26
28
|
if(Array.isArray(n)&&n.length>0){const e=r(n)
|
|
27
29
|
if(e)return e}}return null}(r)
|
|
28
|
-
if(!
|
|
29
|
-
const c=o
|
|
30
|
+
if(!i)return null
|
|
31
|
+
const c=i[o.children]
|
|
30
32
|
if(!Array.isArray(c)||0===c.length)return null
|
|
31
|
-
const
|
|
32
|
-
return
|
|
33
|
-
if(c[t.id]===e)return
|
|
34
|
-
const
|
|
35
|
-
if(Array.isArray(
|
|
33
|
+
const u=t>=0?t:c.length+t
|
|
34
|
+
return u>=0&&u<c.length?c[u]:null}function h(r,e,t=n){return function r(n,o=[]){for(let i=0;i<n.length;i++){const c=n[i],u=[...o,i]
|
|
35
|
+
if(c[t.id]===e)return u
|
|
36
|
+
const s=c[t.children]
|
|
37
|
+
if(Array.isArray(s)&&s.length>0){const n=r(s,u)
|
|
36
38
|
if(n)return n}}return null}(r)}function d(r,e,t=n){if(!Array.isArray(e)||0===e.length)return null
|
|
37
|
-
let
|
|
39
|
+
let o=r
|
|
38
40
|
for(let r=0;r<e.length;r++){const n=e[r]
|
|
39
|
-
if(!Array.isArray(
|
|
40
|
-
const o
|
|
41
|
-
if(r===e.length-1)return
|
|
42
|
-
const c=
|
|
41
|
+
if(!Array.isArray(o)||n<0||n>=o.length)return null
|
|
42
|
+
const i=o[n]
|
|
43
|
+
if(r===e.length-1)return i
|
|
44
|
+
const c=i[t.children]
|
|
43
45
|
if(!Array.isArray(c))return null
|
|
44
|
-
|
|
45
|
-
return function r(n,
|
|
46
|
-
const n=
|
|
47
|
-
Array.isArray(n)&&n.length>0&&r(n,
|
|
48
|
-
const n=o[t.children]
|
|
49
|
-
if(Array.isArray(n)&&n.length>0){const e=r(n,i+1)
|
|
50
|
-
if(null!==e)return e}}return null}(r)}function T(r,e,t=n){const i=new Set
|
|
51
|
-
return function r(n){const o=[]
|
|
52
|
-
for(const c of n){const n=c[e]
|
|
53
|
-
if(null!=n){if(i.has(n))continue
|
|
54
|
-
i.add(n)}const f={...c},u=c[t.children]
|
|
55
|
-
Array.isArray(u)&&u.length>0&&(f[t.children]=r(u)),o.push(f)}return o}(r)}function g(r,e,t=n){return function r(n){for(let i=0;i<n.length;i++){const o=n[i]
|
|
56
|
-
if(o[t.id]===e)return n.splice(i,1),!0
|
|
57
|
-
const c=o[t.children]
|
|
58
|
-
if(Array.isArray(c)&&c.length>0&&r(c))return!0}return!1}(r)}function p(r,e,t=n){!function r(n){for(const i of n){e(i)
|
|
46
|
+
o=c}return null}function y(r,e=n){const t={}
|
|
47
|
+
return function r(n,o=1){for(const i of n){t[i[e.id]]=o
|
|
48
|
+
const n=i[e.children]
|
|
49
|
+
Array.isArray(n)&&n.length>0&&r(n,o+1)}}(r),t}function A(r,e,t=n){return function r(n,o=1){for(const i of n){if(i[t.id]===e)return o
|
|
59
50
|
const n=i[t.children]
|
|
60
|
-
Array.isArray(n)&&n.length>0
|
|
51
|
+
if(Array.isArray(n)&&n.length>0){const e=r(n,o+1)
|
|
52
|
+
if(null!==e)return e}}return null}(r)}function g(r,e,t=n){const o=new Set,i=r=>{if("function"==typeof e)return e(r)
|
|
53
|
+
if(Array.isArray(e)){const n=[]
|
|
54
|
+
let t=!1
|
|
55
|
+
for(const o of e){const e=r[o]
|
|
56
|
+
null!=e?(t=!0,n.push(String(e))):n.push("")}if(!t)return
|
|
57
|
+
return n.join("")}return r[e]}
|
|
58
|
+
return function r(n){const e=[]
|
|
59
|
+
for(const c of n){const n=i(c)
|
|
60
|
+
if(null!=n){if(o.has(n))continue
|
|
61
|
+
o.add(n)}const u={...c},s=c[t.children]
|
|
62
|
+
Array.isArray(s)&&s.length>0&&(u[t.children]=r(s)),e.push(u)}return e}(r)}function T(r,e,t=n){return function r(n){for(let o=0;o<n.length;o++){const i=n[o]
|
|
63
|
+
if(i[t.id]===e)return n.splice(o,1),!0
|
|
64
|
+
const c=i[t.children]
|
|
65
|
+
if(Array.isArray(c)&&c.length>0&&r(c))return!0}return!1}(r)}function p(r,e,t=n){!function r(n){for(const o of n){e(o)
|
|
66
|
+
const n=o[t.children]
|
|
67
|
+
Array.isArray(n)&&n.length>0&&r(n)}}(r)}function m(r){return!Array.isArray(r)||0===r.length}function v(r,e=n){if(!S(r,e))return!0
|
|
61
68
|
const t=r[e.children]
|
|
62
|
-
return void 0===t||!Array.isArray(t)||0===t.length}function
|
|
63
|
-
const n=
|
|
69
|
+
return void 0===t||!Array.isArray(t)||0===t.length}function b(r,e,t=n){const o=function r(n){for(const o of n){if(o[t.id]===e)return o
|
|
70
|
+
const n=o[t.children]
|
|
64
71
|
if(Array.isArray(n)&&n.length>0){const e=r(n)
|
|
65
72
|
if(null!==e)return e}}return null}(r)
|
|
66
|
-
if(!
|
|
67
|
-
const o
|
|
68
|
-
return Array.isArray(
|
|
69
|
-
const n=
|
|
70
|
-
if(Array.isArray(n)&&n.length>0){const e=r(n,
|
|
73
|
+
if(!o)return[]
|
|
74
|
+
const i=o[t.children]
|
|
75
|
+
return Array.isArray(i)?i:[]}function D(r,e,t=n){const o=function r(n,o=null){for(const i of n){if(i[t.id]===e)return{node:i,parent:o}
|
|
76
|
+
const n=i[t.children]
|
|
77
|
+
if(Array.isArray(n)&&n.length>0){const e=r(n,i)
|
|
71
78
|
if(null!==e)return e}}return null}(r)
|
|
72
|
-
if(!
|
|
73
|
-
if(null===
|
|
74
|
-
const o
|
|
75
|
-
return Array.isArray(
|
|
76
|
-
const n=
|
|
77
|
-
if(Array.isArray(n)&&n.length>0){const e=r(n,
|
|
78
|
-
if(null!==e)return e}}return null}(r)}function
|
|
79
|
+
if(!o)return[]
|
|
80
|
+
if(null===o.parent)return r
|
|
81
|
+
const i=o.parent[t.children]
|
|
82
|
+
return Array.isArray(i)?i:[]}function x(r,e,t=n){return function r(n,o=null){for(const i of n){if(i[t.id]===e)return o
|
|
83
|
+
const n=i[t.children]
|
|
84
|
+
if(Array.isArray(n)&&n.length>0){const e=r(n,i)
|
|
85
|
+
if(null!==e)return e}}return null}(r)}function N(r,e,t=n){for(const n of r){if(n[t.id]===e)return!0
|
|
79
86
|
const r=n[t.children]
|
|
80
|
-
if(Array.isArray(r)&&r.length>0&&
|
|
87
|
+
if(Array.isArray(r)&&r.length>0&&N(r,e,t))return!0}return!1}function S(r,e=n){if(!r||"object"!=typeof r||Array.isArray(r))return!1
|
|
81
88
|
const t=r[e.children]
|
|
82
89
|
if(void 0!==t){if(null===t)return!1
|
|
83
90
|
if(!Array.isArray(t))return!1
|
|
84
|
-
for(const r of t)if(!
|
|
85
|
-
for(const n of r)if(!
|
|
86
|
-
return!0}function
|
|
91
|
+
for(const r of t)if(!S(r,e))return!1}return!0}function B(r,e=n){if(!Array.isArray(r))return!1
|
|
92
|
+
for(const n of r)if(!S(n,e))return!1
|
|
93
|
+
return!0}function L(r,e=n){if(!r||"object"!=typeof r||Array.isArray(r))return!1
|
|
87
94
|
const t=r[e.children]
|
|
88
|
-
return void 0===t||Array.isArray(t)}function
|
|
95
|
+
return void 0===t||Array.isArray(t)}function j(r,e=n,t=new WeakSet){if(!r||"object"!=typeof r||Array.isArray(r))return!1
|
|
89
96
|
if(t.has(r))return!1
|
|
90
97
|
t.add(r)
|
|
91
|
-
const
|
|
92
|
-
if(void 0!==
|
|
93
|
-
if(!Array.isArray(
|
|
94
|
-
for(const r of
|
|
98
|
+
const o=r[e.children]
|
|
99
|
+
if(void 0!==o){if(null===o)return!1
|
|
100
|
+
if(!Array.isArray(o))return!1
|
|
101
|
+
for(const r of o)if(!j(r,e,t))return!1}return!0}function w(r,e,t=n){return!(e<=0)&&function r(n,o){if(o>e)return!1
|
|
95
102
|
for(const e of n){const n=e[t.children]
|
|
96
|
-
if(Array.isArray(n)&&n.length>0&&!r(n,
|
|
97
|
-
return void 0===t||!Array.isArray(t)||0===t.length}function
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
103
|
+
if(Array.isArray(n)&&n.length>0&&!r(n,o+1))return!1}return!0}(r,1)}function O(r,e=n){const t=r[e.children]
|
|
104
|
+
return void 0===t||!Array.isArray(t)||0===t.length}function P(r,e,t=n){const o=function r(n,o=null){for(const i of n){if(i[t.id]===e)return{found:!0,isRoot:null===o}
|
|
105
|
+
const n=i[t.children]
|
|
106
|
+
if(Array.isArray(n)&&n.length>0){const e=r(n,i)
|
|
107
|
+
if(e.found)return e}}return{found:!1,isRoot:!1}}(r)
|
|
108
|
+
return o.found&&o.isRoot}function M(r,e=n){const t=[]
|
|
109
|
+
return function r(n){for(const o of n){const{[e.children]:n,...i}=o
|
|
110
|
+
t.push(i)
|
|
111
|
+
const c=o[e.children]
|
|
112
|
+
Array.isArray(c)&&c.length>0&&r(c)}}(r),t}function E(r,e={}){const{rootParentId:t=null,parentIdField:o="parentId",fieldNames:i=n}=e
|
|
102
113
|
if(Array.isArray(r)){if(0===r.length)return[]
|
|
103
114
|
const n=new Map
|
|
104
|
-
for(const e of r){const r=e[
|
|
115
|
+
for(const e of r){const r=e[i.id]
|
|
105
116
|
if(null==r)continue
|
|
106
|
-
const t={...e,[
|
|
117
|
+
const t={...e,[i.children]:[]}
|
|
107
118
|
n.set(r,t)}const e=[]
|
|
108
|
-
for(const c of r){const r=c[
|
|
119
|
+
for(const c of r){const r=c[i.id],u=c[o]
|
|
109
120
|
if(null==r)continue
|
|
110
|
-
const
|
|
111
|
-
if(
|
|
112
|
-
else{const r=n.get(
|
|
113
|
-
if(r){const n=r[
|
|
114
|
-
Array.isArray(n)?n.push(
|
|
121
|
+
const s=n.get(r)
|
|
122
|
+
if(s)if(u===t||null==u)e.push(s)
|
|
123
|
+
else{const r=n.get(u)
|
|
124
|
+
if(r){const n=r[i.children]
|
|
125
|
+
Array.isArray(n)?n.push(s):r[i.children]=[s]}else e.push(s)}}return e}if(r instanceof Map){if(0===r.size)return[]
|
|
115
126
|
const n=[]
|
|
116
|
-
return r.forEach((r,e)=>{n.push({...r,[
|
|
127
|
+
return r.forEach((r,e)=>{n.push({...r,[i.id]:e})}),E(n,e)}if(r&&"object"==typeof r&&!Array.isArray(r)&&!(r instanceof Map)){const n=r[i.children]
|
|
117
128
|
if(Array.isArray(n))return[r]
|
|
118
129
|
const t=Object.keys(r)
|
|
119
130
|
if(t.length>0){let n=!0
|
|
@@ -121,14 +132,81 @@ for(const e of t){const t=r[e]
|
|
|
121
132
|
if(!t||"object"!=typeof t||Array.isArray(t)){n=!1
|
|
122
133
|
break}}if(n){const n=[]
|
|
123
134
|
for(const e of t){const t=r[e]
|
|
124
|
-
n.push({...t,[
|
|
125
|
-
return function r(n){for(const
|
|
126
|
-
if(null!=n){const{[e.children]:r,...
|
|
127
|
-
t.set(n,
|
|
128
|
-
Array.isArray(
|
|
129
|
-
return Array.isArray(r)&&0!==r.length?(function r(n,
|
|
130
|
-
for(const
|
|
131
|
-
t[
|
|
132
|
-
const
|
|
133
|
-
Array.isArray(
|
|
134
|
-
|
|
135
|
+
n.push({...t,[i.id]:e})}if(n.length>0)return E(n,e)}}return[{...r,[i.children]:[]}]}return[]}function F(r,e=n){const t=new Map
|
|
136
|
+
return function r(n){for(const o of n){const n=o[e.id]
|
|
137
|
+
if(null!=n){const{[e.children]:r,...i}=o
|
|
138
|
+
t.set(n,i)}const i=o[e.children]
|
|
139
|
+
Array.isArray(i)&&i.length>0&&r(i)}}(r),t}function R(r,e=n){const t=[]
|
|
140
|
+
return Array.isArray(r)&&0!==r.length?(function r(n,o=0){t[o]||(t[o]=[])
|
|
141
|
+
for(const i of n){const{[e.children]:n,...c}=i
|
|
142
|
+
t[o].push(c)
|
|
143
|
+
const u=i[e.children]
|
|
144
|
+
Array.isArray(u)&&u.length>0&&r(u,o+1)}}(r),t):[]}function W(r){return Array.isArray(r)&&0!==r.length&&1===r.length?r[0]:null}function k(r,e=n){if(!Array.isArray(r)||0===r.length)return[]
|
|
145
|
+
function t(r){const n={...r},o=r[e.children]
|
|
146
|
+
return Array.isArray(o)&&o.length>0&&(n[e.children]=o.map(r=>t(r))),n}return r.map(r=>t(r))}function C(r){return Array.isArray(r)&&0!==r.length?r.map(r=>({...r})):[]}function I(r,e,t=n){if(!Array.isArray(r)||0===r.length)return[]
|
|
147
|
+
if(!e||"object"!=typeof e||Array.isArray(e)||e.constructor!==Object)throw new Error('cloneSubtree: 必须传入对象,例如 { id: 1 } 或 { name: "xxx" }')
|
|
148
|
+
const o=Object.keys(e)
|
|
149
|
+
if(0===o.length)return[]
|
|
150
|
+
if(o.length>1)throw new Error('cloneSubtree: 查找对象只能包含一个字段,例如 { id: 1 } 或 { name: "xxx" }')
|
|
151
|
+
const i=o[0],c=e[i],u=function r(n){for(const e of n){if(e[i]===c)return e
|
|
152
|
+
const n=e[t.children]
|
|
153
|
+
if(Array.isArray(n)&&n.length>0){const e=r(n)
|
|
154
|
+
if(e)return e}}return null}(r)
|
|
155
|
+
return u?[function r(n){const e={...n},o=n[t.children]
|
|
156
|
+
return Array.isArray(o)&&o.length>0&&(e[t.children]=o.map(n=>r(n))),e}(u)]:[]}function V(r,e,t=n){if(!Array.isArray(r)||0===r.length)return[]
|
|
157
|
+
function o(r){const n={...e(r)},i=r[t.children]
|
|
158
|
+
return Array.isArray(i)&&i.length>0&&(n[t.children]=i.map(r=>o(r))),n}return r.map(r=>o(r))}function z(...r){const e=[]
|
|
159
|
+
for(const t of r)Array.isArray(t)&&t.length>0&&e.push(...k(t,n))
|
|
160
|
+
return e}function _(r,e,t=n){if(!Array.isArray(r)||0===r.length)return[]
|
|
161
|
+
const o=k(r,t)
|
|
162
|
+
return function r(n){n.sort(e)
|
|
163
|
+
for(const e of n){const n=e[t.children]
|
|
164
|
+
Array.isArray(n)&&n.length>0&&r(n)}}(o),o}function q(r,e,t,o=n){let i=t
|
|
165
|
+
return function r(n){for(const t of n){i=e(i,t)
|
|
166
|
+
const n=t[o.children]
|
|
167
|
+
Array.isArray(n)&&n.length>0&&r(n)}}(r),i}function G(r,e,t,o=n){return Array.isArray(r)&&0!==r.length?k(r.slice(e,t),o):[]}function H(r,e,t=n){const o={}
|
|
168
|
+
!function r(n){for(const i of n){const n=String(e.groupBy(i))
|
|
169
|
+
if(!o[n]){o[n]={}
|
|
170
|
+
for(const[r,t]of Object.entries(e.aggregations))"count"===t.operation?o[n][r]=0:"avg"===t.operation?o[n][r]={sum:0,count:0}:"max"===t.operation||"min"===t.operation?o[n][r]=t.field?i[t.field]??null:null:o[n][r]=0}for(const[r,t]of Object.entries(e.aggregations)){const{operation:e,field:c}=t
|
|
171
|
+
if("count"===e)o[n][r]++
|
|
172
|
+
else if("avg"===e){if(c){const e=i[c]??0
|
|
173
|
+
o[n][r].sum+=e,o[n][r].count++}}else if("max"===e){if(c){const e=i[c]
|
|
174
|
+
null!=e&&(null===o[n][r]||e>o[n][r])&&(o[n][r]=e)}}else if("min"===e){if(c){const e=i[c]
|
|
175
|
+
null!=e&&(null===o[n][r]||e<o[n][r])&&(o[n][r]=e)}}else"sum"===e&&c&&(o[n][r]+=i[c]??0)}const c=i[t.children]
|
|
176
|
+
Array.isArray(c)&&c.length>0&&r(c)}}(r)
|
|
177
|
+
for(const r in o)for(const[n,t]of Object.entries(e.aggregations))"avg"===t.operation&&o[r][n].count>0?o[r][n]=o[r][n].sum/o[r][n].count:"avg"===t.operation&&0===o[r][n].count&&(o[r][n]=0)
|
|
178
|
+
return o}function J(r,e,t=n){const o={}
|
|
179
|
+
return function r(n){for(const i of n){const n=String(i[e]??"")
|
|
180
|
+
o[n]||(o[n]=[]),o[n].push(i)
|
|
181
|
+
const c=i[t.children]
|
|
182
|
+
Array.isArray(c)&&c.length>0&&r(c)}}(r),o}function K(r,e,t=n){const o={}
|
|
183
|
+
return function r(n){for(const i of n){const n=String(e(i)??"")
|
|
184
|
+
o[n]||(o[n]=[]),o[n].push(i)
|
|
185
|
+
const c=i[t.children]
|
|
186
|
+
Array.isArray(c)&&c.length>0&&r(c)}}(r),o}function Q(r,e,t=n){return q(r,(r,n)=>r+(n[e]??0),0,t)}function U(r,e,t=n){let o=0,i=0
|
|
187
|
+
return q(r,(r,n)=>{const t=n[e]
|
|
188
|
+
return null!=t&&(o+=t,i++),null},null,t),i>0?o/i:0}function X(r,e,t=n){let o=null,i=!1
|
|
189
|
+
return q(r,(r,n)=>{const t=n[e]
|
|
190
|
+
return null!=t&&"number"==typeof t&&(!i||t>o)&&(o=t,i=!0),null},null,t),i?o:null}function Y(r,e,t=n){let o=null,i=!1
|
|
191
|
+
return q(r,(r,n)=>{const t=n[e]
|
|
192
|
+
return null!=t&&"number"==typeof t&&(!i||t<o)&&(o=t,i=!0),null},null,t),i?o:null}function Z(r,e,t=n){return q(r,e?(r,n)=>r+(e(n)?1:0):r=>r+1,0,t)}function $(r,e=n){if(!Array.isArray(r)||0===r.length)return{totalNodes:0,leafNodes:0,maxDepth:0,minDepth:0,avgDepth:0,levels:0}
|
|
193
|
+
let t=0,o=0,i=0,c=1/0,u=0
|
|
194
|
+
return function r(n,s){for(const l of n){t++,u+=s,s>i&&(i=s),s<c&&(c=s)
|
|
195
|
+
const n=l[e.children]
|
|
196
|
+
Array.isArray(n)&&n.length>0?r(n,s+1):o++}}(r,1),{totalNodes:t,leafNodes:o,maxDepth:i,minDepth:c===1/0?0:c,avgDepth:t>0?u/t:0,levels:i}}function rr(r,e={},t=n){const{includeBasic:o=!0,includeLevelAnalysis:i=!0,includeBranchingFactor:c=!0,includeDepthDistribution:u=!0,includeBalanceAnalysis:s=!0,includePathAnalysis:l=!0,includeLeafAnalysis:f=!0}=e
|
|
197
|
+
if(!Array.isArray(r)||0===r.length)return{totalNodes:0,leafNodes:0,internalNodes:0,maxDepth:0,minDepth:0,avgDepth:0,levels:0,byLevel:{},maxWidth:0,avgWidth:0,widthByLevel:{},avgBranchingFactor:0,maxBranchingFactor:0,minBranchingFactor:0,branchingFactorDistribution:{},depthDistribution:{},depthVariance:0,isBalanced:!1,balanceRatio:0,avgPathLength:0,maxPathLength:0,minPathLength:0,leafNodeRatio:0,leafNodesByLevel:{}}
|
|
198
|
+
const a={},h={},d={},y={},A={}
|
|
199
|
+
let g=0,T=0,p=0,m=0,v=1/0,b=0,D=0,x=0,N=0,S=0,B=0,L=0,j=1/0,w=0,O=0,P=1/0
|
|
200
|
+
const M=[]
|
|
201
|
+
!function r(n,e,E){const F=n.length
|
|
202
|
+
i&&(a[e]=(a[e]||0)+F,h[e]=(h[e]||0)+F,F>D&&(D=F),x+=F,N++)
|
|
203
|
+
for(const i of n){g++
|
|
204
|
+
const n=e+1
|
|
205
|
+
if(o&&(b+=n,n>m&&(m=n),n<v&&(v=n)),s&&M.push(n),u&&(d[n]=(d[n]||0)+1),l){const r=E+1
|
|
206
|
+
w+=r,r>O&&(O=r),r<P&&(P=r)}const a=i[t.children],h=Array.isArray(a)?a.length:0
|
|
207
|
+
h>0?(o&&p++,c&&(S+=h,B++,A[h]=(A[h]||0)+1,h>L&&(L=h),h<j&&(j=h)),r(a,n,l?E+1:E)):(o&&T++,f&&(y[n]=(y[n]||0)+1))}}(r,0,0)
|
|
208
|
+
const E=o&&g>0?b/g:0,F=i&&N>0?x/N:0,R=c&&B>0?S/B:0,W=l&&g>0?w/g:0,k=f&&g>0?T/g:0
|
|
209
|
+
let C=0,I=!1,V=0
|
|
210
|
+
if(s){if(M.length>0){const r=E
|
|
211
|
+
C=M.reduce((n,e)=>n+Math.pow(e-r,2),0)/M.length}I=C<2&&m-v<=2,V=m>0?v/m:0}return{totalNodes:o?g:0,leafNodes:o?T:0,internalNodes:o?p:0,maxDepth:o?m:0,minDepth:o?v===1/0?0:v:0,avgDepth:E,levels:o?m:0,byLevel:i?a:{},maxWidth:i?D:0,avgWidth:F,widthByLevel:i?h:{},avgBranchingFactor:R,maxBranchingFactor:c?0===L?0:L:0,minBranchingFactor:c?j===1/0?0:j:0,branchingFactorDistribution:c?A:{},depthDistribution:u?d:{},depthVariance:C,isBalanced:I,balanceRatio:V,avgPathLength:W,maxPathLength:l?O:0,minPathLength:l?P===1/0?0:P:0,leafNodeRatio:k,leafNodesByLevel:f?y:{}}}const nr={mapTree:e,filterTree:t,findTree:o,pushTree:i,unshiftTree:c,popTree:u,shiftTree:s,someTree:l,everyTree:f,atTree:a,indexOfTree:h,atIndexOfTree:d,getNodeDepthMap:y,getNodeDepth:A,dedupTree:g,removeTree:T,forEachTree:p,isEmptyTreeData:m,isEmptySingleTreeData:v,getParentTree:x,getChildrenTree:b,getSiblingsTree:D,includesTree:N,isSingleTreeData:S,isTreeData:B,isValidTreeNode:L,isTreeNodeWithCircularCheck:j,isSafeTreeDepth:w,isLeafNode:O,isRootNode:P,convertToArrayTree:M,convertBackTree:E,convertToMapTree:F,convertToLevelArrayTree:R,convertToObjectTree:W,cloneTree:k,shallowCloneTree:C,cloneSubtree:I,cloneWithTransform:V,concatTree:z,sortTree:_,reduceTree:q,sliceTree:G,aggregateTree:H,groupTree:J,groupByTree:K,sumTree:Q,avgTree:U,maxTree:X,minTree:Y,countTree:Z,getTreeStats:$,analyzeTree:rr}
|
|
212
|
+
r.aggregateTree=H,r.analyzeTree=rr,r.atIndexOfTree=d,r.atTree=a,r.avgTree=U,r.cloneSubtree=I,r.cloneTree=k,r.cloneWithTransform=V,r.concatTree=z,r.convertBackTree=E,r.convertToArrayTree=M,r.convertToLevelArrayTree=R,r.convertToMapTree=F,r.convertToObjectTree=W,r.countTree=Z,r.dedupTree=g,r.default=nr,r.everyTree=f,r.filterTree=t,r.findTree=o,r.forEachTree=p,r.getChildrenTree=b,r.getNodeDepth=A,r.getNodeDepthMap=y,r.getParentTree=x,r.getSiblingsTree=D,r.getTreeStats=$,r.groupByTree=K,r.groupTree=J,r.includesTree=N,r.indexOfTree=h,r.isEmptySingleTreeData=v,r.isEmptyTreeData=m,r.isLeafNode=O,r.isRootNode=P,r.isSafeTreeDepth=w,r.isSingleTreeData=S,r.isTreeData=B,r.isTreeNodeWithCircularCheck=j,r.isValidTreeNode=L,r.mapTree=e,r.maxTree=X,r.minTree=Y,r.popTree=u,r.pushTree=i,r.reduceTree=q,r.removeTree=T,r.shallowCloneTree=C,r.shiftTree=s,r.sliceTree=G,r.someTree=l,r.sortTree=_,r.sumTree=Q,r.unshiftTree=c,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.11.0",
|
|
4
|
+
"description": "Lightweight tree-structured data processing library with 50+ APIs (traversal, search, modification, conversion, query, analysis, validation). Supports ESM/CJS/UMD, tree-shaking, zero dependencies",
|
|
5
5
|
"main": "dist/tree-processor.cjs.js",
|
|
6
6
|
"module": "dist/tree-processor.esm.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -24,6 +24,8 @@
|
|
|
24
24
|
"test": "npm run build && vitest run src/index-all-dist.test.ts",
|
|
25
25
|
"test:src": "vitest run src/index.test.ts",
|
|
26
26
|
"test:dist": "npm run build && vitest run src/index-dist-only.test.ts",
|
|
27
|
+
"test:performance": "vitest run src/index.performance.test.ts",
|
|
28
|
+
"test:performance:all": "vitest run src/index.all-performance.test.ts",
|
|
27
29
|
"test:coverage": "vitest --coverage",
|
|
28
30
|
"update:badge": "node scripts/update-coverage-badge.js",
|
|
29
31
|
"update:downloads": "node scripts/update-downloads-badge.js",
|