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