befly-shared 1.3.9 → 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.
Files changed (2) hide show
  1. package/package.json +4 -3
  2. package/utils/fieldClear.ts +22 -28
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "befly-shared",
3
- "version": "1.3.9",
4
- "gitHead": "f5d17c019a58e82cfacec5ce3a1f04e3225ba838",
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"
@@ -1,39 +1,33 @@
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) {
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> {
11
15
  return val !== null && typeof val === "object" && !Array.isArray(val);
12
16
  }
13
17
 
14
- function isArray(val) {
18
+ function isArray(val: unknown): val is any[] {
15
19
  return Array.isArray(val);
16
20
  }
17
21
 
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 = {}) {
22
+ export function fieldClear<T = any>(data: T | T[], options: FieldClearOptions = {}): FieldClearResult<T> {
28
23
  const pickKeys = options.pickKeys;
29
24
  const omitKeys = options.omitKeys;
30
25
  const keepValues = options.keepValues;
31
26
  const excludeValues = options.excludeValues;
32
27
  const keepMap = options.keepMap;
33
28
 
34
- const filterObj = (obj) => {
35
- /** @type {Record<string, any>} */
36
- const result = {};
29
+ const filterObj = (obj: Record<string, any>) => {
30
+ const result: Record<string, any> = {};
37
31
 
38
32
  let keys = Object.keys(obj);
39
33
  if (pickKeys && pickKeys.length) {
@@ -71,7 +65,7 @@ export function fieldClear(data, options = {}) {
71
65
  };
72
66
 
73
67
  if (isArray(data)) {
74
- return data
68
+ return (data as any[])
75
69
  .map((item) => {
76
70
  if (isObject(item)) {
77
71
  return filterObj(item);
@@ -83,12 +77,12 @@ export function fieldClear(data, options = {}) {
83
77
  return Object.keys(item).length > 0;
84
78
  }
85
79
  return true;
86
- });
80
+ }) as FieldClearResult<T>;
87
81
  }
88
82
 
89
83
  if (isObject(data)) {
90
- return filterObj(data);
84
+ return filterObj(data as Record<string, any>) as FieldClearResult<T>;
91
85
  }
92
86
 
93
- return data;
87
+ return data as FieldClearResult<T>;
94
88
  }