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