@nickyzj2023/utils 1.0.19 → 1.0.21

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/src/object.ts CHANGED
@@ -2,53 +2,51 @@ import { isObject, isPrimitive } from "./is";
2
2
 
3
3
  /**
4
4
  * 深度合并两个对象,规则如下:
5
- * 1. 原始值覆盖:如果两个值都是原始类型(string、number、boolean、null、undefined、symbol、bigint),则用后者覆盖;
5
+ * 1. 原始值覆盖:如果两个值都是原始类型,则用后者覆盖;
6
6
  * 2. 数组拼接:如果两个值都是数组,则拼接为大数组;
7
7
  * 3. 对象递归合并:如果两个值都是对象,则进行递归深度合并;
8
- * 4. 类型不匹配覆盖:如果两个值的类型不同,则用后者覆盖;
9
- * 5. 独有属性保留:如果字段只存在于其中一个对象中,则保留它。
10
8
  *
11
9
  * @template T 第一个对象
12
10
  * @template U 第二个对象
13
- * @param {T} obj1 要合并的第一个对象,相同字段可能会被 obj2 覆盖
11
+ * @param {T} obj1 要合并的第一个对象,相同字段会被 obj2 覆盖
14
12
  * @param {U} obj2 要合并的第二个对象
15
13
  */
16
14
  export const mergeObjects = <
17
- T extends Record<string, any>,
18
- U extends Record<string, any>,
15
+ T extends Record<string, any>,
16
+ U extends Record<string, any>,
19
17
  >(
20
- obj1: T,
21
- obj2: U,
18
+ obj1: T,
19
+ obj2: U,
22
20
  ) => {
23
- const result: Record<string, any> = { ...obj1 };
24
-
25
- for (const key of Object.keys(obj2)) {
26
- const val1 = result[key];
27
- const val2 = obj2[key];
28
-
29
- // 如果都是原始值,则用后者覆盖
30
- if (isPrimitive(val1) && isPrimitive(val2)) {
31
- result[key] = val2;
32
- continue;
33
- }
34
-
35
- // 如果都是数组,则拼接为大数组
36
- if (Array.isArray(val1) && Array.isArray(val2)) {
37
- result[key] = val1.concat(val2);
38
- continue;
39
- }
40
-
41
- // 如果都是对象,则深度递归合并
42
- if (isObject(val1) && isObject(val2)) {
43
- result[key] = mergeObjects(val1, val2);
44
- continue;
45
- }
46
-
47
- // 如果类型不同,则用后者覆盖
48
- result[key] = val2;
49
- }
50
-
51
- return result as T & U;
21
+ const result: Record<string, any> = { ...obj1 };
22
+
23
+ for (const key of Object.keys(obj2)) {
24
+ const val1 = result[key];
25
+ const val2 = obj2[key];
26
+
27
+ // 如果都是原始值,则用后者覆盖
28
+ if (isPrimitive(val1) && isPrimitive(val2)) {
29
+ result[key] = val2;
30
+ continue;
31
+ }
32
+
33
+ // 如果都是数组,则拼接为大数组
34
+ if (Array.isArray(val1) && Array.isArray(val2)) {
35
+ result[key] = val1.concat(val2);
36
+ continue;
37
+ }
38
+
39
+ // 如果都是对象,则深度递归合并
40
+ if (isObject(val1) && isObject(val2)) {
41
+ result[key] = mergeObjects(val1, val2);
42
+ continue;
43
+ }
44
+
45
+ // 其他情况用后者覆盖
46
+ result[key] = val2;
47
+ }
48
+
49
+ return result as T & U;
52
50
  };
53
51
 
54
52
  // --- mapKeys ---
@@ -56,11 +54,11 @@ export const mergeObjects = <
56
54
  // 这里的 DeepMapKeys 只能承诺 key 是 string,无法推断出 key 的具体字面量变化
57
55
  // 因为 TS 不支持根据任意函数反推 Key 的字面量类型
58
56
  export type DeepMapKeys<T> =
