@whitesev/utils 2.9.6 → 2.9.8

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;
344
342
  }
343
+ else {
344
+ childObjectValue = UtilsContext.assign(targetValue, sourceValue, isAdd);
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;
@@ -438,6 +444,9 @@ class CommonUtil {
438
444
  }
439
445
  toJSON(data, errorCallBack) {
440
446
  let result = {};
447
+ if (data == null) {
448
+ return result;
449
+ }
441
450
  if (typeof data === "object") {
442
451
  return data;
443
452
  }
@@ -5490,7 +5499,7 @@ class DOMUtils {
5490
5499
  }
5491
5500
  const domUtils = new DOMUtils();
5492
5501
 
5493
- const version = "2.9.6";
5502
+ const version = "2.9.8";
5494
5503
 
5495
5504
  class Utils {
5496
5505
  windowApi;
@@ -8028,7 +8037,7 @@ class Utils {
8028
8037
  }
8029
8038
  }
8030
8039
  /**
8031
- * 深度获取对象属性
8040
+ * 深度获取对象的某个属性
8032
8041
  * @param target 待获取的对象
8033
8042
  * @param handler 获取属性的回调
8034
8043
  */