@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.
- package/dist/index.amd.js +35 -25
- package/dist/index.amd.js.map +1 -1
- package/dist/index.amd.min.js +1 -1
- package/dist/index.amd.min.js.map +1 -1
- package/dist/index.cjs.js +35 -25
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.cjs.min.js +1 -1
- package/dist/index.cjs.min.js.map +1 -1
- package/dist/index.esm.js +35 -25
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.iife.js +35 -25
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.iife.min.js.map +1 -1
- package/dist/index.system.js +35 -25
- package/dist/index.system.js.map +1 -1
- package/dist/index.system.min.js +1 -1
- package/dist/index.system.min.js.map +1 -1
- package/dist/index.umd.js +35 -25
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/types/src/Utils.d.ts +15 -5
- package/package.json +1 -1
- package/src/Utils.ts +53 -36
|
@@ -1205,7 +1205,7 @@ declare class Utils {
|
|
|
1205
1205
|
/**
|
|
1206
1206
|
* 数组按照内部某个值的大小比对排序,该函数会改变原数组
|
|
1207
1207
|
* @param data 数据|获取数据的方法
|
|
1208
|
-
* @param
|
|
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[],
|
|
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): (
|
|
1571
|
-
createFunction(param: string, code: string): (
|
|
1572
|
-
createFunction<
|
|
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
package/src/Utils.ts
CHANGED
|
@@ -2959,7 +2959,7 @@ class Utils {
|
|
|
2959
2959
|
/**
|
|
2960
2960
|
* 数组按照内部某个值的大小比对排序,该函数会改变原数组
|
|
2961
2961
|
* @param data 数据|获取数据的方法
|
|
2962
|
-
* @param
|
|
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
|
-
|
|
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
|
|
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
|
|
2988
|
-
return typeof
|
|
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
|
|
2993
|
-
* @param
|
|
3000
|
+
* number类型排序方法
|
|
3001
|
+
* @param afterInst
|
|
3002
|
+
* @param beforeInst
|
|
2994
3003
|
*/
|
|
2995
|
-
const sortFunc = function (
|
|
2996
|
-
const beforeValue =
|
|
2997
|
-
const afterValue =
|
|
3004
|
+
const sortFunc = function (afterInst: any, beforeInst: any) {
|
|
3005
|
+
const beforeValue = getTargetValue(beforeInst); /* 前 */
|
|
3006
|
+
const afterValue = getTargetValue(afterInst); /* 后 */
|
|
2998
3007
|
if (sortByDesc) {
|
|
2999
|
-
|
|
3008
|
+
// 降序
|
|
3009
|
+
// 5、4、3、2、1
|
|
3010
|
+
if (beforeValue < afterValue) {
|
|
3000
3011
|
return -1;
|
|
3001
|
-
} else if (
|
|
3012
|
+
} else if (beforeValue > afterValue) {
|
|
3002
3013
|
return 1;
|
|
3003
3014
|
} else {
|
|
3004
3015
|
return 0;
|
|
3005
3016
|
}
|
|
3006
3017
|
} else {
|
|
3007
|
-
|
|
3018
|
+
// 升序
|
|
3019
|
+
// 1、2、3、4、5
|
|
3020
|
+
if (beforeValue > afterValue) {
|
|
3008
3021
|
return -1;
|
|
3009
|
-
} else if (
|
|
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
|
|
3024
|
-
for (let
|
|
3025
|
-
const beforeNode = nodeList[
|
|
3026
|
-
const afterNode = nodeList[
|
|
3027
|
-
const beforeValue =
|
|
3028
|
-
const afterValue =
|
|
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
|
-
|
|
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 = (<
|
|
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): (
|
|
3803
|
-
createFunction(param: string, code: string): (
|
|
3804
|
-
createFunction<
|
|
3805
|
-
|
|
3806
|
-
|
|
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
|
|
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);
|