@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.esm.js CHANGED
@@ -4211,9 +4211,35 @@ class Progress {
4211
4211
 
4212
4212
  class UtilsDictionary {
4213
4213
  items;
4214
- constructor(key, value) {
4214
+ constructor(...args) {
4215
4215
  this.items = new Map();
4216
- if (key != null) {
4216
+ if (args.length === 1) {
4217
+ // 数组|对象
4218
+ const data = args[0];
4219
+ if (Array.isArray(data)) {
4220
+ // 数组
4221
+ // [[1,2], [3,4], ...]
4222
+ for (let index = 0; index < data.length; index++) {
4223
+ const item = data[index];
4224
+ if (Array.isArray(item)) {
4225
+ const [key, value] = item;
4226
+ this.set(key, value);
4227
+ }
4228
+ }
4229
+ }
4230
+ else if (typeof data === "object" && data != null) {
4231
+ // 对象
4232
+ // {1:2, 3:4}
4233
+ for (const key in data) {
4234
+ if (Reflect.has(data, key)) {
4235
+ this.set(key, data[key]);
4236
+ }
4237
+ }
4238
+ }
4239
+ }
4240
+ else if (args.length === 2) {
4241
+ // 键、值
4242
+ const [key, value] = args;
4217
4243
  this.set(key, value);
4218
4244
  }
4219
4245
  }
@@ -4229,7 +4255,7 @@ class UtilsDictionary {
4229
4255
  get entries() {
4230
4256
  const that = this;
4231
4257
  return function* () {
4232
- const itemKeys = Object.keys(that.getItems());
4258
+ const itemKeys = that.keys();
4233
4259
  for (const keyName of itemKeys) {
4234
4260
  yield [keyName, that.get(keyName)];
4235
4261
  }
@@ -4266,7 +4292,7 @@ class UtilsDictionary {
4266
4292
  */
4267
4293
  set(key, val) {
4268
4294
  if (key === void 0) {
4269
- throw new Error("Utils.Dictionary().set 参数 key 不能为空");
4295
+ throw new Error("Utils.Dictionary().set 参数 key 不能为undefined");
4270
4296
  }
4271
4297
  this.items.set(key, val);
4272
4298
  }
@@ -5472,7 +5498,7 @@ class DOMUtils {
5472
5498
  }
5473
5499
  const domUtils = new DOMUtils();
5474
5500
 
5475
- const version = "2.9.2";
5501
+ const version = "2.9.4";
5476
5502
 
5477
5503
  class Utils {
5478
5504
  windowApi;
@@ -7632,30 +7658,34 @@ class Utils {
7632
7658
  });
7633
7659
  }
7634
7660
  }
7635
- sortListByProperty(data, getPropertyValueFunc, sortByDesc = true) {
7661
+ sortListByProperty(data, getComparePropertyValue, sortByDesc = true) {
7636
7662
  const that = this;
7637
- if (typeof getPropertyValueFunc !== "function" && typeof getPropertyValueFunc !== "string") {
7663
+ if (typeof getComparePropertyValue !== "function" && typeof getComparePropertyValue !== "string") {
7638
7664
  throw new Error("Utils.sortListByProperty 参数 getPropertyValueFunc 必须为 function|string 类型");
7639
7665
  }
7640
7666
  if (typeof sortByDesc !== "boolean") {
7641
7667
  throw new Error("Utils.sortListByProperty 参数 sortByDesc 必须为 boolean 类型");
7642
7668
  }
7643
- const getObjValue = function (obj) {
7644
- return typeof getPropertyValueFunc === "string" ? obj[getPropertyValueFunc] : getPropertyValueFunc(obj);
7669
+ const getTargetValue = function (target) {
7670
+ return typeof getComparePropertyValue === "string"
7671
+ ? target[getComparePropertyValue]
7672
+ : getComparePropertyValue(target);
7645
7673
  };
7646
7674
  /**
7647
- * 排序方法
7648
- * @param after_obj
7649
- * @param before_obj
7675
+ * number类型排序方法
7676
+ * @param afterInst
7677
+ * @param beforeInst
7650
7678
  */
7651
- const sortFunc = function (after_obj, before_obj) {
7652
- const beforeValue = getObjValue(before_obj); /* 前 */
7653
- const afterValue = getObjValue(after_obj); /* 后 */
7679
+ const sortFunc = function (afterInst, beforeInst) {
7680
+ const beforeValue = getTargetValue(beforeInst); /* 前 */
7681
+ const afterValue = getTargetValue(afterInst); /* 后 */
7654
7682
  if (sortByDesc) {
7655
- if (afterValue > beforeValue) {
7683
+ // 降序
7684
+ // 5、4、3、2、1
7685
+ if (beforeValue < afterValue) {
7656
7686
  return -1;
7657
7687
  }
7658
- else if (afterValue < beforeValue) {
7688
+ else if (beforeValue > afterValue) {
7659
7689
  return 1;
7660
7690
  }
7661
7691
  else {
@@ -7663,10 +7693,12 @@ class Utils {
7663
7693
  }
7664
7694
  }
7665
7695
  else {
7666
- if (afterValue < beforeValue) {
7696
+ // 升序
7697
+ // 1、2、3、4、5
7698
+ if (beforeValue > afterValue) {
7667
7699
  return -1;
7668
7700
  }
7669
- else if (afterValue > beforeValue) {
7701
+ else if (beforeValue < afterValue) {
7670
7702
  return 1;
7671
7703
  }
7672
7704
  else {
@@ -7675,18 +7707,18 @@ class Utils {
7675
7707
  }
7676
7708
  };
7677
7709
  /**
7678
- * 排序元素方法
7710
+ * 元素排序方法
7679
7711
  * @param nodeList 元素列表
7680
7712
  * @param getNodeListFunc 获取元素列表的函数
7681
7713
  */
7682
7714
  const sortNodeFunc = function (nodeList, getNodeListFunc) {
7683
7715
  const nodeListLength = nodeList.length;
7684
- for (let i = 0; i < nodeListLength - 1; i++) {
7685
- for (let j = 0; j < nodeListLength - 1 - i; j++) {
7686
- const beforeNode = nodeList[j];
7687
- const afterNode = nodeList[j + 1];
7688
- const beforeValue = getObjValue(beforeNode); /* 前 */
7689
- const afterValue = getObjValue(afterNode); /* 后 */
7716
+ for (let index = 0; index < nodeListLength - 1; index++) {
7717
+ for (let index2 = 0; index2 < nodeListLength - 1 - index; index2++) {
7718
+ const beforeNode = nodeList[index2];
7719
+ const afterNode = nodeList[index2 + 1];
7720
+ const beforeValue = getTargetValue(beforeNode); /* 前 */
7721
+ const afterValue = getTargetValue(afterNode); /* 后 */
7690
7722
  if ((sortByDesc == true && beforeValue < afterValue) || (sortByDesc == false && beforeValue > afterValue)) {
7691
7723
  /* 升序/降序 */
7692
7724
  /* 相邻元素两两对比 */
@@ -7709,7 +7741,9 @@ class Utils {
7709
7741
  let getDataFunc = null;
7710
7742
  if (data instanceof Function) {
7711
7743
  getDataFunc = data;
7712
- data = data();
7744
+ const newData = getDataFunc();
7745
+ data = newData;
7746
+ result = newData;
7713
7747
  }
7714
7748
  if (Array.isArray(data)) {
7715
7749
  data.sort(sortFunc);