@unicom-cloud/utils 0.1.21 → 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 CHANGED
@@ -1 +1 @@
1
- {"name":"@unicom-cloud/utils","version":"0.1.21","dependencies":{},"peerDependencies":{"lodash":"^4.17.21"},"main":"./index.js","type":"module","types":"types/index.d.ts","publishConfig":{"registry":"https://registry.npmjs.org/","access":"public"}}
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"}}
@@ -1,80 +1,17 @@
1
1
  import "../../is/index.js";
2
- import A from "lodash/isArray";
3
- import T from "lodash/isObjectLike";
4
- import F from "lodash/isString";
5
- import x from "lodash/isFunction";
6
- function z(c, r, e, i) {
7
- return r && c ? { shouldExit: !0, newMatch: !0 } : e && !c ? { shouldExit: !0, newMatch: !1 } : { shouldExit: !1, newMatch: i || c };
2
+ import S from "lodash/isArray";
3
+ import B from "lodash/isObjectLike";
4
+ import p from "lodash/isString";
5
+ import C from "lodash/isNumber";
6
+ import K from "lodash/isFunction";
7
+ import L from "lodash/isRegExp";
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 };
8
10
  }
9
- function O(c, r, e) {
10
- const { strict: i, fuzzyMatch: l, caseSensitive: u, matchMode: S, allowEmptyString: y } = e, n = S === "OR", p = S === "AND";
11
- let M = !1;
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;
73
- }
74
- function V(c, r, e = {}) {
75
- if (!A(c)) throw new TypeError("参数treeData必须是一个数组");
76
- if (!T(r)) throw new TypeError("参数searchCondition必须是一个对象");
77
- const i = {
11
+ function P(i, a, m = {}) {
12
+ if (!S(i)) throw new TypeError("参数 treeData 必须是一个数组");
13
+ if (!B(a)) throw new TypeError("参数 searchCondition 必须是一个对象");
14
+ const w = {
78
15
  strategy: "BFS",
79
16
  childrenKey: "children",
80
17
  strict: !1,
@@ -83,68 +20,96 @@ function V(c, r, e = {}) {
83
20
  caseSensitive: !1,
84
21
  allowEmptyString: !1,
85
22
  matchMode: "AND",
86
- preserveTreeStructure: !1,
87
- customMatcher: void 0,
88
- ...e
23
+ returnTree: !1,
24
+ returnTreeMaxDepth: 0,
25
+ // 只在 returnTree 为 true 时才生效
26
+ ...m
89
27
  // 用户配置覆盖默认值
90
28
  }, {
91
- strategy: l,
29
+ strategy: E,
92
30
  childrenKey: u,
93
- strict: S,
94
- fuzzyMatch: y,
95
- returnAllMatches: n,
96
- caseSensitive: p,
97
- allowEmptyString: M,
98
- matchMode: t,
99
- preserveTreeStructure: m,
100
- customMatcher: d
101
- } = i;
102
- if (m) {
103
- const f = K(
104
- c,
105
- r,
106
- i
31
+ strict: R,
32
+ fuzzyMatch: b,
33
+ returnAllMatches: M,
34
+ caseSensitive: v,
35
+ allowEmptyString: z,
36
+ matchMode: T,
37
+ returnTree: g,
38
+ returnTreeMaxDepth: d
39
+ } = w, D = T === "OR", x = T === "AND", y = E === "BFS";
40
+ if (!(y || E === "DFS")) throw new Error('配置 strategy 必须是 "BFS" 或 "DFS"');
41
+ if (!(D || x)) throw new Error('配置 matchMode 必须是 "AND" 或 "OR"');
42
+ if (!p(u)) throw new TypeError("配置 childrenKey 必须是字符串");
43
+ if (g) {
44
+ if (!M) throw new Error(
45
+ "当 returnTree 为 true 时,returnAllMatches 也必须为 true"
107
46
  );
108
- return n ? f : f[0] || null;
47
+ if (!(C(d) && d > -1)) throw new TypeError("配置 returnTreeMaxDepth 必须是一个非负整数");
109
48
  }
110
- const s = t === "OR", a = t === "AND", o = l === "BFS";
111
- if (!(o || l === "DFS")) throw new Error('配置 strategy 必须是 "BFS" 或 "DFS"');
112
- if (!(s || a)) throw new Error('配置 matchMode 必须是 "AND" 或 "OR"');
113
- if (!F(u)) throw new TypeError("配置 childrenKey 必须是字符串");
114
- const h = [...c], D = [];
115
- if (Object.entries(r).length)
116
- for (; h.length > 0; ) {
117
- const f = o ? h.shift() : h.pop();
118
- if (!f) continue;
119
- try {
120
- const { match: w, matches: v } = O(
121
- f,
122
- r,
123
- i
124
- );
125
- let R = w;
126
- if (d) {
127
- const E = d(f, v);
128
- typeof E == "boolean" ? R = E : E && typeof E.match == "boolean" && (R = E.match);
49
+ const h = [...i], A = [], F = Object.entries(a);
50
+ if (F.length) {
51
+ const N = (r) => {
52
+ let s = !1;
53
+ for (const [n, e] of F) {
54
+ const t = r[n];
55
+ if (e === "" && !z)
56
+ continue;
57
+ let o = !1;
58
+ if (K(e))
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)) {
63
+ let l = e, c = t;
64
+ v || (l = e.toLowerCase(), c = t.toLowerCase()), o = c.includes(l);
65
+ } else
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;
71
+ }
72
+ return s;
73
+ };
74
+ if (g) {
75
+ const r = (n, e = 1) => {
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) {
79
+ const O = r(c, e + 1);
80
+ O && l.push(O);
81
+ }
82
+ if (o || l.length > 0) {
83
+ const c = { ...n };
84
+ return l.length > 0 && (c[u] = l), c;
129
85
  }
130
- if (R)
131
- if (n)
132
- D.push(f);
86
+ return null;
87
+ }, s = [];
88
+ for (const n of i) {
89
+ const e = r(n, 1);
90
+ e && s.push(e);
91
+ }
92
+ return s;
93
+ } else
94
+ for (; h.length > 0; ) {
95
+ const r = y ? h.shift() : h.pop();
96
+ if (!r) continue;
97
+ if (N(r))
98
+ if (M)
99
+ A.push(r);
133
100
  else
134
- return f;
135
- } catch {
136
- continue;
101
+ return r;
102
+ const s = r[u];
103
+ if (S(s))
104
+ if (y)
105
+ h.push(...s);
106
+ else
107
+ for (let n = s.length - 1; n >= 0; n--)
108
+ h.push(s[n]);
137
109
  }
138
- const N = f[u];
139
- if (A(N))
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;
110
+ }
111
+ return M ? A : null;
147
112
  }
148
113
  export {
149
- V as default
114
+ P as default
150
115
  };
@@ -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
- export { isArguments, isArray, isArrayBuffer, isArrayLike, isArrayLikeObject, isBoolean, isBuffer, isDate, isElement, isEmpty, isEqual, isEqualWith, isError, isFinite, isFunction, isInteger, isLength, isMap, isMatch, isMatchWith, isNaN, isNative, isNil, isNull, isNumber, isObject, isObjectLike, isPlainObject, isRegExp, isSafeInteger, isSet, isString, isSymbol, isTypedArray, isUndefined, isWeakMap, isWeakSet };
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
- preserveTreeStructure?: boolean;
61
+ returnTree?: boolean;
74
62
  /**
75
- * 自定义匹配函数,可以覆盖默认的匹配逻辑
76
- * 返回true表示匹配,false表示不匹配
63
+ * 搜索深度限制(从1开始计数),0表示无限制
64
+ * @default 0
77
65
  */
78
- customMatcher?: CustomMatchFunction;
66
+ returnTreeMaxDepth?: number;
79
67
  }
80
68
  /**
81
69
  * 树节点接口定义