@unicom-cloud/utils 0.1.22 → 0.1.23
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/package.json +1 -1
- package/tree/search/index.js +36 -40
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@unicom-cloud/utils","version":"0.1.
|
|
1
|
+
{"name":"@unicom-cloud/utils","version":"0.1.23","dependencies":{},"peerDependencies":{"lodash":"^4.17.21"},"main":"./index.js","type":"module","types":"types/index.d.ts","publishConfig":{"registry":"https://registry.npmjs.org/","access":"public"}}
|
package/tree/search/index.js
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
import "../../is/index.js";
|
|
2
2
|
import S from "lodash/isArray";
|
|
3
3
|
import B from "lodash/isObjectLike";
|
|
4
|
-
import
|
|
4
|
+
import p from "lodash/isString";
|
|
5
5
|
import C from "lodash/isNumber";
|
|
6
6
|
import K from "lodash/isFunction";
|
|
7
7
|
import L from "lodash/isRegExp";
|
|
8
|
-
function j(
|
|
9
|
-
return
|
|
8
|
+
function j(i, a, m, w) {
|
|
9
|
+
return a && i ? { shouldExit: !0, newMatch: !0 } : m && !i ? { shouldExit: !0, newMatch: !1 } : { shouldExit: !1, newMatch: w || i };
|
|
10
10
|
}
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
function Q(o, f, h = {}) {
|
|
16
|
-
if (!S(o)) throw new TypeError("参数 treeData 必须是一个数组");
|
|
17
|
-
if (!B(f)) throw new TypeError("参数 searchCondition 必须是一个对象");
|
|
11
|
+
function P(i, a, m = {}) {
|
|
12
|
+
if (!S(i)) throw new TypeError("参数 treeData 必须是一个数组");
|
|
13
|
+
if (!B(a)) throw new TypeError("参数 searchCondition 必须是一个对象");
|
|
18
14
|
const w = {
|
|
19
15
|
strategy: "BFS",
|
|
20
16
|
childrenKey: "children",
|
|
@@ -27,11 +23,11 @@ function Q(o, f, h = {}) {
|
|
|
27
23
|
returnTree: !1,
|
|
28
24
|
returnTreeMaxDepth: 0,
|
|
29
25
|
// 只在 returnTree 为 true 时才生效
|
|
30
|
-
...
|
|
26
|
+
...m
|
|
31
27
|
// 用户配置覆盖默认值
|
|
32
28
|
}, {
|
|
33
29
|
strategy: E,
|
|
34
|
-
childrenKey:
|
|
30
|
+
childrenKey: u,
|
|
35
31
|
strict: R,
|
|
36
32
|
fuzzyMatch: b,
|
|
37
33
|
returnAllMatches: M,
|
|
@@ -39,18 +35,18 @@ function Q(o, f, h = {}) {
|
|
|
39
35
|
allowEmptyString: z,
|
|
40
36
|
matchMode: T,
|
|
41
37
|
returnTree: g,
|
|
42
|
-
returnTreeMaxDepth:
|
|
38
|
+
returnTreeMaxDepth: d
|
|
43
39
|
} = w, D = T === "OR", x = T === "AND", y = E === "BFS";
|
|
44
40
|
if (!(y || E === "DFS")) throw new Error('配置 strategy 必须是 "BFS" 或 "DFS"');
|
|
45
41
|
if (!(D || x)) throw new Error('配置 matchMode 必须是 "AND" 或 "OR"');
|
|
46
|
-
if (!
|
|
42
|
+
if (!p(u)) throw new TypeError("配置 childrenKey 必须是字符串");
|
|
47
43
|
if (g) {
|
|
48
44
|
if (!M) throw new Error(
|
|
49
45
|
"当 returnTree 为 true 时,returnAllMatches 也必须为 true"
|
|
50
46
|
);
|
|
51
|
-
if (!(C(
|
|
47
|
+
if (!(C(d) && d > -1)) throw new TypeError("配置 returnTreeMaxDepth 必须是一个非负整数");
|
|
52
48
|
}
|
|
53
|
-
const
|
|
49
|
+
const h = [...i], A = [], F = Object.entries(a);
|
|
54
50
|
if (F.length) {
|
|
55
51
|
const N = (r) => {
|
|
56
52
|
let s = !1;
|
|
@@ -58,62 +54,62 @@ function Q(o, f, h = {}) {
|
|
|
58
54
|
const t = r[n];
|
|
59
55
|
if (e === "" && !z)
|
|
60
56
|
continue;
|
|
61
|
-
let
|
|
57
|
+
let o = !1;
|
|
62
58
|
if (K(e))
|
|
63
|
-
|
|
64
|
-
else if (L(e) &&
|
|
65
|
-
|
|
66
|
-
else if (b &&
|
|
59
|
+
o = !!e(t, r);
|
|
60
|
+
else if (L(e) && p(t))
|
|
61
|
+
o = t ? e.test(t) : !1;
|
|
62
|
+
else if (b && p(e) && p(t)) {
|
|
67
63
|
let l = e, c = t;
|
|
68
|
-
v || (l = e.toLowerCase(), c = t.toLowerCase()),
|
|
64
|
+
v || (l = e.toLowerCase(), c = t.toLowerCase()), o = c.includes(l);
|
|
69
65
|
} else
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
if (
|
|
73
|
-
return
|
|
74
|
-
s =
|
|
66
|
+
o = R ? t === e : t == e;
|
|
67
|
+
const f = j(o, D, x, s);
|
|
68
|
+
if (f.shouldExit)
|
|
69
|
+
return f.newMatch;
|
|
70
|
+
s = f.newMatch;
|
|
75
71
|
}
|
|
76
72
|
return s;
|
|
77
73
|
};
|
|
78
74
|
if (g) {
|
|
79
75
|
const r = (n, e = 1) => {
|
|
80
|
-
const t =
|
|
81
|
-
if (S(
|
|
82
|
-
for (const c of
|
|
76
|
+
const t = d > 0 && e > d, o = !t && N(n), f = t ? void 0 : n[u], l = [];
|
|
77
|
+
if (S(f))
|
|
78
|
+
for (const c of f) {
|
|
83
79
|
const O = r(c, e + 1);
|
|
84
80
|
O && l.push(O);
|
|
85
81
|
}
|
|
86
|
-
if (
|
|
87
|
-
const c =
|
|
88
|
-
return l.length > 0 && (c[
|
|
82
|
+
if (o || l.length > 0) {
|
|
83
|
+
const c = { ...n };
|
|
84
|
+
return l.length > 0 && (c[u] = l), c;
|
|
89
85
|
}
|
|
90
86
|
return null;
|
|
91
87
|
}, s = [];
|
|
92
|
-
for (const n of
|
|
88
|
+
for (const n of i) {
|
|
93
89
|
const e = r(n, 1);
|
|
94
90
|
e && s.push(e);
|
|
95
91
|
}
|
|
96
92
|
return s;
|
|
97
93
|
} else
|
|
98
|
-
for (;
|
|
99
|
-
const r = y ?
|
|
94
|
+
for (; h.length > 0; ) {
|
|
95
|
+
const r = y ? h.shift() : h.pop();
|
|
100
96
|
if (!r) continue;
|
|
101
97
|
if (N(r))
|
|
102
98
|
if (M)
|
|
103
99
|
A.push(r);
|
|
104
100
|
else
|
|
105
101
|
return r;
|
|
106
|
-
const s = r[
|
|
102
|
+
const s = r[u];
|
|
107
103
|
if (S(s))
|
|
108
104
|
if (y)
|
|
109
|
-
|
|
105
|
+
h.push(...s);
|
|
110
106
|
else
|
|
111
107
|
for (let n = s.length - 1; n >= 0; n--)
|
|
112
|
-
|
|
108
|
+
h.push(s[n]);
|
|
113
109
|
}
|
|
114
110
|
}
|
|
115
111
|
return M ? A : null;
|
|
116
112
|
}
|
|
117
113
|
export {
|
|
118
|
-
|
|
114
|
+
P as default
|
|
119
115
|
};
|