59
- T extends Array<infer U>
60
- ? Array<DeepMapKeys<U>>
61
- : T extends object
62
- ? { [key: string]: DeepMapKeys<T[keyof T]> }
63
- : T;
57
+ T extends Array<infer U>
58
+ ? Array<DeepMapKeys<U>>
59
+ : T extends object
60
+ ? { [key: string]: DeepMapKeys<T[keyof T]> }
61
+ : T;
64
62
 
65
63
  /**
66
64
  * 递归处理对象里的 key
@@ -76,30 +74,30 @@ export type DeepMapKeys<T> =
76
74
  * console.log(result); // { A: { B: 1 } }
77
75
  */
78
76
  export const mapKeys = <T>(
79
- obj: T,
80
- getNewKey: (key: string) => string,
77
+ obj: T,
78
+ getNewKey: (key: string) => string,
81
79
  ): DeepMapKeys<T> => {
82
- // 递归处理数组
83
- if (Array.isArray(obj)) {
84
- return obj.map((item) => mapKeys(item, getNewKey)) as any;
85
- }
86
-
87
- // 处理普通对象
88
- if (isObject(obj)) {
89
- const keys = Object.keys(obj);
90
- return keys.reduce(
91
- (result, key) => {
92
- const newKey = getNewKey(key);
93
- const value = (obj as any)[key];
94
- result[newKey] = mapKeys(value, getNewKey);
95
- return result;
96
- },
97
- {} as Record<string, any>,
98
- ) as DeepMapKeys<T>;
99
- }
100
-
101
- // 处理非数组/对象
102
- return obj as any;
80
+ // 递归处理数组
81
+ if (Array.isArray(obj)) {
82
+ return obj.map((item) => mapKeys(item, getNewKey)) as any;
83
+ }
84
+
85
+ // 处理普通对象
86
+ if (isObject(obj)) {
87
+ const keys = Object.keys(obj);
88
+ return keys.reduce(
89
+ (result, key) => {
90
+ const newKey = getNewKey(key);
91
+ const value = (obj as any)[key];
92
+ result[newKey] = mapKeys(value, getNewKey);
93
+ return result;
94
+ },
95
+ {} as Record<string, any>,
96
+ ) as DeepMapKeys<T>;
97
+ }
98
+
99
+ // 处理非数组/对象
100
+ return obj as any;
103
101
  };
104
102
 
105
103
  // --- mapValues ---
