tree-processor 0.10.0 → 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 +726 -34
- package/README.md +727 -36
- 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 +130 -55
- package/dist/tree-processor.esm.js +116 -41
- package/dist/tree-processor.umd.js +174 -99
- package/package.json +4 -2
|
@@ -1,122 +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,o=r=>{if("function"==typeof e)return e(r)
|
|
51
|
-
if(Array.isArray(e)){const n=e.map(n=>r[n])
|
|
52
|
-
if(n.every(r=>null==r))return
|
|
53
|
-
return JSON.stringify(n)}return r[e]}
|
|
54
|
-
return function r(n){const e=[]
|
|
55
|
-
for(const c of n){const n=o(c)
|
|
56
|
-
if(null!=n){if(i.has(n))continue
|
|
57
|
-
i.add(n)}const f={...c},u=c[t.children]
|
|
58
|
-
Array.isArray(u)&&u.length>0&&(f[t.children]=r(u)),e.push(f)}return e}(r)}function g(r,e,t=n){return function r(n){for(let i=0;i<n.length;i++){const o=n[i]
|
|
59
|
-
if(o[t.id]===e)return n.splice(i,1),!0
|
|
60
|
-
const c=o[t.children]
|
|
61
|
-
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
|
|
62
50
|
const n=i[t.children]
|
|
63
|
-
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
|
|
64
68
|
const t=r[e.children]
|
|
65
|
-
return void 0===t||!Array.isArray(t)||0===t.length}function
|
|
66
|
-
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]
|
|
67
71
|
if(Array.isArray(n)&&n.length>0){const e=r(n)
|
|
68
72
|
if(null!==e)return e}}return null}(r)
|
|
69
|
-
if(!
|
|
70
|
-
const o
|
|
71
|
-
return Array.isArray(
|
|
72
|
-
const n=
|
|
73
|
-
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)
|
|
74
78
|
if(null!==e)return e}}return null}(r)
|
|
75
|
-
if(!
|
|
76
|
-
if(null===
|
|
77
|
-
const o
|
|
78
|
-
return Array.isArray(
|
|
79
|
-
const n=
|
|
80
|
-
if(Array.isArray(n)&&n.length>0){const e=r(n,
|
|
81
|
-
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
|
|
82
86
|
const r=n[t.children]
|
|
83
|
-
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
|
|
84
88
|
const t=r[e.children]
|
|
85
89
|
if(void 0!==t){if(null===t)return!1
|
|
86
90
|
if(!Array.isArray(t))return!1
|
|
87
|
-
for(const r of t)if(!
|
|
88
|
-
for(const n of r)if(!
|
|
89
|
-
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
|
|
90
94
|
const t=r[e.children]
|
|
91
|
-
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
|
|
92
96
|
if(t.has(r))return!1
|
|
93
97
|
t.add(r)
|
|
94
|
-
const
|
|
95
|
-
if(void 0!==
|
|
96
|
-
if(!Array.isArray(
|
|
97
|
-
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
|
|
98
102
|
for(const e of n){const n=e[t.children]
|
|
99
|
-
if(Array.isArray(n)&&n.length>0&&!r(n,
|
|
100
|
-
return void 0===t||!Array.isArray(t)||0===t.length}function
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
|
105
113
|
if(Array.isArray(r)){if(0===r.length)return[]
|
|
106
114
|
const n=new Map
|
|
107
|
-
for(const e of r){const r=e[
|
|
115
|
+
for(const e of r){const r=e[i.id]
|
|
108
116
|
if(null==r)continue
|
|
109
|
-
const t={...e,[
|
|
117
|
+
const t={...e,[i.children]:[]}
|
|
110
118
|
n.set(r,t)}const e=[]
|
|
111
|
-
for(const c of r){const r=c[
|
|
119
|
+
for(const c of r){const r=c[i.id],u=c[o]
|
|
112
120
|
if(null==r)continue
|
|
113
|
-
const
|
|
114
|
-
if(
|
|
115
|
-
else{const r=n.get(
|
|
116
|
-
if(r){const n=r[
|
|
117
|
-
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[]
|
|
118
126
|
const n=[]
|
|
119
|
-
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]
|
|
120
128
|
if(Array.isArray(n))return[r]
|
|
121
129
|
const t=Object.keys(r)
|
|
122
130
|
if(t.length>0){let n=!0
|
|
@@ -124,14 +132,81 @@ for(const e of t){const t=r[e]
|
|
|
124
132
|
if(!t||"object"!=typeof t||Array.isArray(t)){n=!1
|
|
125
133
|
break}}if(n){const n=[]
|
|
126
134
|
for(const e of t){const t=r[e]
|
|
127
|
-
n.push({...t,[
|
|
128
|
-
return function r(n){for(const
|
|
129
|
-
if(null!=n){const{[e.children]:r,...
|
|
130
|
-
t.set(n,
|
|
131
|
-
Array.isArray(
|
|
132
|
-
return Array.isArray(r)&&0!==r.length?(function r(n,
|
|
133
|
-
for(const
|
|
134
|
-
t[
|
|
135
|
-
const
|
|
136
|
-
Array.isArray(
|
|
137
|
-
|
|
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",
|