@whitesev/utils 2.9.1 → 2.9.3

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.
@@ -1205,7 +1205,7 @@ declare class Utils {
1205
1205
  /**
1206
1206
  * 数组按照内部某个值的大小比对排序,该函数会改变原数组
1207
1207
  * @param data 数据|获取数据的方法
1208
- * @param getPropertyValueFunc 数组内部项的某个属性的值的方法,参数为这个项
1208
+ * @param getComparePropertyValue 获取想要比较的属性,仅为number|string类型
1209
1209
  * @param sortByDesc (可选)排序方式
1210
1210
  * + true (默认)倒序(值最大排第一个,如:6、5、4、3...)
1211
1211
  * + false 升序(值最小排第一个,如:1、2、3、4...)
@@ -1216,8 +1216,11 @@ declare class Utils {
1216
1216
  * @example
1217
1217
  * Utils.sortListByProperty([{"time":"2022-1-1"},{"time":"2022-2-2"}],(item)=>{return item["time"]},false)
1218
1218
  * > [{time: '2022-1-1'},{time: '2022-2-2'}]
1219
+ * @example
1220
+ * // 元素排序
1221
+ * Utils.sortListByProperty( () => document.querySelectorAll("a"), it => it.getAttribute("data-index"))
1219
1222
  **/
1220
- sortListByProperty<T>(data: T[], getPropertyValueFunc: string | ((value: T) => any), sortByDesc?: boolean): T[];
1223
+ sortListByProperty<T>(data: T[] | (() => T[]), getComparePropertyValue: string | ((value: T) => number | string), sortByDesc?: boolean): T[];
1221
1224
  /**
1222
1225
  * 字符串首字母转大写
1223
1226
  * @param targetString 目标字符串
@@ -1567,9 +1570,16 @@ declare class Utils {
1567
1570
  * fn(4,5);
1568
1571
  * > 9
1569
1572
  */
1570
- createFunction(code: string): (...args: any[]) => any;
1571
- createFunction(param: string, code: string): (...args: any[]) => any;
1572
- createFunction<A extends string[], T extends boolean>(...params: [...A, code: string, isAsync: T]): T extends true ? (...params: any[]) => Promise<any> : (...args: any[]) => any;
1573
+ createFunction<R = any>(code: string): () => R;
1574
+ createFunction<R = any>(param: string, code: string): (param: any) => R;
1575
+ createFunction<P extends string[]>(...params: [...P, code: string]): (...args: {
1576
+ [K in keyof P]: any;
1577
+ }) => any;
1578
+ createFunction<P extends string[], T extends boolean>(...params: [...P, code: string, isAsync: T]): T extends true ? (...params: {
1579
+ [K in keyof P]: any;
1580
+ }) => Promise<any> : (...args: {
1581
+ [K in keyof P]: any;
1582
+ }) => any;
1573
1583
  }
1574
1584
  declare const utils: Utils;
1575
1585
  export { utils as Utils };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/utils",
4
- "version": "2.9.1",
4
+ "version": "2.9.3",
5
5
  "type": "module",
6
6
  "description": "一个常用的工具库",
7
7
  "main": "dist/index.cjs.js",