@@ -107,11 +105,11 @@ export const mapKeys = <T>(
107
105
  // 这里的 DeepMapValues 尝试保留 key,但将 value 类型替换为 R
108
106
  // 注意:如果原 value 是对象,我们递归处理结构,而不是把整个对象变成 R
109
107
  export type DeepMapValues<T, R> =
110
- T extends Array<infer U>
111
- ? Array<DeepMapValues<U, R>>
112
- : T extends object
113
- ? { [K in keyof T]: T[K] extends object ? DeepMapValues<T[K], R> : R }
114
- : R;
108
+ T extends Array<infer U>
109
+ ? Array<DeepMapValues<U, R>>
110
+ : T extends object
111
+ ? { [K in keyof T]: T[K] extends object ? DeepMapValues<T[K], R> : R }
112
+ : R;
115
113
 
116
114
  /**
117
115
  * 递归处理对象里的 value
@@ -128,38 +126,38 @@ export type DeepMapValues<T, R> =
128
126
  * console.log(result); // { a: 2, b: { c: 3 } }
129
127
  */
130
128
  export const mapValues = <T, R = any>(
131
- obj: T,
132
- getNewValue: (value: any, key: string | number) => R,
129
+ obj: T,
130
+ getNewValue: (value: any, key: string | number) => R,
133
131
  ): DeepMapValues<T, R> => {
134
- // 处理数组
135
- if (Array.isArray(obj)) {
136
- return obj.map((item, index) => {
137
- // 如果元素是对象,则递归处理
138
- if (isObject(item)) {
139
- return mapValues(item, getNewValue);
140
- }
141
- // 如果元素是原始值,则直接应用 getNewValue(此时 key 为数组下标)
142
- return getNewValue(item, index);
143
- }) as any;
144
- }
145
-
146
- // 处理普通对象
147
- if (isObject(obj)) {
148
- const keys = Object.keys(obj);
149
- return keys.reduce((result, key) => {
150
- const value = (obj as any)[key];
151
- // 如果值为对象或数组,则递归处理
152
- if (!isPrimitive(value)) {
153
- result[key] = mapValues(value, getNewValue);
154
- }
155
- // 否则直接应用 getNewValue
156
- else {
157
- result[key] = getNewValue(value, key);
158
- }
159
- return result;
160
- }, {} as any) as DeepMapValues<T, R>;
161
- }
162
-
163
- // 处理非数组/对象
164
- return obj as any;
132
+ // 处理数组
133
+ if (Array.isArray(obj)) {
134
+ return obj.map((item, index) => {
135
+ // 如果元素是对象,则递归处理
136
+ if (isObject(item)) {
137
+ return mapValues(item, getNewValue);
138
+ }
139
+ // 如果元素是原始值,则直接应用 getNewValue(此时 key 为数组下标)
140
+ return getNewValue(item, index);
141
+ }) as any;
142
+ }
143
+
144
+ // 处理普通对象
145
+ if (isObject(obj)) {
146
+ const keys = Object.keys(obj);
147
+ return keys.reduce((result, key) => {
148
+ const value = (obj as any)[key];
149
+ // 如果值为对象或数组,则递归处理
150
+ if (isObject(value) || Array.isArray(value)) {
151
+ result[key] = mapValues(value, getNewValue);
152
+ }
153
+ // 否则直接应用 getNewValue
154
+ else {
155
+ result[key] = getNewValue(value, key);
156
+ }
157
+ return result;
158
+ }, {} as any) as DeepMapValues<T, R>;
159
+ }
160
+
161
+ // 处理非数组/对象
162
+ return obj as any;
165
163
  };
package/src/string.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * 下划线命名法转为驼峰命名法
3
3
  *
4
4
  * @example
5
- * snakeToCamel("user_name") // 'userName'
5
+ * snakeToCamel("user_name") // "userName"
6
6
  */
7
7
  export const snakeToCamel = (str: string) => {
8
8
  return str.replace(/_([a-zA-Z])/g, (match, pattern) => pattern.toUpperCase());
@@ -12,7 +12,7 @@ export const snakeToCamel = (str: string) => {
12
12
  * 驼峰命名法转为下划线命名法
13
13
  *
14
14
  * @example
15
- * camelToSnake("shouldComponentUpdate") // 'should_component_update'
15
+ * camelToSnake("shouldComponentUpdate") // "should_component_update"
16
16
  */
17
17
  export const camelToSnake = (str: string) => {
18
18
  return str.replace(
@@ -25,7 +25,7 @@ export const camelToSnake = (str: string) => {
25
25
  * 字符串首字母大写
26
26
  *
27
27
  * @example
28
- * capitalize("hello") // 'Hello'
28
+ * capitalize("hello") // "Hello"
29
29
  */
30
30
  export const capitalize = (s: string) => {
31
31
  return s.charAt(0).toUpperCase() + s.slice(1);
@@ -35,7 +35,7 @@ export const capitalize = (s: string) => {
35
35
  * 字符串首字母小写
36
36
  *
37
37
  * @example
38
- * decapitalize("Hello") // 'hello'
38
+ * decapitalize("Hello") // "hello"
39
39
  */
40
40
  export const decapitalize = (s: string) => {
41
41
  return s.charAt(0).toLowerCase() + s.slice(1);
package/src/time.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * await sleep(1000); // 等待 1 秒执行后续代码
6
6
  */
7
7
  export const sleep = async (time = 150) => {
8
- return new Promise((resolve) => {
9
- setTimeout(resolve, time);
10
- });
8
+ return new Promise((resolve) => {
9
+ setTimeout(resolve, time);
10
+ });
11
11
  };