vmo-tree 0.0.0-beta.3 → 0.0.1

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 CHANGED
@@ -79,19 +79,19 @@ console.log(mappedTree)
79
79
 
80
80
  ## 静态方法
81
81
 
82
- - `VmoTree.array2Tree(array, nodeKey, parentKey, childrenKey)`: 把平展的数组变为树结构。
82
+ - `VmoTree.array2Tree(array, nodeKey, parentKey, childrenKey,keepOrphansAsRoot)`: 把平展的数组变为树结构。
83
83
 
84
84
  - `array`: 需要转化的数组。
85
85
  - `nodeKey`: 节点主键
86
86
  - `parentKey`: 父节点主键
87
87
  - `childrenKey`: 子节点主键
88
+ - `keepOrphansAsRoot`: 孤儿节点是否保留为根节点 defalut:false
88
89
 
89
90
  - `VmoTree.flatten2Array(tree, nodeKey, parentKey, childrenKey)`: 将树形结构展平特定数组。
90
91
  - `tree`: 需要转化的目标树。
91
92
  - `nodeKey`: 节点主键
92
93
  - `parentKey`: 父节点主键
93
94
  - `childrenKey`: 子节点主键
94
- -
95
95
  - `VmoTree.path(predicate, targetTree, childrenKey, pickFirst)`: 获取给定节点到根节点的路径。
96
96
  - `predicate`: 目标节点指针或比对函数。
97
97
  - `targetTree`: 查找树
@@ -108,7 +108,7 @@ console.log(mappedTree)
108
108
  - `childrenKey`: 子节点键名
109
109
  - `prioritizeChildren`: 默认 true 子节点优先会先处理子节点情况,然后再将处理结果合并到父节点访问器中一并判断,false,则会优先处理父节点判定,再进行子节点
110
110
  - `VmoTree.find(targetNode, targetTree, childrenKey, pickFirst)`: 查找树节点。
111
- - `visit`: 目标节点指针或比对函数
111
+ - `visit`: 条件函数,或者也可以给出目标节点的部分属性,方法会自动完成比对,属性给出越多越精准
112
112
  - `targetTree`: 子节点键名
113
113
  - `childrenKey`: 子节点键名
114
114
  - `pickFirst`:仅匹配首个结果
@@ -123,7 +123,7 @@ const vmoTreeInstance = new VmoTree(flatArray, { nodeKey: 'id', parentKey: 'pare
123
123
 
124
124
  ### `find(target, option?)`
125
125
 
126
- - `target`: 查找的条件,可以是节点对象或一个条件函数。
126
+ - `target`: 条件函数,或节点比对数据,同静态方法。
127
127
  - `option`: 可选对象,支持以下属性:
128
128
  - `childrenKey`:指定子节点的键名(本次操作)。
129
129
  - `pickFirst`:布尔值,是否只寻找第一个匹配的节点(本次操作)。
@@ -132,6 +132,8 @@ const vmoTreeInstance = new VmoTree(flatArray, { nodeKey: 'id', parentKey: 'pare
132
132
 
133
133
  - `visit`: 映射函数,接收并返回一个节点对象。
134
134
  - `option`: 可选对象,支持以下属性:
135
+ - `nodeKey`: 节点主键
136
+ - `parentKey`: 父节点主键
135
137
  - `childrenKey`:指定子节点的键名 (本次操作)。
136
138
  - `prioritizeChildren`:子节点是否优先,如子节点优先,则父节点映射处理时,其子节点已先行被映射处理完毕 默认 true。
137
139
 
@@ -144,16 +146,17 @@ const vmoTreeInstance = new VmoTree(flatArray, { nodeKey: 'id', parentKey: 'pare
144
146
 
145
147
  ### `path(targetNode, option?)`
146
148
 
147
- - `targetNode`: 表示要查找路径的节点对象或查找条件。
149
+ - `targetNode`: 条件函数,或节点比对数据。
148
150
  - `option`: 可选对象,包含以下属性:
149
151
  - `childrenKey`:指定子节点的键名。
152
+ - `pickFirst`:布尔值,是否只寻找第一个匹配的节点的路径(本次操作)。
150
153
 
151
154
  ### `flatten(option?)`
152
155
 
153
156
  - `option`: 可选对象,用于指定树展平到数组时的相关配置。
154
- - `nodeKey`: 节点主键
155
- - `parentKey`: 父节点主键
156
- - `childrenKey`: 子节点主键
157
+ - `nodeKey`: 节点主键(本次操作)
158
+ - `parentKey`: 父节点主键(本次操作)
159
+ - `childrenKey`: 子节点主键(本次操作)
157
160
 
158
161
  ## 配置树
159
162
 
@@ -172,7 +175,7 @@ console.log(config)
172
175
 
173
176
  ## 注意事项
174
177
 
175
- - 处理非常深的树可能会导致性能问题。
178
+ - 因为支持 `prioritizeChildren` 子节点优先,`map`, `filter` 处理非常深的树可能会导致性能问题 。
176
179
  - 确保输入的数据结构符合期望的格式,以避免不可预见的错误。
177
180
 
178
181
  通过上面的指南,你可以灵活地操作复杂的树形数据。希望此文档能帮助你快速上手 `VmoTree` 库!
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { VmoTree } from './use.lib/vmo-tree';
2
2
  import type { TreeNode, NodeData, TreeConfig } from './use.lib/vmo-tree';
3
- export { VmoTree, TreeNode, NodeData, TreeConfig };
3
+ export { VmoTree };
4
+ export type { TreeNode, NodeData, TreeConfig };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -21,32 +21,33 @@ export interface TreeConfig {
21
21
  }
22
22
  /**
23
23
  * 查找节点路径(深度优先)
24
- * @param target 目标节点
24
+ * @param target 目标节点, findNodePath 不支持多节点匹配路径,因此他的 target只能是一个对象,不再支持条件函数
25
25
  * @param node 当前子节点
26
26
  * @param childrenKey 子节点键名
27
27
  * @param path 路径递归的数组
28
28
  * @returns {NodeData[] | null}
29
29
  */
30
- export declare function findNodePath(target: NodeData, node: NodeData, childrenKey: string, path?: NodeData[]): NodeData[] | null;
30
+ export declare function findNodePath<T extends NodeData>(target: T, node: T, childrenKey: string, path?: T[], treeRoot?: T | undefined): NodeData[] | null;
31
31
  export declare class VmoTree<T> extends Array {
32
32
  /**
33
- * 将数组转化为 tree 数据
33
+ * 将数组转化为 tree 数据 迭代方法
34
34
  * @param array 需要转化的数组
35
35
  * @param nodeKey 节点主键
36
36
  * @param parentKey 父节点主键
37
37
  * @param childrenKey 子节点键名
38
+ * @param keepOrphansAsRoot 孤儿节点转为根节点
38
39
  * @returns
39
40
  */
40
- static array2Tree<N extends NodeData, children extends string>(array: Record<string, any>[], nodeKey: string | undefined, parentKey: string | undefined, childrenKey: children): VmoTree<TreeNode<N, children>>;
41
+ static array2Tree<N extends NodeData, children extends string>(array: Record<string, any>[], nodeKey?: string, parentKey?: string, childrenKey?: children, keepOrphansAsRoot?: boolean): VmoTree<TreeNode<N, children>>;
41
42
  /**
42
- * 将树数据转换为 array 数组数据
43
+ * 将树数据转换为 array 数组数据 迭代方法
43
44
  * @param targetTree 需要转化的目标树
44
- * @param childrenKey 子节点键名
45
45
  * @param nodekey 节点主键
46
- * @param parentId 父节点主键
46
+ * @param parentKey 父节点主键
47
+ * @param childrenKey 子节点键名
47
48
  * @returns {T[]} 返回数组
48
49
  */
49
- static flatten2Array<T extends Record<string, any>>(targetTree: NodeData[], nodekey?: string, parentKey?: string, childrenKey?: string, array?: NodeData[], parent?: any): T[];
50
+ static flatten2Array<T extends Record<string, any>>(targetTree: NodeData[], nodekey?: string, parentKey?: string, childrenKey?: string): T[];
50
51
  /**
51
52
  * 根据目标节点,或者目标节点条件,返回其到根节点的路径
52
53
  * @param targetNode 目标节点指针或比对函数
@@ -57,7 +58,7 @@ export declare class VmoTree<T> extends Array {
57
58
  */
58
59
  static path<T extends Record<string, any>>(targetNode: T | {
59
60
  (node: T): boolean;
60
- }, targetTree: T[], childrenKey: string, pickFirst?: boolean): T[] | T[][];
61
+ }, targetTree: T[] | T, childrenKey: string, pickFirst?: boolean): T[] | T[][];
61
62
  /**
62
63
  * 树组件过滤核心方法
63
64
  * @param predicate 过滤函数
@@ -75,7 +76,7 @@ export declare class VmoTree<T> extends Array {
75
76
  * @param prioritizeChildren @default true 子节点优先会先处理子节点情况,然后再将处理结果合并到父节点访问器中一并判断,false,则会优先处理父节点判定,再进行子节点处理
76
77
  * @returns {T[]}
77
78
  */
78
- static map<T extends Record<string, any>>(visit: (node: T, index: number, ary: T[]) => T, targetTree: T[], childrenKey: string, prioritizeChildren?: boolean): VmoTree<T>;
79
+ static map<T extends Record<string, any>>(visit: (node: T, index: number, ary: T[]) => T, targetTree: T[] | VmoTree<T>, childrenKey: string, prioritizeChildren?: boolean): T[];
79
80
  /**
80
81
  * find node 的核心方法
81
82
  * @param targetNode 目标节点指针或比对函数
@@ -90,12 +91,12 @@ export declare class VmoTree<T> extends Array {
90
91
  /**
91
92
  * 静态方法构造 VmoTree 实例
92
93
  * @param arr 来源数组(可空)
93
- * @param childrenKey 子节点键名
94
94
  * @param nodeKey 节点主键
95
95
  * @param parentKey 父节点主键
96
+ * @param childrenKey 子节点键名
96
97
  * @returns {VmoTree<T>}
97
98
  */
98
- static treeFrom<T>(arr?: T[], childrenKey?: string, nodeKey?: string, parentKey?: string): VmoTree<T>;
99
+ static treeFrom<T>(arr?: T[], nodeKey?: string, parentKey?: string, childrenKey?: string): VmoTree<T>;
99
100
  /**
100
101
  * 构造函数
101
102
  * @param array 树结构化数据
@@ -111,13 +112,15 @@ export declare class VmoTree<T> extends Array {
111
112
  */
112
113
  find(target: {
113
114
  (value: T, index: number, ary: T[]): boolean;
114
- }, option?: Partial<Pick<TreeConfig, 'childrenKey'> & {
115
+ }, option?: Partial<TreeConfig & {
115
116
  pickFirst: boolean;
116
117
  }>): T | T[] | null;
117
118
  /**
118
119
  * 实例映射方法
119
120
  * @param visit 访问函数
120
121
  * @param {
122
+ * @param nodeKey 节点主键
123
+ * @param parentKey 父节点主键
121
124
  * @param childrenKey 子节点主键
122
125
  * @param prioritizeChildren 子节点优先
123
126
  * }option 操作配置
@@ -125,7 +128,7 @@ export declare class VmoTree<T> extends Array {
125
128
  */
126
129
  map<T>(visit: (value: any, index: number, array: any[]) => T, option?: Partial<Pick<TreeConfig, 'childrenKey'> & {
127
130
  prioritizeChildren: boolean;
128
- }>): T[];
131
+ }>): T[] & VmoTree<T>;
129
132
  /**
130
133
  * 实例过滤方法
131
134
  * @param predicate 过滤方法
@@ -134,10 +137,10 @@ export declare class VmoTree<T> extends Array {
134
137
  */
135
138
  filter<S extends T>(predicate: (value: T, index: number, array: T[]) => boolean, option?: Partial<Pick<TreeConfig, 'childrenKey'> & {
136
139
  prioritizeChildren: boolean;
137
- }>): S[];
140
+ }>): S[] & VmoTree<T>;
138
141
  filter(predicate: (value: T, index: number, array: T[]) => boolean, option?: Partial<Pick<TreeConfig, 'childrenKey'> & {
139
142
  prioritizeChildren: boolean;
140
- }>): T[];
143
+ }>): T[] & VmoTree<T>;
141
144
  /**
142
145
  * 获取指定或条件节点的路径,允许多结果集
143
146
  * @param targetNode 目标节点指针或比对函数
@@ -159,7 +162,7 @@ export declare class VmoTree<T> extends Array {
159
162
  * 设置树的配置
160
163
  * @param option 树结构配置
161
164
  */
