@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/dist/index.esm.js CHANGED
@@ -310,38 +310,44 @@ class CommonUtil {
310
310
  if (target == null) {
311
311
  target = {};
312
312
  }
313
+ // 当前遍历的目标对象
314
+ let iteratorTarget;
313
315
  if (isAdd) {
314
- for (const sourceKeyName in source) {
315
- const targetKeyName = sourceKeyName;
316
- const targetValue = Reflect.get(target, targetKeyName);
317
- const sourceValue = Reflect.get(source, sourceKeyName);
318
- if (typeof sourceValue === "object" &&
319
- sourceValue != null &&
320
- sourceKeyName in target &&
321
- !UtilsContext.isDOM(sourceValue)) {
322
- /* 源端的值是object类型,且不是元素节点 */
323
- Reflect.set(target, sourceKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
324
- continue;
325
- }
326
- Reflect.set(target, sourceKeyName, sourceValue);
327
- }
316
+ // 追加并覆盖是以source为准
317
+ iteratorTarget = source;
328
318
  }
329
319
  else {
330
- for (const targetKeyName in target) {
331
- if (targetKeyName in source) {
332
- const targetValue = Reflect.get(target, targetKeyName);
333
- const sourceValue = Reflect.get(source, targetKeyName);
334
- if (typeof sourceValue === "object" &&
335
- sourceValue != null &&
336
- !UtilsContext.isDOM(sourceValue) &&
337
- Object.keys(sourceValue).length) {
338
- /* 源端的值是object类型,且不是元素节点 */
339
- Reflect.set(target, targetKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
340
- continue;
320
+ // 覆盖以target为准
321
+ iteratorTarget = target;
322
+ }
323
+ for (const keyName in iteratorTarget) {
324
+ if (!isAdd && !(keyName in source)) {
325
+ // 仅替换 但是源端没有此键
326
+ continue;
327
+ }
328
+ const targetValue = Reflect.get(target, keyName);
329
+ const sourceValue = Reflect.get(source, keyName);
330
+ if (typeof sourceValue === "object" &&
331
+ sourceValue != null &&
332
+ keyName in target &&
333
+ !UtilsContext.isDOM(sourceValue)) {
334
+ // 源端的值是object类型,且不是元素节点
335
+ // 如果是数组,那此数组中有值,清空旧的数组再赋值
336
+ let childObjectValue;
337
+ if (Array.isArray(sourceValue)) {
338
+ if (Array.isArray(targetValue)) {
339
+ targetValue.length = 0;
341
340
  }
342
- /* 直接赋值 */
343
- Reflect.set(target, targetKeyName, sourceValue);
341
+ childObjectValue = sourceValue;
342
+ }
343
+ else {
344
+ childObjectValue = UtilsContext.assign(targetValue, sourceValue, isAdd);
344
345
  }
346
+ Reflect.set(target, keyName, childObjectValue);
347
+ }
348
+ else {
349
+ /* 直接赋值 */
350
+ Reflect.set(target, keyName, sourceValue);
345
351
  }
346
352
  }
347
353
  return target;
@@ -5490,7 +5496,7 @@ class DOMUtils {
5490
5496
  }
5491
5497
  const domUtils = new DOMUtils();
5492
5498
 
5493
- const version = "2.9.6";
5499
+ const version = "2.9.7";
5494
5500
 
5495
5501
  class Utils {
5496
5502
  windowApi;