@whitesev/utils 2.9.2 → 2.9.4

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
@@ -4217,9 +4217,35 @@
4217
4217
 
4218
4218
  class UtilsDictionary {
4219
4219
  items;
4220
- constructor(key, value) {
4220
+ constructor(...args) {
4221
4221
  this.items = new Map();
4222
- if (key != null) {
4222
+ if (args.length === 1) {
4223
+ // 数组|对象
4224
+ const data = args[0];
4225
+ if (Array.isArray(data)) {
4226
+ // 数组
4227
+ // [[1,2], [3,4], ...]
4228
+ for (let index = 0; index < data.length; index++) {
4229
+ const item = data[index];
4230
+ if (Array.isArray(item)) {
4231
+ const [key, value] = item;
4232
+ this.set(key, value);
4233
+ }
4234
+ }
4235
+ }
4236
+ else if (typeof data === "object" && data != null) {
4237
+ // 对象
4238
+ // {1:2, 3:4}
4239
+ for (const key in data) {
4240
+ if (Reflect.has(data, key)) {
4241
+ this.set(key, data[key]);
4242
+ }
4243
+ }
4244
+ }
4245
+ }
4246
+ else if (args.length === 2) {
4247
+ // 键、值
4248
+ const [key, value] = args;
4223
4249
  this.set(key, value);
4224
4250
  }
4225
4251
  }
@@ -4235,7 +4261,7 @@
4235
4261
  get entries() {
4236
4262
  const that = this;
4237
4263
  return function* () {
4238
- const itemKeys = Object.keys(that.getItems());
4264
+ const itemKeys = that.keys();
4239
4265
  for (const keyName of itemKeys) {
4240
4266
  yield [keyName, that.get(keyName)];
4241
4267
  }
@@ -4272,7 +4298,7 @@
4272
4298
  */
4273
4299
  set(key, val) {
4274
4300
  if (key === void 0) {
4275
- throw new Error("Utils.Dictionary().set 参数 key 不能为空");
4301
+ throw new Error("Utils.Dictionary().set 参数 key 不能为undefined");
4276
4302
  }
4277
4303
  this.items.set(key, val);
4278
4304
  }
@@ -5478,7 +5504,7 @@
5478
5504
  }
5479
5505
  const domUtils = new DOMUtils();
5480
5506
 
5481
- const version = "2.9.2";
5507
+ const version = "2.9.4";
5482
5508
 
5483
5509
  class Utils {
5484
5510
  windowApi;
@@ -7638,30 +7664,34 @@
7638
7664
  });
7639
7665
  }
7640
7666
  }
7641
- sortListByProperty(data, getPropertyValueFunc, sortByDesc = true) {
7667
+ sortListByProperty(data, getComparePropertyValue, sortByDesc = true) {
7642
7668
  const that = this;
7643
- if (typeof getPropertyValueFunc !== "function" && typeof getPropertyValueFunc !== "string") {
7669
+ if (typeof getComparePropertyValue !== "function" && typeof getComparePropertyValue !== "string") {
7644
7670
  throw new Error("Utils.sortListByProperty 参数 getPropertyValueFunc 必须为 function|string 类型");
7645
7671
  }
7646
7672
  if (typeof sortByDesc !== "boolean") {
7647
7673
  throw new Error("Utils.sortListByProperty 参数 sortByDesc 必须为 boolean 类型");
7648
7674
  }
7649
- const getObjValue = function (obj) {
7650
- return typeof getPropertyValueFunc === "string" ? obj[getPropertyValueFunc] : getPropertyValueFunc(obj);
7675
+ const getTargetValue = function (target) {
7676
+ return typeof getComparePropertyValue === "string"
7677
+ ? target[getComparePropertyValue]
7678
+ : getComparePropertyValue(target);
7651
7679
  };
7652
7680
  /**
7653
- * 排序方法
7654
- * @param after_obj
7655
- * @param before_obj
7681
+ * number类型排序方法
7682
+ * @param afterInst
7683
+ * @param beforeInst
7656
7684
  */
7657
- const sortFunc = function (after_obj, before_obj) {
7658
- const beforeValue = getObjValue(before_obj); /* 前 */
7659
- const afterValue = getObjValue(after_obj); /* 后 */
7685
+ const sortFunc = function (afterInst, beforeInst) {
7686
+ const beforeValue = getTargetValue(beforeInst); /* 前 */
7687
+ const afterValue = getTargetValue(afterInst); /* 后 */
7660
7688
  if (sortByDesc) {
7661
- if (afterValue > beforeValue) {
7689
+ // 降序
7690
+ // 5、4、3、2、1
7691
+ if (beforeValue < afterValue) {
7662
7692
  return -1;
7663
7693
  }
7664
- else if (afterValue < beforeValue) {
7694
+ else if (beforeValue > afterValue) {
7665
7695
  return 1;
7666
7696
  }
7667
7697
  else {
@@ -7669,10 +7699,12 @@
7669
7699
  }
7670
7700
  }
7671
7701
  else {
7672
- if (afterValue < beforeValue) {
7702
+ // 升序
7703
+ // 1、2、3、4、5
7704
+ if (beforeValue > afterValue) {
7673
7705
  return -1;
7674
7706
  }
7675
- else if (afterValue > beforeValue) {
7707
+ else if (beforeValue < afterValue) {
7676
7708
  return 1;
7677
7709
  }
7678
7710
  else {
@@ -7681,18 +7713,18 @@
7681
7713
  }
7682
7714
  };
7683
7715
  /**
7684
- * 排序元素方法
7716
+ * 元素排序方法
7685
7717
  * @param nodeList 元素列表
7686
7718
  * @param getNodeListFunc 获取元素列表的函数
7687
7719
  */
7688
7720
  const sortNodeFunc = function (nodeList, getNodeListFunc) {
7689
7721
  const nodeListLength = nodeList.length;
7690
- for (let i = 0; i < nodeListLength - 1; i++) {
7691
- for (let j = 0; j < nodeListLength - 1 - i; j++) {
7692
- const beforeNode = nodeList[j];
7693
- const afterNode = nodeList[j + 1];
7694
- const beforeValue = getObjValue(beforeNode); /* 前 */
7695
- const afterValue = getObjValue(afterNode); /* 后 */
7722
+ for (let index = 0; index < nodeListLength - 1; index++) {
7723
+ for (let index2 = 0; index2 < nodeListLength - 1 - index; index2++) {
7724
+ const beforeNode = nodeList[index2];
7725
+ const afterNode = nodeList[index2 + 1];
7726
+ const beforeValue = getTargetValue(beforeNode); /* 前 */
7727
+ const afterValue = getTargetValue(afterNode); /* 后 */
7696
7728
  if ((sortByDesc == true && beforeValue < afterValue) || (sortByDesc == false && beforeValue > afterValue)) {
7697
7729
  /* 升序/降序 */
7698
7730
  /* 相邻元素两两对比 */
@@ -7715,7 +7747,9 @@
7715
7747
  let getDataFunc = null;
7716
7748
  if (data instanceof Function) {
7717
7749
  getDataFunc = data;
7718
- data = data();
7750
+ const newData = getDataFunc();
7751
+ data = newData;
7752
+ result = newData;
7719
7753
  }
7720
7754
  if (Array.isArray(data)) {
7721
7755
  data.sort(sortFunc);