@whitesev/utils 2.9.5 → 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
@@ -79,6 +79,9 @@ class ColorConversion {
79
79
  if (typeof level !== "number") {
80
80
  level = Number(level);
81
81
  }
82
+ if (isNaN(level)) {
83
+ throw new TypeError(`输入错误的level:${level}`);
84
+ }
82
85
  const rgbc = this.hexToRgb(color);
83
86
  for (let index = 0; index < 3; index++) {
84
87
  const rgbcItemValue = rgbc[index];
@@ -99,6 +102,9 @@ class ColorConversion {
99
102
  if (typeof level !== "number") {
100
103
  level = Number(level);
101
104
  }
105
+ if (isNaN(level)) {
106
+ throw new TypeError(`输入错误的level:${level}`);
107
+ }
102
108
  const rgbc = this.hexToRgb(color);
103
109
  for (let index = 0; index < 3; index++) {
104
110
  const rgbcItemValue = Number(rgbc[index]);
@@ -306,38 +312,44 @@ class CommonUtil {
306
312
  if (target == null) {
307
313
  target = {};
308
314
  }
315
+ // 当前遍历的目标对象
316
+ let iteratorTarget;
309
317
  if (isAdd) {
310
- for (const sourceKeyName in source) {
311
- const targetKeyName = sourceKeyName;
312
- const targetValue = Reflect.get(target, targetKeyName);
313
- const sourceValue = Reflect.get(source, sourceKeyName);
314
- if (typeof sourceValue === "object" &&
315
- sourceValue != null &&
316
- sourceKeyName in target &&
317
- !UtilsContext.isDOM(sourceValue)) {
318
- /* 源端的值是object类型,且不是元素节点 */
319
- Reflect.set(target, sourceKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
320
- continue;
321
- }
322
- Reflect.set(target, sourceKeyName, sourceValue);
323
- }
318
+ // 追加并覆盖是以source为准
319
+ iteratorTarget = source;
324
320
  }
325
321
  else {
326
- for (const targetKeyName in target) {
327
- if (targetKeyName in source) {
328
- const targetValue = Reflect.get(target, targetKeyName);
329
- const sourceValue = Reflect.get(source, targetKeyName);
330
- if (typeof sourceValue === "object" &&
331
- sourceValue != null &&
332
- !UtilsContext.isDOM(sourceValue) &&
333
- Object.keys(sourceValue).length) {
334
- /* 源端的值是object类型,且不是元素节点 */
335
- Reflect.set(target, targetKeyName, UtilsContext.assign(targetValue, sourceValue, isAdd));
336
- 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;
337
342
  }
338
- /* 直接赋值 */
339
- Reflect.set(target, targetKeyName, sourceValue);
343
+ childObjectValue = sourceValue;
344
+ }
345
+ else {
346
+ childObjectValue = UtilsContext.assign(targetValue, sourceValue, isAdd);
340
347
  }
348
+ Reflect.set(target, keyName, childObjectValue);
349
+ }
350
+ else {
351
+ /* 直接赋值 */
352
+ Reflect.set(target, keyName, sourceValue);
341
353
  }
342
354
  }
343
355
  return target;
@@ -582,7 +594,7 @@ class UtilsGMCookie {
582
594
  }
583
595
  }
584
596
  /**
585
- * 获取多组Cookie
597
+ * 获取多组Cookie
586
598
  * @param option 配置
587
599
  **/
588
600
  getList(option) {
@@ -695,7 +707,8 @@ class UtilsGMCookie {
695
707
  }
696
708
  }
697
709
  /**
698
- * 解析cookie字符串
710
+ * 解析cookie字符串,按`;`分割
711
+ *
699
712
  * 例如:document.cookie
700
713
  * @param cookieStr
701
714
  */
@@ -2112,31 +2125,31 @@ class GMMenu {
2112
2125
  }
2113
2126
  /**
2114
2127
  * 设置当enable为true时默认显示在菜单中前面的emoji图标
2115
- * @param emojiString
2128
+ * @param emojiString emoji字符串
2116
2129
  */
2117
2130
  setEnableTrueEmoji(emojiString) {
2118
2131
  if (typeof emojiString !== "string") {
2119
- throw new Error("参数emojiString必须是string类型");
2132
+ throw new TypeError("参数emojiString必须是string类型");
2120
2133
  }
2121
2134
  this.MenuHandle.$emoji.success = emojiString;
2122
2135
  }
2123
2136
  /**
2124
2137
  * 设置当enable为false时默认显示在菜单中前面的emoji图标
2125
- * @param emojiString
2138
+ * @param emojiString emoji字符串
2126
2139
  */
2127
2140
  setEnableFalseEmoji(emojiString) {
2128
2141
  if (typeof emojiString !== "string") {
2129
- throw new Error("参数emojiString必须是string类型");
2142
+ throw new TypeError("参数emojiString必须是string类型");
2130
2143
  }
2131
2144
  this.MenuHandle.$emoji.error = emojiString;
2132
2145
  }
2133
2146
  /**
2134
2147
  * 设置本地存储的菜单外部的键名
2135
- * @param keyName
2148
+ * @param keyName 键名
2136
2149
  */
2137
2150
  setLocalStorageKeyName(keyName) {
2138
2151
  if (typeof keyName !== "string") {
2139
- throw new Error("参数keyName必须是string类型");
2152
+ throw new TypeError("参数keyName必须是string类型");
2140
2153
  }
2141
2154
  this.MenuHandle.$data.key = keyName;
2142
2155
  }
@@ -5485,7 +5498,7 @@ class DOMUtils {
5485
5498
  }
5486
5499
  const domUtils = new DOMUtils();
5487
5500
 
5488
- const version = "2.9.5";
5501
+ const version = "2.9.7";
5489
5502
 
5490
5503
  class Utils {
5491
5504
  windowApi;