@whitesev/utils 2.3.1 → 2.3.2
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 +21 -5
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +21 -5
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +21 -5
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +21 -5
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +21 -5
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +21 -5
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/Utils.d.ts +9 -0
- package/package.json +1 -1
- package/src/Utils.ts +38 -10
|
@@ -1419,6 +1419,15 @@ declare class Utils {
|
|
|
1419
1419
|
* > [{"key": 2}]
|
|
1420
1420
|
**/
|
|
1421
1421
|
uniqueArray<T extends any, TT extends any>(uniqueArrayData?: T[], compareArrayData?: TT[], compareFun?: (item1: T, item2: TT) => boolean): any[];
|
|
1422
|
+
/**
|
|
1423
|
+
* 数组去重,去除不需要的值
|
|
1424
|
+
* @param uniqueArrayData 需要过滤的数组
|
|
1425
|
+
* @param getIdentfierValue 获取用于确定唯一性的值
|
|
1426
|
+
* @example
|
|
1427
|
+
* Utils.uniqueArray([{name:"1",host:"baidu.com"},{name:"2",host:"baidu.com"},{name:"3",host:"baidu.com"}]);
|
|
1428
|
+
* > [{name:"1",host:"baidu.com"}]
|
|
1429
|
+
*/
|
|
1430
|
+
uniqueArray<T>(uniqueArrayData: T[], getIdentfierValue: (itemValue: T) => any): T[];
|
|
1422
1431
|
/**
|
|
1423
1432
|
* 等待函数数组全部执行完毕,注意,每个函数的顺序不是同步
|
|
1424
1433
|
* @param data 需要遍历的数组
|
package/package.json
CHANGED
package/src/Utils.ts
CHANGED
|
@@ -3951,20 +3951,48 @@ class Utils {
|
|
|
3951
3951
|
compareArrayData?: TT[],
|
|
3952
3952
|
compareFun?: (item1: T, item2: TT) => boolean
|
|
3953
3953
|
): any[];
|
|
3954
|
-
|
|
3954
|
+
/**
|
|
3955
|
+
* 数组去重,去除不需要的值
|
|
3956
|
+
* @param uniqueArrayData 需要过滤的数组
|
|
3957
|
+
* @param getIdentfierValue 获取用于确定唯一性的值
|
|
3958
|
+
* @example
|
|
3959
|
+
* Utils.uniqueArray([{name:"1",host:"baidu.com"},{name:"2",host:"baidu.com"},{name:"3",host:"baidu.com"}]);
|
|
3960
|
+
* > [{name:"1",host:"baidu.com"}]
|
|
3961
|
+
*/
|
|
3962
|
+
uniqueArray<T>(
|
|
3963
|
+
uniqueArrayData: T[],
|
|
3964
|
+
getIdentfierValue: (itemValue: T) => any
|
|
3965
|
+
): T[];
|
|
3966
|
+
uniqueArray<T, T2>(
|
|
3955
3967
|
uniqueArrayData: T[] = [],
|
|
3956
|
-
compareArrayData:
|
|
3957
|
-
compareFun: (
|
|
3958
|
-
// @ts-ignore
|
|
3968
|
+
compareArrayData: any,
|
|
3969
|
+
compareFun: any = (item: any, item2: any) => {
|
|
3959
3970
|
return item === item2;
|
|
3960
3971
|
}
|
|
3961
3972
|
): any[] {
|
|
3962
|
-
|
|
3963
|
-
|
|
3964
|
-
|
|
3965
|
-
|
|
3966
|
-
|
|
3967
|
-
|
|
3973
|
+
if (typeof compareArrayData === "function") {
|
|
3974
|
+
const compareFn = compareArrayData;
|
|
3975
|
+
const seen = new Set();
|
|
3976
|
+
|
|
3977
|
+
const result: T[] = [];
|
|
3978
|
+
for (const item of uniqueArrayData) {
|
|
3979
|
+
// 使用compareFn函数来获取当前对象的唯一标识
|
|
3980
|
+
const identfier = compareFn(item);
|
|
3981
|
+
// 如果Set中还没有这个标识,则添加到结果数组中,并将其标识存入Set
|
|
3982
|
+
if (!seen.has(identfier)) {
|
|
3983
|
+
seen.add(identfier);
|
|
3984
|
+
result.push(item);
|
|
3985
|
+
}
|
|
3986
|
+
}
|
|
3987
|
+
return result;
|
|
3988
|
+
} else {
|
|
3989
|
+
return Array.from(uniqueArrayData).filter(
|
|
3990
|
+
(item) =>
|
|
3991
|
+
!Array.from(compareArrayData).some(function (item2) {
|
|
3992
|
+
return compareFun(item, item2);
|
|
3993
|
+
})
|
|
3994
|
+
);
|
|
3995
|
+
}
|
|
3968
3996
|
}
|
|
3969
3997
|
/**
|
|
3970
3998
|
* 等待函数数组全部执行完毕,注意,每个函数的顺序不是同步
|