@whitesev/domutils 1.7.3 → 1.7.5

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.amd.js CHANGED
@@ -523,7 +523,7 @@ define((function () { 'use strict';
523
523
  },
524
524
  };
525
525
 
526
- const version = "1.7.3";
526
+ const version = "1.7.5";
527
527
 
528
528
  /* 数据 */
529
529
  const GlobalData = {
@@ -901,35 +901,41 @@ define((function () { 'use strict';
901
901
  if (target == null) {
902
902
  target = {};
903
903
  }
904
+ // 当前遍历的目标对象
905
+ let iteratorTarget;
904
906
  if (isAdd) {
905
- for (const sourceKeyName in source) {
906
- const targetKeyName = sourceKeyName;
907
- const targetValue = Reflect.get(target, targetKeyName);
908
- const sourceValue = Reflect.get(source, sourceKeyName);
909
- if (typeof sourceValue === "object" && sourceValue != null && sourceKeyName in target && !isDOM(sourceValue)) {
910
- /* 源端的值是object类型,且不是元素节点 */
911
- Reflect.set(target, sourceKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
912
- continue;
913
- }
914
- Reflect.set(target, sourceKeyName, sourceValue);
915
- }
907
+ // 追加并覆盖是以source为准
908
+ iteratorTarget = source;
916
909
  }
917
910
  else {
918
- for (const targetKeyName in target) {
919
- if (targetKeyName in source) {
920
- const targetValue = Reflect.get(target, targetKeyName);
921
- const sourceValue = Reflect.get(source, targetKeyName);
922
- if (typeof sourceValue === "object" &&
923
- sourceValue != null &&
924
- !isDOM(sourceValue) &&
925
- Object.keys(sourceValue).length) {
926
- /* 源端的值是object类型,且不是元素节点 */
927
- Reflect.set(target, targetKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
928
- continue;
911
+ // 覆盖以target为准
912
+ iteratorTarget = target;
913
+ }
914
+ for (const keyName in iteratorTarget) {
915
+ if (!isAdd && !(keyName in source)) {
916
+ // 仅替换 但是源端没有此键
917
+ continue;
918
+ }
919
+ const targetValue = Reflect.get(target, keyName);
920
+ const sourceValue = Reflect.get(source, keyName);
921
+ if (typeof sourceValue === "object" && sourceValue != null && keyName in target && !isDOM(sourceValue)) {
922
+ // 源端的值是object类型,且不是元素节点
923
+ // 如果是数组,那此数组中有值,清空旧的数组再赋值
924
+ let childObjectValue;
925
+ if (Array.isArray(sourceValue)) {
926
+ if (Array.isArray(targetValue)) {
927
+ targetValue.length = 0;
929
928
  }
930
- /* 直接赋值 */
931
- Reflect.set(target, targetKeyName, sourceValue);
929
+ childObjectValue = sourceValue;
930
+ }
931
+ else {
932
+ childObjectValue = UtilsContext.assign(targetValue, sourceValue, isAdd);
932
933
  }
934
+ Reflect.set(target, keyName, childObjectValue);
935
+ }
936
+ else {
937
+ /* 直接赋值 */
938
+ Reflect.set(target, keyName, sourceValue);
933
939
  }
934
940
  }
935
941
  return target;
@@ -3284,12 +3290,12 @@ define((function () { 'use strict';
3284
3290
  }
3285
3291
  /**
3286
3292
  * 移除元素
3287
- * @param $el 目标元素
3293
+ * @param $el 目标元素,可以是数组、单个元素、NodeList、元素选择器
3288
3294
  * @example
3289
- * // 元素a.xx前面添加一个元素
3290
3295
  * DOMUtils.remove(document.querySelector("a.xx"))
3291
3296
  * DOMUtils.remove(document.querySelectorAll("a.xx"))
3292
3297
  * DOMUtils.remove("a.xx")
3298
+ * DOMUtils.remove([a.xxx, div.xxx, span.xxx])
3293
3299
  * */
3294
3300
  remove($el) {
3295
3301
  const that = this;