befly-shared 1.3.8 → 1.3.10
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 +4 -3
- package/utils/fieldClear.ts +88 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly-shared",
|
|
3
|
-
"version": "1.3.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "1.3.10",
|
|
4
|
+
"gitHead": "1fb1ccce3a69fb6a57f42a1e269c307ed159ae9a",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "Befly 纯函数与纯常量共享包(不包含 Node/浏览器特定实现)",
|
|
7
7
|
"license": "Apache-2.0",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
"./types/*": "./types/*.ts"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
|
-
"test": "bun test"
|
|
20
|
+
"test": "bun test",
|
|
21
|
+
"typecheck": "bunx tsgo --noEmit"
|
|
21
22
|
},
|
|
22
23
|
"dependencies": {
|
|
23
24
|
"es-toolkit": "^1.43.0"
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// fieldClear 工具函数实现(shared 版)
|
|
2
|
+
// 支持 pick/omit/keepValues/excludeValues/keepMap,处理对象和数组
|
|
3
|
+
|
|
4
|
+
export interface FieldClearOptions {
|
|
5
|
+
pickKeys?: string[];
|
|
6
|
+
omitKeys?: string[];
|
|
7
|
+
keepValues?: any[];
|
|
8
|
+
excludeValues?: any[];
|
|
9
|
+
keepMap?: Record<string, any>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type FieldClearResult<T> = T extends Array<infer U> ? Array<FieldClearResult<U>> : T extends object ? { [K in keyof T]?: T[K] } : T;
|
|
13
|
+
|
|
14
|
+
function isObject(val: unknown): val is Record<string, any> {
|
|
15
|
+
return val !== null && typeof val === "object" && !Array.isArray(val);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isArray(val: unknown): val is any[] {
|
|
19
|
+
return Array.isArray(val);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function fieldClear<T = any>(data: T | T[], options: FieldClearOptions = {}): FieldClearResult<T> {
|
|
23
|
+
const pickKeys = options.pickKeys;
|
|
24
|
+
const omitKeys = options.omitKeys;
|
|
25
|
+
const keepValues = options.keepValues;
|
|
26
|
+
const excludeValues = options.excludeValues;
|
|
27
|
+
const keepMap = options.keepMap;
|
|
28
|
+
|
|
29
|
+
const filterObj = (obj: Record<string, any>) => {
|
|
30
|
+
const result: Record<string, any> = {};
|
|
31
|
+
|
|
32
|
+
let keys = Object.keys(obj);
|
|
33
|
+
if (pickKeys && pickKeys.length) {
|
|
34
|
+
keys = keys.filter((k) => pickKeys.includes(k));
|
|
35
|
+
}
|
|
36
|
+
if (omitKeys && omitKeys.length) {
|
|
37
|
+
keys = keys.filter((k) => !omitKeys.includes(k));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
for (const key of keys) {
|
|
41
|
+
const value = obj[key];
|
|
42
|
+
|
|
43
|
+
// 1. keepMap 优先
|
|
44
|
+
if (keepMap && Object.prototype.hasOwnProperty.call(keepMap, key)) {
|
|
45
|
+
if (Object.is(keepMap[key], value)) {
|
|
46
|
+
result[key] = value;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 2. keepValues
|
|
52
|
+
if (keepValues && keepValues.length && !keepValues.includes(value)) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 3. excludeValues
|
|
57
|
+
if (excludeValues && excludeValues.length && excludeValues.includes(value)) {
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
result[key] = value;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return result;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
if (isArray(data)) {
|
|
68
|
+
return (data as any[])
|
|
69
|
+
.map((item) => {
|
|
70
|
+
if (isObject(item)) {
|
|
71
|
+
return filterObj(item);
|
|
72
|
+
}
|
|
73
|
+
return item;
|
|
74
|
+
})
|
|
75
|
+
.filter((item) => {
|
|
76
|
+
if (isObject(item)) {
|
|
77
|
+
return Object.keys(item).length > 0;
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}) as FieldClearResult<T>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (isObject(data)) {
|
|
84
|
+
return filterObj(data as Record<string, any>) as FieldClearResult<T>;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return data as FieldClearResult<T>;
|
|
88
|
+
}
|