befly-shared 1.3.7 → 1.3.9
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 +3 -3
- package/utils/fieldClear.ts +94 -0
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly-shared",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.9",
|
|
4
|
+
"gitHead": "f5d17c019a58e82cfacec5ce3a1f04e3225ba838",
|
|
4
5
|
"private": false,
|
|
5
6
|
"description": "Befly 纯函数与纯常量共享包(不包含 Node/浏览器特定实现)",
|
|
6
7
|
"license": "Apache-2.0",
|
|
@@ -23,6 +24,5 @@
|
|
|
23
24
|
},
|
|
24
25
|
"engines": {
|
|
25
26
|
"bun": ">=1.3.0"
|
|
26
|
-
}
|
|
27
|
-
"gitHead": "79270d11fb0ff1a5dafada0d12812f4335ef9f45"
|
|
27
|
+
}
|
|
28
28
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {Object} FieldClearOptions
|
|
3
|
+
* @property {string[]=} pickKeys
|
|
4
|
+
* @property {string[]=} omitKeys
|
|
5
|
+
* @property {any[]=} keepValues
|
|
6
|
+
* @property {any[]=} excludeValues
|
|
7
|
+
* @property {Record<string, any>=} keepMap
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
function isObject(val) {
|
|
11
|
+
return val !== null && typeof val === "object" && !Array.isArray(val);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function isArray(val) {
|
|
15
|
+
return Array.isArray(val);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 清理对象/数组字段
|
|
20
|
+
* - 支持 pick/omit/keepValues/excludeValues
|
|
21
|
+
* - 支持 keepMap 强制保留
|
|
22
|
+
* @template T
|
|
23
|
+
* @param {T|T[]} data
|
|
24
|
+
* @param {FieldClearOptions=} options
|
|
25
|
+
* @returns {any}
|
|
26
|
+
*/
|
|
27
|
+
export function fieldClear(data, options = {}) {
|
|
28
|
+
const pickKeys = options.pickKeys;
|
|
29
|
+
const omitKeys = options.omitKeys;
|
|
30
|
+
const keepValues = options.keepValues;
|
|
31
|
+
const excludeValues = options.excludeValues;
|
|
32
|
+
const keepMap = options.keepMap;
|
|
33
|
+
|
|
34
|
+
const filterObj = (obj) => {
|
|
35
|
+
/** @type {Record<string, any>} */
|
|
36
|
+
const result = {};
|
|
37
|
+
|
|
38
|
+
let keys = Object.keys(obj);
|
|
39
|
+
if (pickKeys && pickKeys.length) {
|
|
40
|
+
keys = keys.filter((k) => pickKeys.includes(k));
|
|
41
|
+
}
|
|
42
|
+
if (omitKeys && omitKeys.length) {
|
|
43
|
+
keys = keys.filter((k) => !omitKeys.includes(k));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
for (const key of keys) {
|
|
47
|
+
const value = obj[key];
|
|
48
|
+
|
|
49
|
+
// 1. keepMap 优先
|
|
50
|
+
if (keepMap && Object.prototype.hasOwnProperty.call(keepMap, key)) {
|
|
51
|
+
if (Object.is(keepMap[key], value)) {
|
|
52
|
+
result[key] = value;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 2. keepValues
|
|
58
|
+
if (keepValues && keepValues.length && !keepValues.includes(value)) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 3. excludeValues
|
|
63
|
+
if (excludeValues && excludeValues.length && excludeValues.includes(value)) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
result[key] = value;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return result;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
if (isArray(data)) {
|
|
74
|
+
return data
|
|
75
|
+
.map((item) => {
|
|
76
|
+
if (isObject(item)) {
|
|
77
|
+
return filterObj(item);
|
|
78
|
+
}
|
|
79
|
+
return item;
|
|
80
|
+
})
|
|
81
|
+
.filter((item) => {
|
|
82
|
+
if (isObject(item)) {
|
|
83
|
+
return Object.keys(item).length > 0;
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (isObject(data)) {
|
|
90
|
+
return filterObj(data);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return data;
|
|
94
|
+
}
|