@unicom-cloud/utils 0.1.21 → 0.1.22
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 +98 -129
- package/types/is/index.d.ts +16 -16
- package/types/tree/search/index.d.ts +6 -18
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@unicom-cloud/utils","version":"0.1.
|
|
1
|
+
{"name":"@unicom-cloud/utils","version":"0.1.22","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,80 +1,21 @@
|
|
|
1
1
|
import "../../is/index.js";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import S from "lodash/isArray";
|
|
3
|
+
import B from "lodash/isObjectLike";
|
|
4
|
+
import m from "lodash/isString";
|
|
5
|
+
import C from "lodash/isNumber";
|
|
6
|
+
import K from "lodash/isFunction";
|
|
7
|
+
import L from "lodash/isRegExp";
|
|
8
|
+
function j(o, f, h, w) {
|
|
9
|
+
return f && o ? { shouldExit: !0, newMatch: !0 } : h && !o ? { shouldExit: !0, newMatch: !1 } : { shouldExit: !1, newMatch: w || o };
|
|
8
10
|
}
|
|
9
|
-
function
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const t = Object.entries(r), m = {};
|
|
13
|
-
for (const [d, s] of t) {
|
|
14
|
-
const a = c[d];
|
|
15
|
-
if (s === "")
|
|
16
|
-
if (y)
|
|
17
|
-
m[d] = a === "";
|
|
18
|
-
else {
|
|
19
|
-
m[d] = !1;
|
|
20
|
-
continue;
|
|
21
|
-
}
|
|
22
|
-
let o = !1;
|
|
23
|
-
try {
|
|
24
|
-
if (s instanceof RegExp)
|
|
25
|
-
o = F(a) ? s.test(a) : !1;
|
|
26
|
-
else if (x(s))
|
|
27
|
-
o = !!s(a);
|
|
28
|
-
else if (l && F(s)) {
|
|
29
|
-
const h = String(a), D = u ? s : s.toLowerCase();
|
|
30
|
-
o = (u ? h : h.toLowerCase()).includes(D);
|
|
31
|
-
} else
|
|
32
|
-
o = i ? a === s : a == s;
|
|
33
|
-
} catch {
|
|
34
|
-
o = !1;
|
|
35
|
-
}
|
|
36
|
-
m[d] = o;
|
|
37
|
-
const g = z(o, n, p, M);
|
|
38
|
-
if (g.shouldExit)
|
|
39
|
-
return { match: g.newMatch, matches: m };
|
|
40
|
-
M = g.newMatch;
|
|
41
|
-
}
|
|
42
|
-
return { match: M, matches: m };
|
|
43
|
-
}
|
|
44
|
-
function K(c, r, e) {
|
|
45
|
-
const i = [];
|
|
46
|
-
for (const l of c) {
|
|
47
|
-
const { match: u, matches: S } = O(
|
|
48
|
-
l,
|
|
49
|
-
r,
|
|
50
|
-
e
|
|
51
|
-
);
|
|
52
|
-
let y = u;
|
|
53
|
-
if (e.customMatcher)
|
|
54
|
-
try {
|
|
55
|
-
const t = e.customMatcher(l, S);
|
|
56
|
-
typeof t == "boolean" ? y = t : t && typeof t.match == "boolean" && (y = t.match);
|
|
57
|
-
} catch (t) {
|
|
58
|
-
console.warn("Custom matcher function error:", t);
|
|
59
|
-
}
|
|
60
|
-
const n = { ...l }, p = l[e.childrenKey];
|
|
61
|
-
if (A(p)) {
|
|
62
|
-
const t = K(
|
|
63
|
-
p,
|
|
64
|
-
r,
|
|
65
|
-
e
|
|
66
|
-
);
|
|
67
|
-
t.length > 0 ? n[e.childrenKey] = t : delete n[e.childrenKey];
|
|
68
|
-
}
|
|
69
|
-
const M = n[e.childrenKey] && n[e.childrenKey].length > 0;
|
|
70
|
-
(y || M) && i.push(n);
|
|
71
|
-
}
|
|
72
|
-
return i;
|
|
11
|
+
function k(o, f) {
|
|
12
|
+
const h = { ...o };
|
|
13
|
+
return delete h[f], h;
|
|
73
14
|
}
|
|
74
|
-
function
|
|
75
|
-
if (!
|
|
76
|
-
if (!
|
|
77
|
-
const
|
|
15
|
+
function Q(o, f, h = {}) {
|
|
16
|
+
if (!S(o)) throw new TypeError("参数 treeData 必须是一个数组");
|
|
17
|
+
if (!B(f)) throw new TypeError("参数 searchCondition 必须是一个对象");
|
|
18
|
+
const w = {
|
|
78
19
|
strategy: "BFS",
|
|
79
20
|
childrenKey: "children",
|
|
80
21
|
strict: !1,
|
|
@@ -83,68 +24,96 @@ function V(c, r, e = {}) {
|
|
|
83
24
|
caseSensitive: !1,
|
|
84
25
|
allowEmptyString: !1,
|
|
85
26
|
matchMode: "AND",
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
27
|
+
returnTree: !1,
|
|
28
|
+
returnTreeMaxDepth: 0,
|
|
29
|
+
// 只在 returnTree 为 true 时才生效
|
|
30
|
+
...h
|
|
89
31
|
// 用户配置覆盖默认值
|
|
90
32
|
}, {
|
|
91
|
-
strategy:
|
|
92
|
-
childrenKey:
|
|
93
|
-
strict:
|
|
94
|
-
fuzzyMatch:
|
|
95
|
-
returnAllMatches:
|
|
96
|
-
caseSensitive:
|
|
97
|
-
allowEmptyString:
|
|
98
|
-
matchMode:
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
} =
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
33
|
+
strategy: E,
|
|
34
|
+
childrenKey: a,
|
|
35
|
+
strict: R,
|
|
36
|
+
fuzzyMatch: b,
|
|
37
|
+
returnAllMatches: M,
|
|
38
|
+
caseSensitive: v,
|
|
39
|
+
allowEmptyString: z,
|
|
40
|
+
matchMode: T,
|
|
41
|
+
returnTree: g,
|
|
42
|
+
returnTreeMaxDepth: p
|
|
43
|
+
} = w, D = T === "OR", x = T === "AND", y = E === "BFS";
|
|
44
|
+
if (!(y || E === "DFS")) throw new Error('配置 strategy 必须是 "BFS" 或 "DFS"');
|
|
45
|
+
if (!(D || x)) throw new Error('配置 matchMode 必须是 "AND" 或 "OR"');
|
|
46
|
+
if (!m(a)) throw new TypeError("配置 childrenKey 必须是字符串");
|
|
47
|
+
if (g) {
|
|
48
|
+
if (!M) throw new Error(
|
|
49
|
+
"当 returnTree 为 true 时,returnAllMatches 也必须为 true"
|
|
107
50
|
);
|
|
108
|
-
|
|
51
|
+
if (!(C(p) && p > -1)) throw new TypeError("配置 returnTreeMaxDepth 必须是一个非负整数");
|
|
109
52
|
}
|
|
110
|
-
const
|
|
111
|
-
if (
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
53
|
+
const d = [...o], A = [], F = Object.entries(f);
|
|
54
|
+
if (F.length) {
|
|
55
|
+
const N = (r) => {
|
|
56
|
+
let s = !1;
|
|
57
|
+
for (const [n, e] of F) {
|
|
58
|
+
const t = r[n];
|
|
59
|
+
if (e === "" && !z)
|
|
60
|
+
continue;
|
|
61
|
+
let i = !1;
|
|
62
|
+
if (K(e))
|
|
63
|
+
i = !!e(t, r);
|
|
64
|
+
else if (L(e) && m(t))
|
|
65
|
+
i = t ? e.test(t) : !1;
|
|
66
|
+
else if (b && m(e) && m(t)) {
|
|
67
|
+
let l = e, c = t;
|
|
68
|
+
v || (l = e.toLowerCase(), c = t.toLowerCase()), i = c.includes(l);
|
|
69
|
+
} else
|
|
70
|
+
i = R ? t === e : t == e;
|
|
71
|
+
const u = j(i, D, x, s);
|
|
72
|
+
if (u.shouldExit)
|
|
73
|
+
return u.newMatch;
|
|
74
|
+
s = u.newMatch;
|
|
75
|
+
}
|
|
76
|
+
return s;
|
|
77
|
+
};
|
|
78
|
+
if (g) {
|
|
79
|
+
const r = (n, e = 1) => {
|
|
80
|
+
const t = p > 0 && e > p, i = !t && N(n), u = t ? void 0 : n[a], l = [];
|
|
81
|
+
if (S(u))
|
|
82
|
+
for (const c of u) {
|
|
83
|
+
const O = r(c, e + 1);
|
|
84
|
+
O && l.push(O);
|
|
85
|
+
}
|
|
86
|
+
if (i || l.length > 0) {
|
|
87
|
+
const c = k(n, a);
|
|
88
|
+
return l.length > 0 && (c[a] = l), c;
|
|
129
89
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
90
|
+
return null;
|
|
91
|
+
}, s = [];
|
|
92
|
+
for (const n of o) {
|
|
93
|
+
const e = r(n, 1);
|
|
94
|
+
e && s.push(e);
|
|
95
|
+
}
|
|
96
|
+
return s;
|
|
97
|
+
} else
|
|
98
|
+
for (; d.length > 0; ) {
|
|
99
|
+
const r = y ? d.shift() : d.pop();
|
|
100
|
+
if (!r) continue;
|
|
101
|
+
if (N(r))
|
|
102
|
+
if (M)
|
|
103
|
+
A.push(r);
|
|
133
104
|
else
|
|
134
|
-
return
|
|
135
|
-
|
|
136
|
-
|
|
105
|
+
return r;
|
|
106
|
+
const s = r[a];
|
|
107
|
+
if (S(s))
|
|
108
|
+
if (y)
|
|
109
|
+
d.push(...s);
|
|
110
|
+
else
|
|
111
|
+
for (let n = s.length - 1; n >= 0; n--)
|
|
112
|
+
d.push(s[n]);
|
|
137
113
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
if (o)
|
|
141
|
-
h.push(...N);
|
|
142
|
-
else
|
|
143
|
-
for (let w = N.length - 1; w >= 0; w--)
|
|
144
|
-
h.push(N[w]);
|
|
145
|
-
}
|
|
146
|
-
return n ? D : null;
|
|
114
|
+
}
|
|
115
|
+
return M ? A : null;
|
|
147
116
|
}
|
|
148
117
|
export {
|
|
149
|
-
|
|
118
|
+
Q as default
|
|
150
119
|
};
|
package/types/is/index.d.ts
CHANGED
|
@@ -3,25 +3,17 @@ import isArray from 'lodash/isArray';
|
|
|
3
3
|
import isArrayBuffer from 'lodash/isArrayBuffer';
|
|
4
4
|
import isArrayLike from 'lodash/isArrayLike';
|
|
5
5
|
import isArrayLikeObject from 'lodash/isArrayLikeObject';
|
|
6
|
-
export function isBlob(o: any): boolean;
|
|
7
6
|
import isBoolean from 'lodash/isBoolean';
|
|
8
7
|
import isBuffer from 'lodash/isBuffer';
|
|
9
|
-
export function isColor(color: any): boolean;
|
|
10
|
-
export function isColor2(color: any): boolean;
|
|
11
8
|
import isDate from 'lodash/isDate';
|
|
12
9
|
import isElement from 'lodash/isElement';
|
|
13
10
|
import isEmpty from 'lodash/isEmpty';
|
|
14
|
-
export function isEmptyArray(o: any): boolean;
|
|
15
|
-
export function isEmptyObject(o: any): boolean;
|
|
16
|
-
export function isEmptyValue(o: any): boolean;
|
|
17
11
|
import isEqual from 'lodash/isEqual';
|
|
18
12
|
import isEqualWith from 'lodash/isEqualWith';
|
|
19
13
|
import isError from 'lodash/isError';
|
|
20
|
-
export function isFile(o: any): boolean;
|
|
21
14
|
import isFinite from 'lodash/isFinite';
|
|
22
15
|
import isFunction from 'lodash/isFunction';
|
|
23
16
|
import isInteger from 'lodash/isInteger';
|
|
24
|
-
export function isJSON(text: any): boolean;
|
|
25
17
|
import isLength from 'lodash/isLength';
|
|
26
18
|
import isMap from 'lodash/isMap';
|
|
27
19
|
import isMatch from 'lodash/isMatch';
|
|
@@ -34,12 +26,6 @@ import isNumber from 'lodash/isNumber';
|
|
|
34
26
|
import isObject from 'lodash/isObject';
|
|
35
27
|
import isObjectLike from 'lodash/isObjectLike';
|
|
36
28
|
import isPlainObject from 'lodash/isPlainObject';
|
|
37
|
-
export function isPromise(p: any): p is Promise<any>;
|
|
38
|
-
export const isReadableByteStreamControllerSupported: boolean;
|
|
39
|
-
export function isReadableStream(params: any): boolean;
|
|
40
|
-
export const isReadableStreamDefaultControllerSupported: boolean;
|
|
41
|
-
export const isReadableStreamSupported: boolean;
|
|
42
|
-
export const isReadableStreamSymbolAsyncIteratorSupported: any;
|
|
43
29
|
import isRegExp from 'lodash/isRegExp';
|
|
44
30
|
import isSafeInteger from 'lodash/isSafeInteger';
|
|
45
31
|
import isSet from 'lodash/isSet';
|
|
@@ -47,7 +33,21 @@ import isString from 'lodash/isString';
|
|
|
47
33
|
import isSymbol from 'lodash/isSymbol';
|
|
48
34
|
import isTypedArray from 'lodash/isTypedArray';
|
|
49
35
|
import isUndefined from 'lodash/isUndefined';
|
|
50
|
-
export function isValidColor(value: any): boolean;
|
|
51
36
|
import isWeakMap from 'lodash/isWeakMap';
|
|
52
37
|
import isWeakSet from 'lodash/isWeakSet';
|
|
53
|
-
|
|
38
|
+
declare function isPromise(p: any): p is Promise<any>;
|
|
39
|
+
declare function isEmptyArray(o: any): boolean;
|
|
40
|
+
declare function isEmptyObject(o: any): boolean;
|
|
41
|
+
declare function isEmptyValue(o: any): boolean;
|
|
42
|
+
declare function isFile(o: any): boolean;
|
|
43
|
+
declare function isBlob(o: any): boolean;
|
|
44
|
+
declare function isReadableStream(params: any): boolean;
|
|
45
|
+
declare const isReadableStreamSupported: boolean;
|
|
46
|
+
declare const isReadableStreamDefaultControllerSupported: boolean;
|
|
47
|
+
declare const isReadableByteStreamControllerSupported: boolean;
|
|
48
|
+
declare const isReadableStreamSymbolAsyncIteratorSupported: any;
|
|
49
|
+
declare function isJSON(text: any): boolean;
|
|
50
|
+
declare function isColor(color: any): boolean;
|
|
51
|
+
declare function isColor2(color: any): boolean;
|
|
52
|
+
declare function isValidColor(value: any): boolean;
|
|
53
|
+
export { isArguments, isArray, isArrayBuffer, isArrayLike, isArrayLikeObject, isBlob, isBoolean, isBuffer, isColor, isColor2, isDate, isElement, isEmpty, isEmptyArray, isEmptyObject, isEmptyValue, isEqual, isEqualWith, isError, isFile, isFinite, isFunction, isInteger, isJSON, isLength, isMap, isMatch, isMatchWith, isNaN, isNative, isNil, isNull, isNumber, isObject, isObjectLike, isPlainObject, isPromise, isReadableByteStreamControllerSupported, isReadableStream, isReadableStreamDefaultControllerSupported, isReadableStreamSupported, isReadableStreamSymbolAsyncIteratorSupported, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isUndefined, isValidColor, isWeakMap, isWeakSet, };
|
|
@@ -9,19 +9,7 @@ type Primitive = string | number | boolean | symbol | null | undefined;
|
|
|
9
9
|
* 3. 正则表达式
|
|
10
10
|
* @template T 节点值的泛型类型
|
|
11
11
|
*/
|
|
12
|
-
type ConditionValue<T = unknown> = Primitive | ((value: T) => boolean) | RegExp;
|
|
13
|
-
/**
|
|
14
|
-
* 自定义匹配函数类型
|
|
15
|
-
* @param node 当前节点
|
|
16
|
-
* @param matches 当前已匹配的条件信息
|
|
17
|
-
* @returns 返回boolean或对象包含匹配详情
|
|
18
|
-
*/
|
|
19
|
-
type CustomMatchFunction = (node: TreeNode, matches: {
|
|
20
|
-
[key: string]: boolean;
|
|
21
|
-
}) => boolean | {
|
|
22
|
-
match: boolean;
|
|
23
|
-
reason?: string;
|
|
24
|
-
};
|
|
12
|
+
type ConditionValue<T = unknown> = Primitive | ((value: T, node?: TreeNode) => boolean) | RegExp;
|
|
25
13
|
/**
|
|
26
14
|
* 树搜索配置选项接口
|
|
27
15
|
*/
|
|
@@ -67,15 +55,15 @@ interface SearchOptions {
|
|
|
67
55
|
*/
|
|
68
56
|
allowEmptyString?: boolean;
|
|
69
57
|
/**
|
|
70
|
-
*
|
|
58
|
+
* 是否以树结构返回结果(保留父子关系)
|
|
71
59
|
* @default false
|
|
72
60
|
*/
|
|
73
|
-
|
|
61
|
+
returnTree?: boolean;
|
|
74
62
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
63
|
+
* 搜索深度限制(从1开始计数),0表示无限制
|
|
64
|
+
* @default 0
|
|
77
65
|
*/
|
|
78
|
-
|
|
66
|
+
returnTreeMaxDepth?: number;
|
|
79
67
|
}
|
|
80
68
|
/**
|
|
81
69
|
* 树节点接口定义
|