package/src/Utils.ts CHANGED
@@ -2959,7 +2959,7 @@ class Utils {
2959
2959
  /**
2960
2960
  * 数组按照内部某个值的大小比对排序,该函数会改变原数组
2961
2961
  * @param data 数据|获取数据的方法
2962
- * @param getPropertyValueFunc 数组内部项的某个属性的值的方法,参数为这个项
2962
+ * @param getComparePropertyValue 获取想要比较的属性,仅为number|string类型
2963
2963
  * @param sortByDesc (可选)排序方式
2964
2964
  * + true (默认)倒序(值最大排第一个,如:6、5、4、3...)
2965
2965
  * + false 升序(值最小排第一个,如:1、2、3、4...)
@@ -2970,43 +2970,56 @@ class Utils {
2970
2970
  * @example
2971
2971
  * Utils.sortListByProperty([{"time":"2022-1-1"},{"time":"2022-2-2"}],(item)=>{return item["time"]},false)
2972
2972
  * > [{time: '2022-1-1'},{time: '2022-2-2'}]
2973
+ * @example
2974
+ * // 元素排序
2975
+ * Utils.sortListByProperty( () => document.querySelectorAll("a"), it => it.getAttribute("data-index"))
2973
2976
  **/
2974
- sortListByProperty<T>(data: T[], getPropertyValueFunc: string | ((value: T) => any), sortByDesc?: boolean): T[];
2975
2977
  sortListByProperty<T>(
2976
- data: T[],
2977
- getPropertyValueFunc: string | ((value: T) => any),
2978
+ data: T[] | (() => T[]),
2979
+ getComparePropertyValue: string | ((value: T) => number | string),
2980
+ sortByDesc?: boolean
2981
+ ): T[];
2982
+ sortListByProperty<T>(
2983
+ data: T[] | (() => T[]),
2984
+ getComparePropertyValue: string | ((value: T) => number | string),
2978
2985
  sortByDesc: boolean = true
2979
2986
  ): T[] {
2980
2987
  const that = this;
2981
- if (typeof getPropertyValueFunc !== "function" && typeof getPropertyValueFunc !== "string") {
2988
+ if (typeof getComparePropertyValue !== "function" && typeof getComparePropertyValue !== "string") {
2982
2989
  throw new Error("Utils.sortListByProperty 参数 getPropertyValueFunc 必须为 function|string 类型");
2983
2990
  }
2984
2991
  if (typeof sortByDesc !== "boolean") {
2985
2992
  throw new Error("Utils.sortListByProperty 参数 sortByDesc 必须为 boolean 类型");
2986
2993
  }
2987
- const getObjValue = function (obj: any) {
2988
- return typeof getPropertyValueFunc === "string" ? obj[getPropertyValueFunc] : getPropertyValueFunc(obj);
2994
+ const getTargetValue = function (target: any): number | string {
2995
+ return typeof getComparePropertyValue === "string"
2996
+ ? target[getComparePropertyValue]
2997
+ : getComparePropertyValue(target);
2989
2998
  };
2990
2999
  /**
2991
- * 排序方法
2992
- * @param after_obj
2993
- * @param before_obj
3000
+ * number类型排序方法
3001
+ * @param afterInst
3002
+ * @param beforeInst
2994
3003
  */
2995
- const sortFunc = function (after_obj: any, before_obj: any) {
2996
- const beforeValue = getObjValue(before_obj); /* 前 */
2997
- const afterValue = getObjValue(after_obj); /* 后 */
3004
+ const sortFunc = function (afterInst: any, beforeInst: any) {
3005
+ const beforeValue = getTargetValue(beforeInst); /* 前 */
3006
+ const afterValue = getTargetValue(afterInst); /* 后 */
2998
3007
  if (sortByDesc) {
2999
- if (afterValue > beforeValue) {
3008
+ // 降序
3009
+ // 5、4、3、2、1
3010
+ if (beforeValue < afterValue) {
3000
3011
  return -1;
3001
- } else if (afterValue < beforeValue) {
3012
+ } else if (beforeValue > afterValue) {
3002
3013
  return 1;
3003
3014
  } else {
3004
3015
  return 0;
3005
3016
  }
3006
3017
  } else {
3007
- if (afterValue < beforeValue) {
3018
+ // 升序
3019
+ // 1、2、3、4、5
3020
+ if (beforeValue > afterValue) {
3008
3021
  return -1;
3009
- } else if (afterValue > beforeValue) {
3022
+ } else if (beforeValue < afterValue) {
3010
3023
  return 1;
3011
3024
  } else {
3012
3025
  return 0;
@@ -3014,18 +3027,18 @@ class Utils {
3014
3027
  }
3015
3028
  };
3016
3029
  /**
3017
- * 排序元素方法
3030
+ * 元素排序方法
3018
3031
  * @param nodeList 元素列表
3019
3032
  * @param getNodeListFunc 获取元素列表的函数
3020
3033
  */
3021
3034
  const sortNodeFunc = function (nodeList: NodeListOf<HTMLElement>, getNodeListFunc: () => NodeListOf<HTMLElement>) {
3022
3035
  const nodeListLength = nodeList.length;
3023
- for (let i = 0; i < nodeListLength - 1; i++) {
3024
- for (let j = 0; j < nodeListLength - 1 - i; j++) {
3025
- const beforeNode = nodeList[j];
3026
- const afterNode = nodeList[j + 1];
3027
- const beforeValue = getObjValue(beforeNode); /* 前 */
3028
- const afterValue = getObjValue(afterNode); /* 后 */
3036
+ for (let index = 0; index < nodeListLength - 1; index++) {
3037
+ for (let index2 = 0; index2 < nodeListLength - 1 - index; index2++) {
3038
+ const beforeNode = nodeList[index2];
3039
+ const afterNode = nodeList[index2 + 1];
3040
+ const beforeValue = getTargetValue(beforeNode); /* 前 */
3041
+ const afterValue = getTargetValue(afterNode); /* 后 */
3029
3042
  if ((sortByDesc == true && beforeValue < afterValue) || (sortByDesc == false && beforeValue > afterValue)) {
3030
3043
  /* 升序/降序 */
3031
3044
  /* 相邻元素两两对比 */
@@ -3043,17 +3056,19 @@ class Utils {
3043
3056
  }
3044
3057
  }
3045
3058
  };
3046
- let result = data;
3047
- let getDataFunc = null;
3059
+ let result = data as T[];
3060
+ let getDataFunc: null | (() => T[]) = null;
3048
3061
  if (data instanceof Function) {
3049
- getDataFunc = data;
3050
- data = (<any>data)();
3062
+ getDataFunc = data as () => T[];
3063
+ const newData: T[] = getDataFunc();
3064
+ data = newData;
3065
+ result = newData;
3051
3066
  }
3052
3067
  if (Array.isArray(data)) {
3053
3068
  data.sort(sortFunc);
3054
3069
  } else if (<any>data instanceof NodeList || that.isJQuery(data)) {
3055
3070
  sortNodeFunc(<any>data, <any>getDataFunc);
3056
- result = (<any>getDataFunc)();
3071
+ result = (<() => T[]>getDataFunc)();
3057
3072
  } else {
3058
3073
  throw new Error("Utils.sortListByProperty 参数 data 必须为 Array|NodeList|jQuery 类型");
3059
3074
  }
@@ -3799,19 +3814,21 @@ class Utils {
3799
3814
  * fn(4,5);
3800
3815
  * > 9
3801
3816
  */
3802
- createFunction(code: string): (...args: any[]) => any;
3803
- createFunction(param: string, code: string): (...args: any[]) => any;
3804
- createFunction<A extends string[], T extends boolean>(
3805
- ...params: [...A, code: string, isAsync: T]
3806
- ): T extends true ? (...params: any[]) => Promise<any> : (...args: any[]) => any;
3817
+ createFunction<R = any>(code: string): () => R;
3818
+ createFunction<R = any>(param: string, code: string): (param: any) => R;
3819
+ createFunction<P extends string[]>(...params: [...P, code: string]): (...args: { [K in keyof P]: any }) => any;
3820
+ createFunction<P extends string[], T extends boolean>(
3821
+ ...params: [...P, code: string, isAsync: T]
3822
+ ): T extends true ? (...params: { [K in keyof P]: any }) => Promise<any> : (...args: { [K in keyof P]: any }) => any;
3807
3823
  createFunction<A extends string[], T extends boolean>(
3808
3824
  ...args: [...A, code: string, isAsync?: T]
3809
3825
  ): T extends true ? (...args: any[]) => Promise<any> : (...args: any[]) => any {
3810
3826
  let isAsync: string | boolean | undefined = args[args.length - 1];
3811
- if (typeof isAsync !== "boolean") {
3827
+ if (typeof isAsync === "boolean") {
3828
+ args.splice(args.length - 1, 1);
3829
+ } else {
3812
3830
  isAsync = false;
3813
3831
  }
3814
- args.splice(args.length - 1, 1);
3815
3832
  if (isAsync) {
3816
3833
  const AsyncFunctionConstructor = Object.getPrototypeOf(async function () {}).constructor;
3817
3834
  return new AsyncFunctionConstructor(...args);