162
- setConfig(option: Partial<TreeConfig>): void;
165
+ setConfig(option?: Partial<TreeConfig>): void;
163
166
  /**
164
167
  * 获取树结构配置
165
168
  * @returns {TreeConfig}
package/dist/vmo-tree.js CHANGED
@@ -1,60 +1,213 @@
1
- const O = {
1
+ const P = {
2
2
  "@@functional/placeholder": !0
3
3
  };
4
- function l(e) {
5
- return e === O;
4
+ function A(t) {
5
+ return t === P;
6
6
  }
7
- function h(e) {
8
- return function t(r) {
9
- return arguments.length === 0 || l(r) ? t : e.apply(this, arguments);
7
+ function y(t) {
8
+ return function r(e) {
9
+ return arguments.length === 0 || A(e) ? r : t.apply(this, arguments);
10
10
  };
11
11
  }
12
- function w(e) {
13
- return function t(r, s) {
12
+ function x(t) {
13
+ return function r(e, n) {
14
14
  switch (arguments.length) {
15
15
  case 0:
16
- return t;
16
+ return r;
17
17
  case 1:
18
- return l(r) ? t : h(function(u) {
19
- return e(r, u);
18
+ return A(e) ? r : y(function(s) {
19
+ return t(e, s);
20
20
  });
21
21
  default:
22
- return l(r) && l(s) ? t : l(r) ? h(function(u) {
23
- return e(u, s);
24
- }) : l(s) ? h(function(u) {
25
- return e(r, u);
26
- }) : e(r, s);
22
+ return A(e) && A(n) ? r : A(e) ? y(function(s) {
23
+ return t(s, n);
24
+ }) : A(n) ? y(function(s) {
25
+ return t(e, s);
26
+ }) : t(e, n);
27
27
  }
28
28
  };
29
29
  }
30
- var j = /* @__PURE__ */ h(function(t) {
31
- return t === null ? "Null" : t === void 0 ? "Undefined" : Object.prototype.toString.call(t).slice(8, -1);
30
+ function j(t) {
31
+ for (var r = [], e; !(e = t.next()).done; )
32
+ r.push(e.value);
33
+ return r;
34
+ }
35
+ function q(t, r, e) {
36
+ for (var n = 0, s = e.length; n < s; ) {
37
+ if (t(r, e[n]))
38
+ return !0;
39
+ n += 1;
40
+ }
41
+ return !1;
42
+ }
43
+ function U(t) {
44
+ var r = String(t).match(/^function (\w*)/);
45
+ return r == null ? "" : r[1];
46
+ }
47
+ function g(t, r) {
48
+ return Object.prototype.hasOwnProperty.call(r, t);
49
+ }
50
+ function C(t, r) {
51
+ return t === r ? t !== 0 || 1 / t === 1 / r : t !== t && r !== r;
52
+ }
53
+ const v = typeof Object.is == "function" ? Object.is : C;
54
+ var E = Object.prototype.toString, F = /* @__PURE__ */ function() {
55
+ return E.call(arguments) === "[object Arguments]" ? function(r) {
56
+ return E.call(r) === "[object Arguments]";
57
+ } : function(r) {
58
+ return g("callee", r);
59
+ };
60
+ }();
61
+ const R = F;
62
+ var $ = !/* @__PURE__ */ {
63
+ toString: null
64
+ }.propertyIsEnumerable("toString"), I = ["constructor", "valueOf", "isPrototypeOf", "toString", "propertyIsEnumerable", "hasOwnProperty", "toLocaleString"], p = /* @__PURE__ */ function() {
65
+ return arguments.propertyIsEnumerable("length");
66
+ }(), B = function(r, e) {
67
+ for (var n = 0; n < r.length; ) {
68
+ if (r[n] === e)
69
+ return !0;
70
+ n += 1;
71
+ }
72
+ return !1;
73
+ }, M = /* @__PURE__ */ y(typeof Object.keys == "function" && !p ? function(r) {
74
+ return Object(r) !== r ? [] : Object.keys(r);
75
+ } : function(r) {
76
+ if (Object(r) !== r)
77
+ return [];
78
+ var e, n, s = [], c = p && R(r);
79
+ for (e in r)
80
+ g(e, r) && (!c || e !== "length") && (s[s.length] = e);
81
+ if ($)
82
+ for (n = I.length - 1; n >= 0; )
83
+ e = I[n], g(e, r) && !B(s, e) && (s[s.length] = e), n -= 1;
84
+ return s;
85
+ });
86
+ const S = M;
87
+ var z = /* @__PURE__ */ y(function(r) {
88
+ return r === null ? "Null" : r === void 0 ? "Undefined" : Object.prototype.toString.call(r).slice(8, -1);
32
89
  });
33
- const g = j;
34
- function _(e) {
35
- return new RegExp(e.source, e.flags ? e.flags : (e.global ? "g" : "") + (e.ignoreCase ? "i" : "") + (e.multiline ? "m" : "") + (e.sticky ? "y" : "") + (e.unicode ? "u" : "") + (e.dotAll ? "s" : ""));
90
+ const m = z;
91
+ function k(t, r, e, n) {
92
+ var s = j(t), c = j(r);
93
+ function u(a, i) {
94
+ return d(a, i, e.slice(), n.slice());
95
+ }
96
+ return !q(function(a, i) {
97
+ return !q(u, i, a);
98
+ }, c, s);
36
99
  }
37
- function m(e, t, r) {
38
- if (r || (r = new v()), d(e))
39
- return e;
40
- var s = function(n) {
41
- var c = r.get(e);
42
- if (c)
43
- return c;
44
- r.set(e, n);
45
- for (var f in e)
46
- Object.prototype.hasOwnProperty.call(e, f) && (n[f] = t ? m(e[f], !0, r) : e[f]);
47
- return n;
100
+ function d(t, r, e, n) {
101
+ if (v(t, r))
102
+ return !0;
103
+ var s = m(t);
104
+ if (s !== m(r))
105
+ return !1;
106
+ if (typeof t["fantasy-land/equals"] == "function" || typeof r["fantasy-land/equals"] == "function")
107
+ return typeof t["fantasy-land/equals"] == "function" && t["fantasy-land/equals"](r) && typeof r["fantasy-land/equals"] == "function" && r["fantasy-land/equals"](t);
108
+ if (typeof t.equals == "function" || typeof r.equals == "function")
109
+ return typeof t.equals == "function" && t.equals(r) && typeof r.equals == "function" && r.equals(t);
110
+ switch (s) {
111
+ case "Arguments":
112
+ case "Array":
113
+ case "Object":
114
+ if (typeof t.constructor == "function" && U(t.constructor) === "Promise")
115
+ return t === r;
116
+ break;
117
+ case "Boolean":
118
+ case "Number":
119
+ case "String":
120
+ if (!(typeof t == typeof r && v(t.valueOf(), r.valueOf())))
121
+ return !1;
122
+ break;
123
+ case "Date":
124
+ if (!v(t.valueOf(), r.valueOf()))
125
+ return !1;
126
+ break;
127
+ case "Error":
128
+ return t.name === r.name && t.message === r.message;
129
+ case "RegExp":
130
+ if (!(t.source === r.source && t.global === r.global && t.ignoreCase === r.ignoreCase && t.multiline === r.multiline && t.sticky === r.sticky && t.unicode === r.unicode))
131
+ return !1;
132
+ break;
133
+ }
134
+ for (var c = e.length - 1; c >= 0; ) {
135
+ if (e[c] === t)
136
+ return n[c] === r;
137
+ c -= 1;
138
+ }
139
+ switch (s) {
140
+ case "Map":
141
+ return t.size !== r.size ? !1 : k(t.entries(), r.entries(), e.concat([t]), n.concat([r]));
142
+ case "Set":
143
+ return t.size !== r.size ? !1 : k(t.values(), r.values(), e.concat([t]), n.concat([r]));
144
+ case "Arguments":
145
+ case "Array":
146
+ case "Object":
147
+ case "Boolean":
148
+ case "Number":
149
+ case "String":
150
+ case "Date":
151
+ case "Error":
152
+ case "RegExp":
153
+ case "Int8Array":
154
+ case "Uint8Array":
155
+ case "Uint8ClampedArray":
156
+ case "Int16Array":
157
+ case "Uint16Array":
158
+ case "Int32Array":
159
+ case "Uint32Array":
160
+ case "Float32Array":
161
+ case "Float64Array":
162
+ case "ArrayBuffer":
163
+ break;
164
+ default:
165
+ return !1;
166
+ }
167
+ var u = S(t);
168
+ if (u.length !== S(r).length)
169
+ return !1;
170
+ var a = e.concat([t]), i = n.concat([r]);
171
+ for (c = u.length - 1; c >= 0; ) {
172
+ var f = u[c];
173
+ if (!(g(f, r) && d(r[f], t[f], a, i)))
174
+ return !1;
175
+ c -= 1;
176
+ }
177
+ return !0;
178
+ }
179
+ var D = /* @__PURE__ */ x(function(r, e) {
180
+ return d(r, e, [], []);
181
+ });
182
+ const O = D;
183
+ var K = /* @__PURE__ */ y(function(r) {
184
+ return r == null;
185
+ });
186
+ const L = K;
187
+ function W(t) {
188
+ return new RegExp(t.source, t.flags ? t.flags : (t.global ? "g" : "") + (t.ignoreCase ? "i" : "") + (t.multiline ? "m" : "") + (t.sticky ? "y" : "") + (t.unicode ? "u" : "") + (t.dotAll ? "s" : ""));
189
+ }
190
+ function b(t, r, e) {
191
+ if (e || (e = new H()), G(t))
192
+ return t;
193
+ var n = function(c) {
194
+ var u = e.get(t);
195
+ if (u)
196
+ return u;
197
+ e.set(t, c);
198
+ for (var a in t)
199
+ Object.prototype.hasOwnProperty.call(t, a) && (c[a] = r ? b(t[a], !0, e) : t[a]);
200
+ return c;
48
201
  };
49
- switch (g(e)) {
202
+ switch (m(t)) {
50
203
  case "Object":
51
- return s(Object.create(Object.getPrototypeOf(e)));
204
+ return n(Object.create(Object.getPrototypeOf(t)));
52
205
  case "Array":
53
- return s(Array(e.length));
206
+ return n(Array(t.length));
54
207
  case "Date":
55
- return new Date(e.valueOf());
208
+ return new Date(t.valueOf());
56
209
  case "RegExp":
57
- return _(e);
210
+ return W(t);
58
211
  case "Int8Array":
59
212
  case "Uint8Array":
60
213
  case "Uint8ClampedArray":
@@ -66,101 +219,105 @@ function m(e, t, r) {
66
219
  case "Float64Array":
67
220
  case "BigInt64Array":
68
221
  case "BigUint64Array":
69
- return e.slice();
222
+ return t.slice();
70
223
  default:
71
- return e;
224
+ return t;
72
225
  }
73
226
  }
74
- function d(e) {
75
- var t = typeof e;
76
- return e == null || t != "object" && t != "function";
227
+ function G(t) {
228
+ var r = typeof t;
229
+ return t == null || r != "object" && r != "function";
77
230
  }
78
- var v = /* @__PURE__ */ function() {
79
- function e() {
231
+ var H = /* @__PURE__ */ function() {
232
+ function t() {
80
233
  this.map = {}, this.length = 0;
81
234
  }
82
- return e.prototype.set = function(t, r) {
83
- var s = this.hash(t), u = this.map[s];
84
- u || (this.map[s] = u = []), u.push([t, r]), this.length += 1;
85
- }, e.prototype.hash = function(t) {
86
- var r = [];
87
- for (var s in t)
88
- r.push(Object.prototype.toString.call(t[s]));
89
- return r.join();
90
- }, e.prototype.get = function(t) {
235
+ return t.prototype.set = function(r, e) {
236
+ var n = this.hash(r), s = this.map[n];
237
+ s || (this.map[n] = s = []), s.push([r, e]), this.length += 1;
238
+ }, t.prototype.hash = function(r) {
239
+ var e = [];
240
+ for (var n in r)
241
+ e.push(Object.prototype.toString.call(r[n]));
242
+ return e.join();
243
+ }, t.prototype.get = function(r) {
91
244
  if (this.length <= 180) {
92
- for (var r in this.map)
93
- for (var c = this.map[r], s = 0; s < c.length; s += 1) {
94
- var u = c[s];
95
- if (u[0] === t)
96
- return u[1];
245
+ for (var e in this.map)
246
+ for (var u = this.map[e], n = 0; n < u.length; n += 1) {
247
+ var s = u[n];
248
+ if (s[0] === r)
249
+ return s[1];
97
250
  }
98
251
  return;
99
252
  }
100
- var n = this.hash(t), c = this.map[n];
101
- if (c)
102
- for (var s = 0; s < c.length; s += 1) {
103
- var u = c[s];
104
- if (u[0] === t)
105
- return u[1];
253
+ var c = this.hash(r), u = this.map[c];
254
+ if (u)
255
+ for (var n = 0; n < u.length; n += 1) {
256
+ var s = u[n];
257
+ if (s[0] === r)
258
+ return s[1];
106
259
  }
107
- }, e;
108
- }(), P = /* @__PURE__ */ h(function(t) {
109
- return t != null && typeof t.clone == "function" ? t.clone() : m(t, !0);
260
+ }, t;
261
+ }(), J = /* @__PURE__ */ y(function(r) {
262
+ return r != null && typeof r.clone == "function" ? r.clone() : b(r, !0);
110
263
  });
111
- const b = P;
112
- var I = /* @__PURE__ */ w(function(t, r) {
113
- for (var s = {}, u = {}, n = 0, c = t.length; n < c; )
114
- u[t[n]] = 1, n += 1;
115
- for (var f in r)
116
- u.hasOwnProperty(f) || (s[f] = r[f]);
117
- return s;
264
+ const o = J;
265
+ var Q = /* @__PURE__ */ x(function(r, e) {
266
+ for (var n = {}, s = 0; s < r.length; )
267
+ r[s] in e && (n[r[s]] = e[r[s]]), s += 1;
268
+ return n;
118
269
  });
119
- const U = I;
120
- function A(e, t, r, s = []) {
121
- if (s.push(t), t === e)
122
- return s;
123
- if (Array.isArray(t[r]))
124
- for (let u of t[r]) {
125
- const n = A(e, u, r, s);
126
- if (n)
127
- return n;
270
+ const _ = Q;
271
+ function w(t, r, e, n = [], s = void 0) {
272
+ if (n.push(r), O(t, _(Object.keys(t), r)) || s === r)
273
+ return n;
274
+ if (Array.isArray(r[e]))
275
+ for (let c of r[e]) {
276
+ const u = w(t, c, e, n, r);
277
+ if (u)
278
+ return u;
128
279
  }
129
- return s.pop(), null;
280
+ return n.pop(), null;
130
281
  }
131
- class i extends Array {
282
+ class l extends Array {
132
283
  // 静态方法 ------
133
284
  /**
134
- * 将数组转化为 tree 数据
285
+ * 将数组转化为 tree 数据 迭代方法
135
286
  * @param array 需要转化的数组
136
287
  * @param nodeKey 节点主键
137
288
  * @param parentKey 父节点主键
138
289
  * @param childrenKey 子节点键名
290
+ * @param keepOrphansAsRoot 孤儿节点转为根节点
139
291
  * @returns
140
292
  */
141
- static array2Tree(t, r = "id", s = "parentId", u) {
142
- const n = {}, c = [];
143
- for (const f of t)
144
- n[f[r]] = { ...f, [u]: [] };
145
- for (const f of t) {
146
- const a = n[f[r]];
147
- f[s] ? n[f[s]] && n[f[s]][u].push(a) : c.push(a);
148
- }
149
- return i.treeFrom(c, u, r, s);
293
+ static array2Tree(r, e = "id", n = "parentId", s = "children", c = !1) {
294
+ let u = [];
295
+ const a = {}, i = {};
296
+ return r.forEach((f) => {
297
+ i[f[e]] = { ...f, [s]: [] }, L(f[n]) ? u.push(i[f[e]]) : i[f[n]] ? i[f[n]][s].push(i[f[e]]) : a[f[n]] ? a[f[n]].push(i[f[e]]) : a[f[n]] = [i[f[e]]], a[f[e]] && (i[f[e]][s] = a[f[e]], delete a[f[e]]);
298
+ }), c && Object.keys(a).length > 0 && Object.keys(a).forEach((f) => {
299
+ u = u.concat(a[f]);
300
+ }), l.treeFrom(u, e, n, s);
150
301
  }
151
302
  /**
152
- * 将树数据转换为 array 数组数据
303
+ * 将树数据转换为 array 数组数据 迭代方法
153
304
  * @param targetTree 需要转化的目标树
154
- * @param childrenKey 子节点键名
155
305
  * @param nodekey 节点主键
156
- * @param parentId 父节点主键
306
+ * @param parentKey 父节点主键
307
+ * @param childrenKey 子节点键名
157
308
  * @returns {T[]} 返回数组
158
309
  */
159
- static flatten2Array(t, r = "id", s = "parentId", u = "children", n = [], c = void 0) {
160
- return t.forEach((f) => {
161
- const a = U([u], f);
162
- c && (a[s] = c), n.push(a), Array.isArray(f[u]) && i.flatten2Array(f[u], r, s, u, n, f[r]);
163
- }), n;
310
+ static flatten2Array(r, e = "id", n = "parentId", s = "children") {
311
+ const c = [], u = [];
312
+ for (r.forEach((a) => {
313
+ u.push({ node: a, parent: void 0 });
314
+ }); u.length > 0; ) {
315
+ const { node: a, parent: i } = u.pop(), f = { ...a };
316
+ delete f[s], i && (f[n] = i), c.push(f), Array.isArray(a[s]) && a[s].forEach((h) => {
317
+ u.unshift({ node: h, parent: a[e] });
318
+ });
319
+ }
320
+ return c;
164
321
  }
165
322
  /**
166
323
  * 根据目标节点,或者目标节点条件,返回其到根节点的路径
@@ -170,14 +327,14 @@ class i extends Array {
170
327
  * @param pickFirst @default true 仅匹配首个结果
171
328
  * @returns {T[] | T[][]}
172
329
  */
173
- static path(t, r, s, u = !0) {
174
- const n = Array.isArray(r) ? { [s]: r } : r, c = u ? [i.find(t, r, s, u)].filter((f) => f != null) : i.find(t, r, s, u);
175
- return u ? c.map((f) => {
176
- const a = A(f, n, s, []);
177
- return a == null || a.shift(), a;
178
- })[0] : c.map((f) => {
179
- const a = A(f, n, s, []);
180
- return a == null || a.shift(), a;
330
+ static path(r, e, n, s = !0) {
331
+ const c = Array.isArray(e), u = c ? { [n]: e } : e, a = c ? e : [e], i = s ? [l.find(r, a, n, s)].filter((f) => f != null) : l.find(r, a, n, s);
332
+ return s ? i.map((f) => {
333
+ const h = w(f, u, n, []);
334
+ return c && (h == null || h.shift()), h;
335
+ })[0] ?? [] : i.map((f) => {
336
+ const h = w(f, u, n, []);
337
+ return c && (h == null || h.shift()), h;
181
338
  });
182
339
  }
183
340
  /**
@@ -188,23 +345,23 @@ class i extends Array {
188
345
  * @param prioritizeChildren @default true 子节点优先会先处理子节点情况,然后再将处理结果合并到父节点访问器中一并判断,false,则会优先处理父节点判定,再进行子节点处理
189
346
  * @returns {T[]}
190
347
  */
191
- static filter(t, r, s, u = !0) {
192
- return b(r).filter((n, c, f) => {
193
- if (u)
194
- return Array.isArray(n[s]) && (n[s] = i.filter(
195
- t,
196
- n[s],
197
- s,
198
- u
199
- )), t(n, c, f);
348
+ static filter(r, e, n, s = !0) {
349
+ return o(e).filter((u, a, i) => {
350
+ if (s)
351
+ return Array.isArray(u[n]) && (u[n] = l.filter(
352
+ r,
353
+ u[n],
354
+ n,
355
+ s
356
+ )), r(u, a, i);
200
357
  {
201
- const a = t(n, c, f);
202
- return Array.isArray(n[s]) && (n[s] = i.filter(
203
- t,
204
- n[s],
205
- s,
206
- u
207
- )), a;
358
+ const f = r(u, a, i);
359
+ return Array.isArray(u[n]) && (u[n] = l.filter(
360
+ r,
361
+ u[n],
362
+ n,
363
+ s
364
+ )), f;
208
365
  }
209
366
  });
210
367
  }
@@ -216,16 +373,15 @@ class i extends Array {
216
373
  * @param prioritizeChildren @default true 子节点优先会先处理子节点情况,然后再将处理结果合并到父节点访问器中一并判断,false,则会优先处理父节点判定,再进行子节点处理
217
374
  * @returns {T[]}
218
375
  */
219
- static map(t, r, s, u = !0) {
220
- const n = [].map.bind(b(r))((c, f, a) => {
221
- if (u)
222
- return Array.isArray(c[s]) && (c[s] = i.map(t, c[s], s)), t(c, f, a);
376
+ static map(r, e, n, s = !0) {
377
+ return [].map.bind(o(e))((u, a, i) => {
378
+ if (s)
379
+ return Array.isArray(u[n]) && (u[n] = l.map(r, u[n], n)), r(u, a, i);
223
380
  {
224
- const y = t(c, f, a);
225
- return Array.isArray(y[s]) && (y[s] = i.map(t, y[s], s)), y;
381
+ const f = r(u, a, i);
382
+ return Array.isArray(f[n]) && (f[n] = l.map(r, f[n], n)), f;
226
383
  }
227
384
  });
228
- return i.treeFrom(n, s);
229
385
  }
230
386
  /**
231
387
  * find node 的核心方法
@@ -235,39 +391,39 @@ class i extends Array {
235
391
  * @param pickFirst @default true 仅匹配首个结果
236
392
  * @returns {T|T[]|null} 返回查找结果集
237
393
  */
238
- static find(t, r, s, u = !0) {
239
- if (u) {
240
- for (let n in r) {
241
- if (typeof t == "function" ? t(r[n]) : r[n] == t)
242
- return r[n];
243
- const c = Array.isArray(r[n][s]) ? i.find(t, r[n][s], s) : null;
244
- if (c)
245
- return c;
394
+ static find(r, e, n, s = !0) {
395
+ if (s) {
396
+ for (let c in e) {
397
+ if (typeof r == "function" ? r(e[c]) : O(r, _(Object.keys(r), e[c])))
398
+ return e[c];
399
+ const u = e[c][n], a = Array.isArray(u) ? l.find(r, u, n) : null;
400
+ if (a)
401
+ return a;
246
402
  }
247
403
  return null;
248
404
  } else {
249
- let n = [];
250
- for (let c in r) {
251
- (typeof t == "function" ? t(r[c]) : r[c] == t) && n.push(r[c]);
252
- const f = Array.isArray(r[c][s]) ? i.find(t, r[c][s], s, u) : [];
253
- f && (n = n.concat(f));
405
+ let c = [];
406
+ for (let u in e) {
407
+ (typeof r == "function" ? r(e[u]) : O(r, _(Object.keys(r), e[u]))) && c.push(e[u]);
408
+ const a = e[u][n], i = Array.isArray(a) ? l.find(r, a, n, s) : [];
409
+ i && (c = c.concat(i));
254
410
  }
255
- return n;
411
+ return c;
256
412
  }
257
413
  }
258
414
  /**
259
415
  * 静态方法构造 VmoTree 实例
260
416
  * @param arr 来源数组(可空)
261
- * @param childrenKey 子节点键名
262
417
  * @param nodeKey 节点主键
263
418
  * @param parentKey 父节点主键
419
+ * @param childrenKey 子节点键名
264
420
  * @returns {VmoTree<T>}
265
421
  */
266
- static treeFrom(t, r, s, u) {
267
- return new i(t, {
268
- childrenKey: r,
269
- nodeKey: s,
270
- parentKey: u
422
+ static treeFrom(r, e, n, s) {
423
+ return new l(r, {
424
+ nodeKey: e,
425
+ parentKey: n,
426
+ childrenKey: s
271
427
  });
272
428
  }
273
429
  // 动态部分
@@ -277,22 +433,22 @@ class i extends Array {
277
433
  * @param option 树结构指定键名
278
434
  * @returns {VmoTree<T>}
279
435
  */
280
- constructor(t, r) {
281
- if (!t || Array.isArray(t)) {
282
- t ? super(...t) : super(), Object.defineProperty(this, "_childrenKey", {
283
- value: (r == null ? void 0 : r.childrenKey) ?? "children",
436
+ constructor(r, e) {
437
+ if (!r || Array.isArray(r)) {
438
+ r ? super(...r) : super(), Object.defineProperty(this, "_nodeKey", {
439
+ value: (e == null ? void 0 : e.nodeKey) ?? "id",
284
440
  writable: !0,
285
441
  enumerable: !1,
286
442
  // 设置为不可枚举
287
443
  configurable: !0
288
- }), Object.defineProperty(this, "_nodeKey", {
289
- value: (r == null ? void 0 : r.nodeKey) ?? "id",
444
+ }), Object.defineProperty(this, "_parentKey", {
445
+ value: (e == null ? void 0 : e.parentKey) ?? "parentId",
290
446
  writable: !0,
291
447
  enumerable: !1,
292
448
  // 设置为不可枚举
293
449
  configurable: !0
294
- }), Object.defineProperty(this, "_parentKey", {
295
- value: (r == null ? void 0 : r.parentKey) ?? "parentId",
450
+ }), Object.defineProperty(this, "_childrenKey", {
451
+ value: (e == null ? void 0 : e.childrenKey) ?? "children",
296
452
  writable: !0,
297
453
  enumerable: !1,
298
454
  // 设置为不可枚举
@@ -310,32 +466,39 @@ class i extends Array {
310
466
  * @param option 查找配置 {childrenKey:string,pickFirst:boolean=true}
311
467
  * @returns {T| T[]| null}
312
468
  */
313
- find(t, r) {
314
- return i.find(t, this, (r == null ? void 0 : r.childrenKey) ?? this._childrenKey, r == null ? void 0 : r.pickFirst);
469
+ find(r, e) {
470
+ return l.find(r, this, (e == null ? void 0 : e.childrenKey) ?? this._childrenKey, e == null ? void 0 : e.pickFirst);
315
471
  }
316
472
  /**
317
473
  * 实例映射方法
318
474
  * @param visit 访问函数
319
475
  * @param {
476
+ * @param nodeKey 节点主键
477
+ * @param parentKey 父节点主键
320
478
  * @param childrenKey 子节点主键
321
479
  * @param prioritizeChildren 子节点优先
322
480
  * }option 操作配置
323
481
  * @returns {T[]}
324
482
  */
325
- map(t, r) {
326
- return i.map(
327
- t,
328
- this,
329
- (r == null ? void 0 : r.childrenKey) ?? this._childrenKey,
330
- r == null ? void 0 : r.prioritizeChildren
483
+ map(r, e) {
484
+ return l.treeFrom(
485
+ l.map(r, this, (e == null ? void 0 : e.childrenKey) ?? this._childrenKey, e == null ? void 0 : e.prioritizeChildren),
486
+ this._nodeKey,
487
+ this._parentKey,
488
+ (e == null ? void 0 : e.childrenKey) ?? this._childrenKey
331
489
  );
332
490
  }
333
- filter(t, r) {
334
- return i.filter(
335
- t,
336
- this,
337
- (r == null ? void 0 : r.childrenKey) ?? this._childrenKey,
338
- r == null ? void 0 : r.prioritizeChildren
491
+ filter(r, e) {
492
+ return l.treeFrom(
493
+ l.filter(
494
+ r,
495
+ this,
496
+ (e == null ? void 0 : e.childrenKey) ?? this._childrenKey,
497
+ e == null ? void 0 : e.prioritizeChildren
498
+ ),
499
+ this._nodeKey,
500
+ this._parentKey,
501
+ (e == null ? void 0 : e.childrenKey) ?? this._chidlrenKey
339
502
  );
340
503
  }
341
504
  /**
@@ -344,29 +507,29 @@ class i extends Array {
344
507
  * @param option 查找配置 {childrenKey:string,pickFirst:boolean=true}
345
508
  * @returns {S[]| S[][]}
346
509
  */
347
- path(t, r) {
348
- return i.path(t, this, (r == null ? void 0 : r.childrenKey) ?? this._childrenKey, r == null ? void 0 : r.pickFirst);
510
+ path(r, e) {
511
+ return l.path(r, this, (e == null ? void 0 : e.childrenKey) ?? this._childrenKey, e == null ? void 0 : e.pickFirst);
349
512
  }
350
513
  /**
351
514
  * 平展树数据结构为数组
352
515
  * @param option // 树结构
353
516
  * @returns {S[]}
354
517
  */
355
- flatten(t) {
356
- return i.flatten2Array(
518
+ flatten(r) {
519
+ return l.flatten2Array(
357
520
  this,
358
- (t == null ? void 0 : t.nodeKey) ?? this._nodeKey,
359
- (t == null ? void 0 : t.parentKey) ?? this._parentKey,
360
- (t == null ? void 0 : t.childrenKey) ?? this._childrenKey
521
+ (r == null ? void 0 : r.nodeKey) ?? this._nodeKey,
522
+ (r == null ? void 0 : r.parentKey) ?? this._parentKey,
523
+ (r == null ? void 0 : r.childrenKey) ?? this._childrenKey
361
524
  );
362
525
  }
363
526
  /**
364
527
  * 设置树的配置
365
528
  * @param option 树结构配置
366
529
  */
367
- setConfig(t) {
368
- const r = this;
369
- r._nodeKey = (t == null ? void 0 : t.nodeKey) ?? r._nodeKey ?? "id", r._parentKey = (t == null ? void 0 : t.parentKey) ?? r._parentKey ?? "parentId", r._childrenKey = (t == null ? void 0 : t.childrenKey) ?? r._childrenKey ?? "children";
530
+ setConfig(r) {
531
+ const e = this;
532
+ e._nodeKey = (r == null ? void 0 : r.nodeKey) ?? e._nodeKey ?? "id", e._parentKey = (r == null ? void 0 : r.parentKey) ?? e._parentKey ?? "parentId", e._childrenKey = (r == null ? void 0 : r.childrenKey) ?? e._childrenKey ?? "children";
370
533
  }
371
534
  /**
372
535
  * 获取树结构配置
@@ -381,5 +544,5 @@ class i extends Array {
381
544
  }
382
545
  }
383
546
  export {
384
- i as VmoTree
547
+ l as VmoTree
385
548
  };
@@ -1 +1 @@
1
- (function(l,y){typeof exports=="object"&&typeof module<"u"?y(exports):typeof define=="function"&&define.amd?define(["exports"],y):(l=typeof globalThis<"u"?globalThis:l||self,y(l["vmo-tree"]={}))})(this,function(l){"use strict";const y={"@@functional/placeholder":!0};function h(s){return s===y}function A(s){return function t(r){return arguments.length===0||h(r)?t:s.apply(this,arguments)}}function j(s){return function t(r,e){switch(arguments.length){case 0:return t;case 1:return h(r)?t:A(function(u){return s(r,u)});default:return h(r)&&h(e)?t:h(r)?A(function(u){return s(u,e)}):h(e)?A(function(u){return s(r,u)}):s(r,e)}}}var w=A(function(t){return t===null?"Null":t===void 0?"Undefined":Object.prototype.toString.call(t).slice(8,-1)});const g=w;function v(s){return new RegExp(s.source,s.flags?s.flags:(s.global?"g":"")+(s.ignoreCase?"i":"")+(s.multiline?"m":"")+(s.sticky?"y":"")+(s.unicode?"u":"")+(s.dotAll?"s":""))}function m(s,t,r){if(r||(r=new P),_(s))return s;var e=function(n){var f=r.get(s);if(f)return f;r.set(s,n);for(var c in s)Object.prototype.hasOwnProperty.call(s,c)&&(n[c]=t?m(s[c],!0,r):s[c]);return n};switch(g(s)){case"Object":return e(Object.create(Object.getPrototypeOf(s)));case"Array":return e(Array(s.length));case"Date":return new Date(s.valueOf());case"RegExp":return v(s);case"Int8Array":case"Uint8Array":case"Uint8ClampedArray":case"Int16Array":case"Uint16Array":case"Int32Array":case"Uint32Array":case"Float32Array":case"Float64Array":case"BigInt64Array":case"BigUint64Array":return s.slice();default:return s}}function _(s){var t=typeof s;return s==null||t!="object"&&t!="function"}var P=function(){function s(){this.map={},this.length=0}return s.prototype.set=function(t,r){var e=this.hash(t),u=this.map[e];u||(this.map[e]=u=[]),u.push([t,r]),this.length+=1},s.prototype.hash=function(t){var r=[];for(var e in t)r.push(Object.prototype.toString.call(t[e]));return r.join()},s.prototype.get=function(t){if(this.length<=180){for(var r in this.map)for(var f=this.map[r],e=0;e<f.length;e+=1){var u=f[e];if(u[0]===t)return u[1]}return}var n=this.hash(t),f=this.map[n];if(f)for(var e=0;e<f.length;e+=1){var u=f[e];if(u[0]===t)return u[1]}},s}(),I=A(function(t){return t!=null&&typeof t.clone=="function"?t.clone():m(t,!0)});const O=I;var x=j(function(t,r){for(var e={},u={},n=0,f=t.length;n<f;)u[t[n]]=1,n+=1;for(var c in r)u.hasOwnProperty(c)||(e[c]=r[c]);return e});const U=x;function d(s,t,r,e=[]){if(e.push(t),t===s)return e;if(Array.isArray(t[r]))for(let u of t[r]){const n=d(s,u,r,e);if(n)return n}return e.pop(),null}class i extends Array{static array2Tree(t,r="id",e="parentId",u){const n={},f=[];for(const c of t)n[c[r]]={...c,[u]:[]};for(const c of t){const a=n[c[r]];c[e]?n[c[e]]&&n[c[e]][u].push(a):f.push(a)}return i.treeFrom(f,u,r,e)}static flatten2Array(t,r="id",e="parentId",u="children",n=[],f=void 0){return t.forEach(c=>{const a=U([u],c);f&&(a[e]=f),n.push(a),Array.isArray(c[u])&&i.flatten2Array(c[u],r,e,u,n,c[r])}),n}static path(t,r,e,u=!0){const n=Array.isArray(r)?{[e]:r}:r,f=u?[i.find(t,r,e,u)].filter(c=>c!=null):i.find(t,r,e,u);return u?f.map(c=>{const a=d(c,n,e,[]);return a==null||a.shift(),a})[0]:f.map(c=>{const a=d(c,n,e,[]);return a==null||a.shift(),a})}static filter(t,r,e,u=!0){return O(r).filter((n,f,c)=>{if(u)return Array.isArray(n[e])&&(n[e]=i.filter(t,n[e],e,u)),t(n,f,c);{const a=t(n,f,c);return Array.isArray(n[e])&&(n[e]=i.filter(t,n[e],e,u)),a}})}static map(t,r,e,u=!0){const n=[].map.bind(O(r))((f,c,a)=>{if(u)return Array.isArray(f[e])&&(f[e]=i.map(t,f[e],e)),t(f,c,a);{const b=t(f,c,a);return Array.isArray(b[e])&&(b[e]=i.map(t,b[e],e)),b}});return i.treeFrom(n,e)}static find(t,r,e,u=!0){if(u){for(let n in r){if(typeof t=="function"?t(r[n]):r[n]==t)return r[n];const f=Array.isArray(r[n][e])?i.find(t,r[n][e],e):null;if(f)return f}return null}else{let n=[];for(let f in r){(typeof t=="function"?t(r[f]):r[f]==t)&&n.push(r[f]);const c=Array.isArray(r[f][e])?i.find(t,r[f][e],e,u):[];c&&(n=n.concat(c))}return n}}static treeFrom(t,r,e,u){return new i(t,{childrenKey:r,nodeKey:e,parentKey:u})}constructor(t,r){if(!t||Array.isArray(t)){t?super(...t):super(),Object.defineProperty(this,"_childrenKey",{value:(r==null?void 0:r.childrenKey)??"children",writable:!0,enumerable:!1,configurable:!0}),Object.defineProperty(this,"_nodeKey",{value:(r==null?void 0:r.nodeKey)??"id",writable:!0,enumerable:!1,configurable:!0}),Object.defineProperty(this,"_parentKey",{value:(r==null?void 0:r.parentKey)??"parentId",writable:!0,enumerable:!1,configurable:!0});return}else throw Error('The constructor of VmoTree requires parameters " new VmoTree(array?: T[], childrenKey?: string)", the current type is incorrect! ')}find(t,r){return i.find(t,this,(r==null?void 0:r.childrenKey)??this._childrenKey,r==null?void 0:r.pickFirst)}map(t,r){return i.map(t,this,(r==null?void 0:r.childrenKey)??this._childrenKey,r==null?void 0:r.prioritizeChildren)}filter(t,r){return i.filter(t,this,(r==null?void 0:r.childrenKey)??this._childrenKey,r==null?void 0:r.prioritizeChildren)}path(t,r){return i.path(t,this,(r==null?void 0:r.childrenKey)??this._childrenKey,r==null?void 0:r.pickFirst)}flatten(t){return i.flatten2Array(this,(t==null?void 0:t.nodeKey)??this._nodeKey,(t==null?void 0:t.parentKey)??this._parentKey,(t==null?void 0:t.childrenKey)??this._childrenKey)}setConfig(t){const r=this;r._nodeKey=(t==null?void 0:t.nodeKey)??r._nodeKey??"id",r._parentKey=(t==null?void 0:t.parentKey)??r._parentKey??"parentId",r._childrenKey=(t==null?void 0:t.childrenKey)??r._childrenKey??"children"}getConfig(){return{nodeKey:this._nodeKey,parentKey:this._parentKey,childrenKey:this._childrenKey}}}l.VmoTree=i,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})});
1
+ (function(A,v){typeof exports=="object"&&typeof module<"u"?v(exports):typeof define=="function"&&define.amd?define(["exports"],v):(A=typeof globalThis<"u"?globalThis:A||self,v(A["vmo-tree"]={}))})(this,function(A){"use strict";const v={"@@functional/placeholder":!0};function g(t){return t===v}function y(t){return function r(e){return arguments.length===0||g(e)?r:t.apply(this,arguments)}}function p(t){return function r(e,n){switch(arguments.length){case 0:return r;case 1:return g(e)?r:y(function(s){return t(e,s)});default:return g(e)&&g(n)?r:g(e)?y(function(s){return t(s,n)}):g(n)?y(function(s){return t(e,s)}):t(e,n)}}}function q(t){for(var r=[],e;!(e=t.next()).done;)r.push(e.value);return r}function E(t,r,e){for(var n=0,s=e.length;n<s;){if(t(r,e[n]))return!0;n+=1}return!1}function C(t){var r=String(t).match(/^function (\w*)/);return r==null?"":r[1]}function m(t,r){return Object.prototype.hasOwnProperty.call(r,t)}function F(t,r){return t===r?t!==0||1/t===1/r:t!==t&&r!==r}const O=typeof Object.is=="function"?Object.is:F;var I=Object.prototype.toString,R=function(){return I.call(arguments)==="[object Arguments]"?function(r){return I.call(r)==="[object Arguments]"}:function(r){return m("callee",r)}}();const $=R;var M=!{toString:null}.propertyIsEnumerable("toString"),S=["constructor","valueOf","isPrototypeOf","toString","propertyIsEnumerable","hasOwnProperty","toLocaleString"],b=function(){return arguments.propertyIsEnumerable("length")}(),B=function(r,e){for(var n=0;n<r.length;){if(r[n]===e)return!0;n+=1}return!1},z=y(typeof Object.keys=="function"&&!b?function(r){return Object(r)!==r?[]:Object.keys(r)}:function(r){if(Object(r)!==r)return[];var e,n,s=[],c=b&&$(r);for(e in r)m(e,r)&&(!c||e!=="length")&&(s[s.length]=e);if(M)for(n=S.length-1;n>=0;)e=S[n],m(e,r)&&!B(s,e)&&(s[s.length]=e),n-=1;return s});const x=z;var D=y(function(r){return r===null?"Null":r===void 0?"Undefined":Object.prototype.toString.call(r).slice(8,-1)});const d=D;function k(t,r,e,n){var s=q(t),c=q(r);function u(a,f){return _(a,f,e.slice(),n.slice())}return!E(function(a,f){return!E(u,f,a)},c,s)}function _(t,r,e,n){if(O(t,r))return!0;var s=d(t);if(s!==d(r))return!1;if(typeof t["fantasy-land/equals"]=="function"||typeof r["fantasy-land/equals"]=="function")return typeof t["fantasy-land/equals"]=="function"&&t["fantasy-land/equals"](r)&&typeof r["fantasy-land/equals"]=="function"&&r["fantasy-land/equals"](t);if(typeof t.equals=="function"||typeof r.equals=="function")return typeof t.equals=="function"&&t.equals(r)&&typeof r.equals=="function"&&r.equals(t);switch(s){case"Arguments":case"Array":case"Object":if(typeof t.constructor=="function"&&C(t.constructor)==="Promise")return t===r;break;case"Boolean":case"Number":case"String":if(!(typeof t==typeof r&&O(t.valueOf(),r.valueOf())))return!1;break;case"Date":if(!O(t.valueOf(),r.valueOf()))return!1;break;case"Error":return t.name===r.name&&t.message===r.message;case"RegExp":if(!(t.source===r.source&&t.global===r.global&&t.ignoreCase===r.ignoreCase&&t.multiline===r.multiline&&t.sticky===r.sticky&&t.unicode===r.unicode))return!1;break}for(var c=e.length-1;c>=0;){if(e[c]===t)return n[c]===r;c-=1}switch(s){case"Map":return t.size!==r.size?!1:k(t.entries(),r.entries(),e.concat([t]),n.concat([r]));case"Set":return t.size!==r.size?!1:k(t.values(),r.values(),e.concat([t]),n.concat([r]));case"Arguments":case"Array":case"Object":case"Boolean":case"Number":case"String":case"Date":case"Error":case"RegExp":case"Int8Array":case"Uint8Array":case"Uint8ClampedArray":case"Int16Array":case"Uint16Array":case"Int32Array":case"Uint32Array":case"Float32Array":case"Float64Array":case"ArrayBuffer":break;default:return!1}var u=x(t);if(u.length!==x(r).length)return!1;var a=e.concat([t]),f=n.concat([r]);for(c=u.length-1;c>=0;){var i=u[c];if(!(m(i,r)&&_(r[i],t[i],a,f)))return!1;c-=1}return!0}var K=p(function(r,e){return _(r,e,[],[])});const o=K;var L=y(function(r){return r==null});const W=L;function G(t){return new RegExp(t.source,t.flags?t.flags:(t.global?"g":"")+(t.ignoreCase?"i":"")+(t.multiline?"m":"")+(t.sticky?"y":"")+(t.unicode?"u":"")+(t.dotAll?"s":""))}function P(t,r,e){if(e||(e=new J),H(t))return t;var n=function(c){var u=e.get(t);if(u)return u;e.set(t,c);for(var a in t)Object.prototype.hasOwnProperty.call(t,a)&&(c[a]=r?P(t[a],!0,e):t[a]);return c};switch(d(t)){case"Object":return n(Object.create(Object.getPrototypeOf(t)));case"Array":return n(Array(t.length));case"Date":return new Date(t.valueOf());case"RegExp":return G(t);case"Int8Array":case"Uint8Array":case"Uint8ClampedArray":case"Int16Array":case"Uint16Array":case"Int32Array":case"Uint32Array":case"Float32Array":case"Float64Array":case"BigInt64Array":case"BigUint64Array":return t.slice();default:return t}}function H(t){var r=typeof t;return t==null||r!="object"&&r!="function"}var J=function(){function t(){this.map={},this.length=0}return t.prototype.set=function(r,e){var n=this.hash(r),s=this.map[n];s||(this.map[n]=s=[]),s.push([r,e]),this.length+=1},t.prototype.hash=function(r){var e=[];for(var n in r)e.push(Object.prototype.toString.call(r[n]));return e.join()},t.prototype.get=function(r){if(this.length<=180){for(var e in this.map)for(var u=this.map[e],n=0;n<u.length;n+=1){var s=u[n];if(s[0]===r)return s[1]}return}var c=this.hash(r),u=this.map[c];if(u)for(var n=0;n<u.length;n+=1){var s=u[n];if(s[0]===r)return s[1]}},t}(),Q=y(function(r){return r!=null&&typeof r.clone=="function"?r.clone():P(r,!0)});const U=Q;var X=p(function(r,e){for(var n={},s=0;s<r.length;)r[s]in e&&(n[r[s]]=e[r[s]]),s+=1;return n});const j=X;function w(t,r,e,n=[],s=void 0){if(n.push(r),o(t,j(Object.keys(t),r))||s===r)return n;if(Array.isArray(r[e]))for(let c of r[e]){const u=w(t,c,e,n,r);if(u)return u}return n.pop(),null}class l extends Array{static array2Tree(r,e="id",n="parentId",s="children",c=!1){let u=[];const a={},f={};return r.forEach(i=>{f[i[e]]={...i,[s]:[]},W(i[n])?u.push(f[i[e]]):f[i[n]]?f[i[n]][s].push(f[i[e]]):a[i[n]]?a[i[n]].push(f[i[e]]):a[i[n]]=[f[i[e]]],a[i[e]]&&(f[i[e]][s]=a[i[e]],delete a[i[e]])}),c&&Object.keys(a).length>0&&Object.keys(a).forEach(i=>{u=u.concat(a[i])}),l.treeFrom(u,e,n,s)}static flatten2Array(r,e="id",n="parentId",s="children"){const c=[],u=[];for(r.forEach(a=>{u.push({node:a,parent:void 0})});u.length>0;){const{node:a,parent:f}=u.pop(),i={...a};delete i[s],f&&(i[n]=f),c.push(i),Array.isArray(a[s])&&a[s].forEach(h=>{u.unshift({node:h,parent:a[e]})})}return c}static path(r,e,n,s=!0){const c=Array.isArray(e),u=c?{[n]:e}:e,a=c?e:[e],f=s?[l.find(r,a,n,s)].filter(i=>i!=null):l.find(r,a,n,s);return s?f.map(i=>{const h=w(i,u,n,[]);return c&&(h==null||h.shift()),h})[0]??[]:f.map(i=>{const h=w(i,u,n,[]);return c&&(h==null||h.shift()),h})}static filter(r,e,n,s=!0){return U(e).filter((u,a,f)=>{if(s)return Array.isArray(u[n])&&(u[n]=l.filter(r,u[n],n,s)),r(u,a,f);{const i=r(u,a,f);return Array.isArray(u[n])&&(u[n]=l.filter(r,u[n],n,s)),i}})}static map(r,e,n,s=!0){return[].map.bind(U(e))((u,a,f)=>{if(s)return Array.isArray(u[n])&&(u[n]=l.map(r,u[n],n)),r(u,a,f);{const i=r(u,a,f);return Array.isArray(i[n])&&(i[n]=l.map(r,i[n],n)),i}})}static find(r,e,n,s=!0){if(s){for(let c in e){if(typeof r=="function"?r(e[c]):o(r,j(Object.keys(r),e[c])))return e[c];const u=e[c][n],a=Array.isArray(u)?l.find(r,u,n):null;if(a)return a}return null}else{let c=[];for(let u in e){(typeof r=="function"?r(e[u]):o(r,j(Object.keys(r),e[u])))&&c.push(e[u]);const a=e[u][n],f=Array.isArray(a)?l.find(r,a,n,s):[];f&&(c=c.concat(f))}return c}}static treeFrom(r,e,n,s){return new l(r,{nodeKey:e,parentKey:n,childrenKey:s})}constructor(r,e){if(!r||Array.isArray(r)){r?super(...r):super(),Object.defineProperty(this,"_nodeKey",{value:(e==null?void 0:e.nodeKey)??"id",writable:!0,enumerable:!1,configurable:!0}),Object.defineProperty(this,"_parentKey",{value:(e==null?void 0:e.parentKey)??"parentId",writable:!0,enumerable:!1,configurable:!0}),Object.defineProperty(this,"_childrenKey",{value:(e==null?void 0:e.childrenKey)??"children",writable:!0,enumerable:!1,configurable:!0});return}else throw Error('The constructor of VmoTree requires parameters " new VmoTree(array?: T[], childrenKey?: string)", the current type is incorrect! ')}find(r,e){return l.find(r,this,(e==null?void 0:e.childrenKey)??this._childrenKey,e==null?void 0:e.pickFirst)}map(r,e){return l.treeFrom(l.map(r,this,(e==null?void 0:e.childrenKey)??this._childrenKey,e==null?void 0:e.prioritizeChildren),this._nodeKey,this._parentKey,(e==null?void 0:e.childrenKey)??this._childrenKey)}filter(r,e){return l.treeFrom(l.filter(r,this,(e==null?void 0:e.childrenKey)??this._childrenKey,e==null?void 0:e.prioritizeChildren),this._nodeKey,this._parentKey,(e==null?void 0:e.childrenKey)??this._chidlrenKey)}path(r,e){return l.path(r,this,(e==null?void 0:e.childrenKey)??this._childrenKey,e==null?void 0:e.pickFirst)}flatten(r){return l.flatten2Array(this,(r==null?void 0:r.nodeKey)??this._nodeKey,(r==null?void 0:r.parentKey)??this._parentKey,(r==null?void 0:r.childrenKey)??this._childrenKey)}setConfig(r){const e=this;e._nodeKey=(r==null?void 0:r.nodeKey)??e._nodeKey??"id",e._parentKey=(r==null?void 0:r.parentKey)??e._parentKey??"parentId",e._childrenKey=(r==null?void 0:r.childrenKey)??e._childrenKey??"children"}getConfig(){return{nodeKey:this._nodeKey,parentKey:this._parentKey,childrenKey:this._childrenKey}}}A.VmoTree=l,Object.defineProperty(A,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vmo-tree",
3
- "version": "0.0.0-beta.3",
3
+ "version": "0.0.1",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "description": "A lightweight JavaScript library for creating and maintaining tree structures, providing easy methods for manipulation, traversal, and search.",
@@ -19,24 +19,27 @@
19
19
  "dist"
20
20
  ],
21
21
  "scripts": {
22
+ "coverage": "vitest run --coverage",
23
+ "test": "vitest",
22
24
  "dev": "vite",
23
25
  "build": "vue-tsc && vite build",
24
26
  "preview": "vite preview"
25
27
  },
26
- "peerDependencies": {
27
- "ramda": "^0.30.0",
28
- "vue": "^3.3.4"
29
- },
30
28
  "devDependencies": {
31
29
  "@types/node": "^20.10.4",
32
30
  "@types/ramda": "^0.30.2",
33
31
  "@vitejs/plugin-vue": "^4.5.0",
32
+ "@vitest/coverage-istanbul": "^2.1.4",
33
+ "@vitest/coverage-v8": "^2.1.4",
34
34
  "autoprefixer": "^10.4.16",
35
+ "jsdom": "^25.0.1",
35
36
  "postcss": "^8.4.32",
37
+ "ramda": "^0.30.0",
36
38
  "tailwindcss": "^3.3.6",
37
39
  "typescript": "^5.2.2",
38
40
  "vite": "^5.0.0",
39
41
  "vite-plugin-dts": "^3.6.4",
42
+ "vitest": "^2.1.4",
40
43
  "vue-tsc": "^1.8.22"
41
44
  }
42
45
  }