@whitesev/utils 2.9.6 → 2.9.7

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,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/utils",
4
- "version": "2.9.6",
4
+ "version": "2.9.7",
5
5
  "type": "module",
6
6
  "description": "一个常用的工具库",
7
7
  "main": "dist/index.cjs.js",
package/src/CommonUtil.ts CHANGED
@@ -33,41 +33,43 @@ class CommonUtil {
33
33
  if (target == null) {
34
34
  target = {};
35
35
  }
36
+ // 当前遍历的目标对象
37
+ let iteratorTarget;
36
38
  if (isAdd) {
37
- for (const sourceKeyName in source) {
38
- const targetKeyName = sourceKeyName;
39
- const targetValue = Reflect.get(target, targetKeyName);
40
- const sourceValue = Reflect.get(source, sourceKeyName);
41
- if (
42
- typeof sourceValue === "object" &&
43
- sourceValue != null &&
44
- sourceKeyName in target &&
45
- !UtilsContext.isDOM(sourceValue)
46
- ) {
47
- /* 源端的值是object类型,且不是元素节点 */
48
- Reflect.set(target, sourceKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
49
- continue;
50
- }
51
- Reflect.set(target, sourceKeyName, sourceValue);
52
- }
39
+ // 追加并覆盖是以source为准
40
+ iteratorTarget = source;
53
41
  } else {
54
- for (const targetKeyName in target) {
55
- if (targetKeyName in source) {
56
- const targetValue = Reflect.get(target, targetKeyName);
57
- const sourceValue = Reflect.get(source, targetKeyName);
58
- if (
59
- typeof sourceValue === "object" &&
60
- sourceValue != null &&
61
- !UtilsContext.isDOM(sourceValue) &&
62
- Object.keys(sourceValue).length
63
- ) {
64
- /* 源端的值是object类型,且不是元素节点 */
65
- Reflect.set(target, targetKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
66
- continue;
42
+ // 覆盖以target为准
43
+ iteratorTarget = target;
44
+ }
45
+ for (const keyName in iteratorTarget) {
46
+ if (!isAdd && !(keyName in source)) {
47
+ // 仅替换 但是源端没有此键
48
+ continue;
49
+ }
50
+ const targetValue = Reflect.get(target, keyName);
51
+ const sourceValue = Reflect.get(source, keyName);
52
+ if (
53
+ typeof sourceValue === "object" &&
54
+ sourceValue != null &&
55
+ keyName in target &&
56
+ !UtilsContext.isDOM(sourceValue)
57
+ ) {
58
+ // 源端的值是object类型,且不是元素节点
59
+ // 如果是数组,那此数组中有值,清空旧的数组再赋值
60
+ let childObjectValue;
61
+ if (Array.isArray(sourceValue)) {
62
+ if (Array.isArray(targetValue)) {
63
+ targetValue.length = 0;
67
64
  }
68
- /* 直接赋值 */
69
- Reflect.set(target, targetKeyName, sourceValue);
65
+ childObjectValue = sourceValue;
66
+ } else {
67
+ childObjectValue = UtilsContext.assign(targetValue, sourceValue, isAdd);
70
68
  }
69
+ Reflect.set(target, keyName, childObjectValue);
70
+ } else {
71
+ /* 直接赋值 */
72
+ Reflect.set(target, keyName, sourceValue);
71
73
  }
72
74
  }
73
75