tools-min-ns 1.5.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/README.md +85 -0
- package/esm/array/ArrayUtil.d.ts +109 -0
- package/esm/array/ArrayUtil.js +242 -0
- package/esm/array/TreeUtil.d.ts +151 -0
- package/esm/array/TreeUtil.js +412 -0
- package/esm/base/BaseUtil.d.ts +91 -0
- package/esm/base/BaseUtil.js +148 -0
- package/esm/browser/BrowserUtil.d.ts +57 -0
- package/esm/browser/BrowserUtil.js +337 -0
- package/esm/browser/fingerprint.d.ts +120 -0
- package/esm/browser/fingerprint.js +1469 -0
- package/esm/check/CheckUtil.d.ts +110 -0
- package/esm/check/CheckUtil.js +465 -0
- package/esm/common.d.ts +43 -0
- package/esm/common.js +44 -0
- package/esm/config.d.ts +7 -0
- package/esm/config.js +7 -0
- package/esm/cookie/CookieUtil.d.ts +56 -0
- package/esm/cookie/CookieUtil.js +12 -0
- package/esm/cookie/cookie.d.ts +2 -0
- package/esm/cookie/cookie.js +104 -0
- package/esm/date/DateUtil.d.ts +261 -0
- package/esm/date/DateUtil.js +763 -0
- package/esm/env/EnvUtil.d.ts +27 -0
- package/esm/env/EnvUtil.js +29 -0
- package/esm/env/env.d.ts +41 -0
- package/esm/env/env.js +106 -0
- package/esm/func/FunctionUtil.d.ts +77 -0
- package/esm/func/FunctionUtil.js +215 -0
- package/esm/index.d.ts +19 -0
- package/esm/index.js +19 -0
- package/esm/number/NumberUtil.d.ts +96 -0
- package/esm/number/NumberUtil.js +256 -0
- package/esm/object/ObjectUtil.d.ts +41 -0
- package/esm/object/ObjectUtil.js +158 -0
- package/esm/pwd/PasswordUtil.d.ts +33 -0
- package/esm/pwd/PasswordUtil.js +68 -0
- package/esm/pwd/crypto.d.ts +6 -0
- package/esm/pwd/crypto.js +1824 -0
- package/esm/request/RequestUtil.d.ts +52 -0
- package/esm/request/RequestUtil.js +53 -0
- package/esm/request/jsonp.d.ts +18 -0
- package/esm/request/jsonp.js +73 -0
- package/esm/request/mgop/env.d.ts +7 -0
- package/esm/request/mgop/env.js +15 -0
- package/esm/request/mgop/index.d.ts +3 -0
- package/esm/request/mgop/index.js +276 -0
- package/esm/request/mgop/responseCode.d.ts +20 -0
- package/esm/request/mgop/responseCode.js +37 -0
- package/esm/request/mgop/type.d.ts +34 -0
- package/esm/request/mgop/type.js +1 -0
- package/esm/request/uniRquest/index.d.ts +2 -0
- package/esm/request/uniRquest/index.js +66 -0
- package/esm/request/uniRquest/miniapp/index.d.ts +2 -0
- package/esm/request/uniRquest/miniapp/index.js +29 -0
- package/esm/request/uniRquest/miniapp-bytedance/index.d.ts +2 -0
- package/esm/request/uniRquest/miniapp-bytedance/index.js +41 -0
- package/esm/request/uniRquest/types.d.ts +65 -0
- package/esm/request/uniRquest/types.js +36 -0
- package/esm/request/uniRquest/utils.d.ts +9 -0
- package/esm/request/uniRquest/utils.js +69 -0
- package/esm/request/uniRquest/web/index.d.ts +2 -0
- package/esm/request/uniRquest/web/index.js +85 -0
- package/esm/request/uniRquest/wechat-miniprogram/index.d.ts +2 -0
- package/esm/request/uniRquest/wechat-miniprogram/index.js +41 -0
- package/esm/request/uniRquest/weex/index.d.ts +14 -0
- package/esm/request/uniRquest/weex/index.js +59 -0
- package/esm/storage/StorageUtil.d.ts +19 -0
- package/esm/storage/StorageUtil.js +75 -0
- package/esm/string/StringUtil.d.ts +392 -0
- package/esm/string/StringUtil.js +556 -0
- package/esm/system/SystemUtil.d.ts +46 -0
- package/esm/system/SystemUtil.js +122 -0
- package/esm/transfer/MapTransferUtil.d.ts +54 -0
- package/esm/transfer/MapTransferUtil.js +160 -0
- package/esm/transfer/TransferUtil.d.ts +98 -0
- package/esm/transfer/TransferUtil.js +323 -0
- package/esm/types.d.ts +25 -0
- package/esm/types.js +1 -0
- package/esm/url/UrlUtil.d.ts +30 -0
- package/esm/url/UrlUtil.js +85 -0
- package/lib/array/ArrayUtil.d.ts +109 -0
- package/lib/array/ArrayUtil.js +252 -0
- package/lib/array/TreeUtil.d.ts +151 -0
- package/lib/array/TreeUtil.js +422 -0
- package/lib/base/BaseUtil.d.ts +91 -0
- package/lib/base/BaseUtil.js +153 -0
- package/lib/browser/BrowserUtil.d.ts +57 -0
- package/lib/browser/BrowserUtil.js +347 -0
- package/lib/browser/fingerprint.d.ts +120 -0
- package/lib/browser/fingerprint.js +1500 -0
- package/lib/check/CheckUtil.d.ts +110 -0
- package/lib/check/CheckUtil.js +475 -0
- package/lib/common.d.ts +43 -0
- package/lib/common.js +50 -0
- package/lib/config.d.ts +7 -0
- package/lib/config.js +12 -0
- package/lib/cookie/CookieUtil.d.ts +56 -0
- package/lib/cookie/CookieUtil.js +22 -0
- package/lib/cookie/cookie.d.ts +2 -0
- package/lib/cookie/cookie.js +109 -0
- package/lib/date/DateUtil.d.ts +261 -0
- package/lib/date/DateUtil.js +773 -0
- package/lib/env/EnvUtil.d.ts +27 -0
- package/lib/env/EnvUtil.js +39 -0
- package/lib/env/env.d.ts +41 -0
- package/lib/env/env.js +112 -0
- package/lib/func/FunctionUtil.d.ts +77 -0
- package/lib/func/FunctionUtil.js +225 -0
- package/lib/index.d.ts +19 -0
- package/lib/index.js +144 -0
- package/lib/number/NumberUtil.d.ts +96 -0
- package/lib/number/NumberUtil.js +266 -0
- package/lib/object/ObjectUtil.d.ts +41 -0
- package/lib/object/ObjectUtil.js +168 -0
- package/lib/pwd/PasswordUtil.d.ts +33 -0
- package/lib/pwd/PasswordUtil.js +78 -0
- package/lib/pwd/crypto.d.ts +6 -0
- package/lib/pwd/crypto.js +1829 -0
- package/lib/request/RequestUtil.d.ts +52 -0
- package/lib/request/RequestUtil.js +94 -0
- package/lib/request/jsonp.d.ts +18 -0
- package/lib/request/jsonp.js +80 -0
- package/lib/request/mgop/env.d.ts +7 -0
- package/lib/request/mgop/env.js +21 -0
- package/lib/request/mgop/index.d.ts +3 -0
- package/lib/request/mgop/index.js +289 -0
- package/lib/request/mgop/responseCode.d.ts +20 -0
- package/lib/request/mgop/responseCode.js +42 -0
- package/lib/request/mgop/type.d.ts +34 -0
- package/lib/request/mgop/type.js +5 -0
- package/lib/request/uniRquest/index.d.ts +2 -0
- package/lib/request/uniRquest/index.js +77 -0
- package/lib/request/uniRquest/miniapp/index.d.ts +2 -0
- package/lib/request/uniRquest/miniapp/index.js +35 -0
- package/lib/request/uniRquest/miniapp-bytedance/index.d.ts +2 -0
- package/lib/request/uniRquest/miniapp-bytedance/index.js +47 -0
- package/lib/request/uniRquest/types.d.ts +65 -0
- package/lib/request/uniRquest/types.js +42 -0
- package/lib/request/uniRquest/utils.d.ts +9 -0
- package/lib/request/uniRquest/utils.js +83 -0
- package/lib/request/uniRquest/web/index.d.ts +2 -0
- package/lib/request/uniRquest/web/index.js +91 -0
- package/lib/request/uniRquest/wechat-miniprogram/index.d.ts +2 -0
- package/lib/request/uniRquest/wechat-miniprogram/index.js +47 -0
- package/lib/request/uniRquest/weex/index.d.ts +14 -0
- package/lib/request/uniRquest/weex/index.js +65 -0
- package/lib/storage/StorageUtil.d.ts +19 -0
- package/lib/storage/StorageUtil.js +85 -0
- package/lib/string/StringUtil.d.ts +392 -0
- package/lib/string/StringUtil.js +566 -0
- package/lib/system/SystemUtil.d.ts +46 -0
- package/lib/system/SystemUtil.js +132 -0
- package/lib/transfer/MapTransferUtil.d.ts +54 -0
- package/lib/transfer/MapTransferUtil.js +165 -0
- package/lib/transfer/TransferUtil.d.ts +98 -0
- package/lib/transfer/TransferUtil.js +333 -0
- package/lib/types.d.ts +25 -0
- package/lib/types.js +5 -0
- package/lib/url/UrlUtil.d.ts +30 -0
- package/lib/url/UrlUtil.js +95 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# use tools-min-ns
|
|
2
|
+
|
|
3
|
+
```
|
|
4
|
+
cnpm i tools-min-ns --save
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
# updates
|
|
8
|
+
|
|
9
|
+
## v1.5.2
|
|
10
|
+
|
|
11
|
+
> StringUtil
|
|
12
|
+
getUuiD 修复参数
|
|
13
|
+
## v1.5.1
|
|
14
|
+
|
|
15
|
+
> DateUtil
|
|
16
|
+
getDateDiff 修复
|
|
17
|
+
|
|
18
|
+
## v1.5.0
|
|
19
|
+
|
|
20
|
+
> BrowserUtil
|
|
21
|
+
|
|
22
|
+
1. 新增 getId获取浏览器指纹
|
|
23
|
+
2. 优化浏览器判断
|
|
24
|
+
|
|
25
|
+
> 新增CookieUtil cookie增删改查
|
|
26
|
+
> 新增EnvUtil 环境判断
|
|
27
|
+
> 新增RequestUtil 请求工具
|
|
28
|
+
|
|
29
|
+
## v1.4.0
|
|
30
|
+
|
|
31
|
+
> PasswordUtil
|
|
32
|
+
|
|
33
|
+
1. 新增 str2MD5加密方法
|
|
34
|
+
|
|
35
|
+
> SystemUtil
|
|
36
|
+
|
|
37
|
+
1. 新增 copyText2Clipboard拷贝文本至剪切板方法
|
|
38
|
+
|
|
39
|
+
## v1.3.4
|
|
40
|
+
|
|
41
|
+
> BrowserUtil
|
|
42
|
+
|
|
43
|
+
1. 修复 浙里办环境判断
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## v1.3.3
|
|
47
|
+
|
|
48
|
+
> UrlUtil
|
|
49
|
+
|
|
50
|
+
1. 修复 setUrlParamsNotRefresh方法
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
## v1.3.2
|
|
54
|
+
|
|
55
|
+
1. 修复url校验
|
|
56
|
+
|
|
57
|
+
## v1.3.1
|
|
58
|
+
|
|
59
|
+
1. 修复url正则
|
|
60
|
+
2. 优化其他
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
## v1.3.0
|
|
64
|
+
|
|
65
|
+
> DateUtil
|
|
66
|
+
|
|
67
|
+
1. 新增 formatDateToFriendly2 日期友好
|
|
68
|
+
2. 新增 isCurrentWeek 判断是否本周
|
|
69
|
+
|
|
70
|
+
## v1.2.0
|
|
71
|
+
|
|
72
|
+
> ArrayUtil
|
|
73
|
+
|
|
74
|
+
1. 新增 randomSortArr 随机排序数组
|
|
75
|
+
|
|
76
|
+
## v1.1.0
|
|
77
|
+
|
|
78
|
+
> TreeUtil
|
|
79
|
+
|
|
80
|
+
1. 新增字符串分割树结构互转
|
|
81
|
+
2. 修改树循环方法的形参
|
|
82
|
+
|
|
83
|
+
## v1.0.0
|
|
84
|
+
|
|
85
|
+
初始化私服
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 数组工具
|
|
3
|
+
* @description
|
|
4
|
+
* @author nanshen
|
|
5
|
+
* @creat 2021-09-15 10:31:35
|
|
6
|
+
*/
|
|
7
|
+
declare namespace ArrayUtil {
|
|
8
|
+
/**
|
|
9
|
+
* 对象数组去重
|
|
10
|
+
* @param array 去重对象
|
|
11
|
+
* @param key 去重key
|
|
12
|
+
* @param isSaveLast 默认true ,true返回重复最后一个对象,false返回重复第一个对象
|
|
13
|
+
* @example
|
|
14
|
+
* const array = [{a:1,b:3},{a:1,b:2},{a:2,b:1}]
|
|
15
|
+
* arrayWeightRemoval(array,'a',true) => [{a:1,b:2},{a:2,b:1}]
|
|
16
|
+
* arrayWeightRemoval(array,'a',false) => [{a:1,b:3},{a:2,b:1}]
|
|
17
|
+
*/
|
|
18
|
+
const arrayWeightRemoval: <T, U>(array: T[] | undefined, key: U extends keyof T ? U : undefined, isSaveLast?: boolean) => T[];
|
|
19
|
+
/**
|
|
20
|
+
* 数组去重
|
|
21
|
+
* @param { (number | string)[]} arr 数组
|
|
22
|
+
* @example
|
|
23
|
+
* unique([1,3,1]) =>[1,3]
|
|
24
|
+
*/
|
|
25
|
+
const unique: (arr: (number | string)[]) => (number | string)[];
|
|
26
|
+
/**
|
|
27
|
+
* 求两个集合的并集
|
|
28
|
+
* @param { (number | string)[]} a 集合a
|
|
29
|
+
* @param { (number | string)[]} b 集合b
|
|
30
|
+
* @example
|
|
31
|
+
* union([1,3,4],[1,2]) => [1,2,3,4]
|
|
32
|
+
*/
|
|
33
|
+
const union: (a: (number | string)[], b: (number | string)[]) => (number | string)[];
|
|
34
|
+
/**
|
|
35
|
+
* 求两个集合的交集
|
|
36
|
+
* @param { (number | string)[]} a 集合a
|
|
37
|
+
* @param { (number | string)[]} b 集合b
|
|
38
|
+
* @example
|
|
39
|
+
* intersect([1,3,4],[1,2]) => [1]
|
|
40
|
+
*/
|
|
41
|
+
const intersect: (a: (number | string)[], b: (number | string)[]) => (number | string)[];
|
|
42
|
+
/**
|
|
43
|
+
* 求两个集合的差集
|
|
44
|
+
* @param { (number | string)[]} a 集合a
|
|
45
|
+
* @param { (number | string)[]} b 集合b
|
|
46
|
+
* @example
|
|
47
|
+
* difference([1,2,3],[1]) => [2,3]
|
|
48
|
+
*/
|
|
49
|
+
const difference: (a: (number | string)[], b: (number | string)[]) => (number | string)[];
|
|
50
|
+
/**
|
|
51
|
+
* 最大值
|
|
52
|
+
* @param {number[]} arr 数组
|
|
53
|
+
* @example
|
|
54
|
+
* max([1,2,3]) => 3
|
|
55
|
+
*/
|
|
56
|
+
const max: (arr: number[]) => number;
|
|
57
|
+
/**
|
|
58
|
+
* 最小值
|
|
59
|
+
* @export
|
|
60
|
+
* @param {number[]} arr 数组
|
|
61
|
+
* @example
|
|
62
|
+
* min([1,2,3]) => 1
|
|
63
|
+
*/
|
|
64
|
+
const min: (arr: number[]) => number;
|
|
65
|
+
/**
|
|
66
|
+
* 求和
|
|
67
|
+
* @export
|
|
68
|
+
* @param {number[]} arr 数字数组
|
|
69
|
+
* @example
|
|
70
|
+
* sum([1,2]) => 3
|
|
71
|
+
*/
|
|
72
|
+
const sum: (arr: number[]) => number;
|
|
73
|
+
/**
|
|
74
|
+
* 平均值
|
|
75
|
+
* @export
|
|
76
|
+
* @param {number[]} arr 数字数组
|
|
77
|
+
* @example
|
|
78
|
+
* average([1,2,3]) => 2
|
|
79
|
+
*/
|
|
80
|
+
const average: (arr: number[]) => number;
|
|
81
|
+
/**
|
|
82
|
+
* 删除其中一个元素
|
|
83
|
+
* @export
|
|
84
|
+
* @param {(number | string)[]} arr 数组
|
|
85
|
+
* @param {(number | string)} ele 要删除的元素
|
|
86
|
+
* @example
|
|
87
|
+
* remove([1,2,3],3) => [1,2]
|
|
88
|
+
*/
|
|
89
|
+
const remove: (arr: (number | string)[], ele: number | string) => (number | string)[];
|
|
90
|
+
/**
|
|
91
|
+
* 数组排序
|
|
92
|
+
* @param {number[]} arr 数组
|
|
93
|
+
* @param {number} type 1:从小到大 2:从大到小 3:随机
|
|
94
|
+
* @example
|
|
95
|
+
* sort([1,2,3],1) => [1,2,3]
|
|
96
|
+
* sort([1,2,3],2) => [3,2,1]
|
|
97
|
+
* sort([1,2,3],3) => [1,3,2]
|
|
98
|
+
*/
|
|
99
|
+
const sort: (arr: number[], type?: number) => number[];
|
|
100
|
+
/**
|
|
101
|
+
* 随机排序数组
|
|
102
|
+
* @param arr any[]
|
|
103
|
+
* @example
|
|
104
|
+
* randomSortArr([1,2,3]) => [1,3,2]
|
|
105
|
+
* randomSortArr([{a:1},{a:2},{a:3}]) => [{a:3},{a:1},{a:2}]
|
|
106
|
+
*/
|
|
107
|
+
function randomSortArr<T>(arr: T[]): T[];
|
|
108
|
+
}
|
|
109
|
+
export default ArrayUtil;
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 数组工具
|
|
3
|
+
* @description
|
|
4
|
+
* @author nanshen
|
|
5
|
+
* @creat 2021-09-15 10:31:35
|
|
6
|
+
*/
|
|
7
|
+
var __read = this && this.__read || function (o, n) {
|
|
8
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
9
|
+
if (!m) return o;
|
|
10
|
+
var i = m.call(o),
|
|
11
|
+
r,
|
|
12
|
+
ar = [],
|
|
13
|
+
e;
|
|
14
|
+
try {
|
|
15
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
e = {
|
|
18
|
+
error: error
|
|
19
|
+
};
|
|
20
|
+
} finally {
|
|
21
|
+
try {
|
|
22
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
23
|
+
} finally {
|
|
24
|
+
if (e) throw e.error;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return ar;
|
|
28
|
+
};
|
|
29
|
+
var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
|
|
30
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
+
if (ar || !(i in from)) {
|
|
32
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
+
ar[i] = from[i];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
+
};
|
|
38
|
+
import NumberUtil from '../number/NumberUtil';
|
|
39
|
+
var ArrayUtil;
|
|
40
|
+
(function (ArrayUtil) {
|
|
41
|
+
/**
|
|
42
|
+
* 对象数组去重
|
|
43
|
+
* @param array 去重对象
|
|
44
|
+
* @param key 去重key
|
|
45
|
+
* @param isSaveLast 默认true ,true返回重复最后一个对象,false返回重复第一个对象
|
|
46
|
+
* @example
|
|
47
|
+
* const array = [{a:1,b:3},{a:1,b:2},{a:2,b:1}]
|
|
48
|
+
* arrayWeightRemoval(array,'a',true) => [{a:1,b:2},{a:2,b:1}]
|
|
49
|
+
* arrayWeightRemoval(array,'a',false) => [{a:1,b:3},{a:2,b:1}]
|
|
50
|
+
*/
|
|
51
|
+
ArrayUtil.arrayWeightRemoval = function (array, key, isSaveLast) {
|
|
52
|
+
var _a;
|
|
53
|
+
if (array === void 0) {
|
|
54
|
+
array = [];
|
|
55
|
+
}
|
|
56
|
+
if (isSaveLast === void 0) {
|
|
57
|
+
isSaveLast = true;
|
|
58
|
+
}
|
|
59
|
+
if (!array.length || key === undefined || ((_a = array === null || array === void 0 ? void 0 : array[0]) === null || _a === void 0 ? void 0 : _a[key]) === undefined) {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
var arrayMap = new Map();
|
|
63
|
+
var region = [];
|
|
64
|
+
array.forEach(function (element) {
|
|
65
|
+
if (isSaveLast) {
|
|
66
|
+
arrayMap.set(element[key], element);
|
|
67
|
+
} else {
|
|
68
|
+
if (!arrayMap.has(element[key])) arrayMap.set(element[key], element);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
arrayMap.forEach(function (ele) {
|
|
72
|
+
if (ele) {
|
|
73
|
+
region.push(ele);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return region;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* 数组去重
|
|
80
|
+
* @param { (number | string)[]} arr 数组
|
|
81
|
+
* @example
|
|
82
|
+
* unique([1,3,1]) =>[1,3]
|
|
83
|
+
*/
|
|
84
|
+
ArrayUtil.unique = function (arr) {
|
|
85
|
+
if (!(arr === null || arr === void 0 ? void 0 : arr.length)) return [];
|
|
86
|
+
var list = __spreadArray([], __read(new Set(arr)), false);
|
|
87
|
+
return list;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* 求两个集合的并集
|
|
91
|
+
* @param { (number | string)[]} a 集合a
|
|
92
|
+
* @param { (number | string)[]} b 集合b
|
|
93
|
+
* @example
|
|
94
|
+
* union([1,3,4],[1,2]) => [1,2,3,4]
|
|
95
|
+
*/
|
|
96
|
+
ArrayUtil.union = function (a, b) {
|
|
97
|
+
if (!(a === null || a === void 0 ? void 0 : a.length)) return [];
|
|
98
|
+
if (!(b === null || b === void 0 ? void 0 : b.length)) return [];
|
|
99
|
+
var aArr = new Set(a);
|
|
100
|
+
var bArr = new Set(b);
|
|
101
|
+
var list = __spreadArray([], __read(new Set(__spreadArray(__spreadArray([], __read(aArr), false), __read(bArr), false))), false);
|
|
102
|
+
return list;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* 求两个集合的交集
|
|
106
|
+
* @param { (number | string)[]} a 集合a
|
|
107
|
+
* @param { (number | string)[]} b 集合b
|
|
108
|
+
* @example
|
|
109
|
+
* intersect([1,3,4],[1,2]) => [1]
|
|
110
|
+
*/
|
|
111
|
+
ArrayUtil.intersect = function (a, b) {
|
|
112
|
+
if (!(a === null || a === void 0 ? void 0 : a.length)) return [];
|
|
113
|
+
if (!(b === null || b === void 0 ? void 0 : b.length)) return [];
|
|
114
|
+
var aArr = new Set(a);
|
|
115
|
+
var bArr = new Set(b);
|
|
116
|
+
var list = __spreadArray([], __read(new Set(__spreadArray([], __read(aArr), false).filter(function (x) {
|
|
117
|
+
return bArr.has(x);
|
|
118
|
+
}))), false);
|
|
119
|
+
return list;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* 求两个集合的差集
|
|
123
|
+
* @param { (number | string)[]} a 集合a
|
|
124
|
+
* @param { (number | string)[]} b 集合b
|
|
125
|
+
* @example
|
|
126
|
+
* difference([1,2,3],[1]) => [2,3]
|
|
127
|
+
*/
|
|
128
|
+
ArrayUtil.difference = function (a, b) {
|
|
129
|
+
var aArr = new Set(a);
|
|
130
|
+
var bArr = new Set(b);
|
|
131
|
+
var list = __spreadArray([], __read(new Set(__spreadArray([], __read(aArr), false).filter(function (x) {
|
|
132
|
+
return !bArr.has(x);
|
|
133
|
+
}))), false);
|
|
134
|
+
return list;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* 最大值
|
|
138
|
+
* @param {number[]} arr 数组
|
|
139
|
+
* @example
|
|
140
|
+
* max([1,2,3]) => 3
|
|
141
|
+
*/
|
|
142
|
+
ArrayUtil.max = function (arr) {
|
|
143
|
+
if (!(arr === null || arr === void 0 ? void 0 : arr.length)) return 0;
|
|
144
|
+
return Math.max.apply(null, arr);
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* 最小值
|
|
148
|
+
* @export
|
|
149
|
+
* @param {number[]} arr 数组
|
|
150
|
+
* @example
|
|
151
|
+
* min([1,2,3]) => 1
|
|
152
|
+
*/
|
|
153
|
+
ArrayUtil.min = function (arr) {
|
|
154
|
+
if (!(arr === null || arr === void 0 ? void 0 : arr.length)) return 0;
|
|
155
|
+
return Math.min.apply(null, arr);
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* 求和
|
|
159
|
+
* @export
|
|
160
|
+
* @param {number[]} arr 数字数组
|
|
161
|
+
* @example
|
|
162
|
+
* sum([1,2]) => 3
|
|
163
|
+
*/
|
|
164
|
+
ArrayUtil.sum = function (arr) {
|
|
165
|
+
return arr.reduce(function (a, b) {
|
|
166
|
+
return NumberUtil.floatAdd(a, b);
|
|
167
|
+
}, 0);
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* 平均值
|
|
171
|
+
* @export
|
|
172
|
+
* @param {number[]} arr 数字数组
|
|
173
|
+
* @example
|
|
174
|
+
* average([1,2,3]) => 2
|
|
175
|
+
*/
|
|
176
|
+
ArrayUtil.average = function (arr) {
|
|
177
|
+
if (!(arr === null || arr === void 0 ? void 0 : arr.length)) return 0;
|
|
178
|
+
return ArrayUtil.sum(arr) / arr.length;
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* 删除其中一个元素
|
|
182
|
+
* @export
|
|
183
|
+
* @param {(number | string)[]} arr 数组
|
|
184
|
+
* @param {(number | string)} ele 要删除的元素
|
|
185
|
+
* @example
|
|
186
|
+
* remove([1,2,3],3) => [1,2]
|
|
187
|
+
*/
|
|
188
|
+
ArrayUtil.remove = function (arr, ele) {
|
|
189
|
+
if (!(arr === null || arr === void 0 ? void 0 : arr.length)) return [];
|
|
190
|
+
var index = arr.indexOf(ele);
|
|
191
|
+
if (index > -1) {
|
|
192
|
+
arr.splice(index, 1);
|
|
193
|
+
}
|
|
194
|
+
return arr;
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* 数组排序
|
|
198
|
+
* @param {number[]} arr 数组
|
|
199
|
+
* @param {number} type 1:从小到大 2:从大到小 3:随机
|
|
200
|
+
* @example
|
|
201
|
+
* sort([1,2,3],1) => [1,2,3]
|
|
202
|
+
* sort([1,2,3],2) => [3,2,1]
|
|
203
|
+
* sort([1,2,3],3) => [1,3,2]
|
|
204
|
+
*/
|
|
205
|
+
ArrayUtil.sort = function (arr, type) {
|
|
206
|
+
if (type === void 0) {
|
|
207
|
+
type = 1;
|
|
208
|
+
}
|
|
209
|
+
if (!(arr === null || arr === void 0 ? void 0 : arr.length)) return [];
|
|
210
|
+
return arr.sort(function (a, b) {
|
|
211
|
+
switch (type) {
|
|
212
|
+
case 1:
|
|
213
|
+
return a - b;
|
|
214
|
+
case 2:
|
|
215
|
+
return b - a;
|
|
216
|
+
case 3:
|
|
217
|
+
return Math.random() - 0.5;
|
|
218
|
+
default:
|
|
219
|
+
return a - b;
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* 随机排序数组
|
|
225
|
+
* @param arr any[]
|
|
226
|
+
* @example
|
|
227
|
+
* randomSortArr([1,2,3]) => [1,3,2]
|
|
228
|
+
* randomSortArr([{a:1},{a:2},{a:3}]) => [{a:3},{a:1},{a:2}]
|
|
229
|
+
*/
|
|
230
|
+
function randomSortArr(arr) {
|
|
231
|
+
var _a;
|
|
232
|
+
return new Array((_a = arr === null || arr === void 0 ? void 0 : arr.length) !== null && _a !== void 0 ? _a : 0).fill(0).map(function (_, i) {
|
|
233
|
+
return i;
|
|
234
|
+
}).sort(function () {
|
|
235
|
+
return Math.random() - 0.5;
|
|
236
|
+
}).map(function (index) {
|
|
237
|
+
return arr[index];
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
ArrayUtil.randomSortArr = randomSortArr;
|
|
241
|
+
})(ArrayUtil || (ArrayUtil = {}));
|
|
242
|
+
export default ArrayUtil;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 树工具
|
|
3
|
+
* @description
|
|
4
|
+
* @author nanshen
|
|
5
|
+
* @creat 2021-09-15 10:31:35
|
|
6
|
+
*/
|
|
7
|
+
import { FieldNames, TreeData, ValueType } from '../types';
|
|
8
|
+
declare namespace TreeUtil {
|
|
9
|
+
/**
|
|
10
|
+
* @name 循环树改变原数组
|
|
11
|
+
* @param tree tree数组
|
|
12
|
+
* @param callback 回调(item:当前循环项,parent: 父对象)
|
|
13
|
+
* @param children children字段
|
|
14
|
+
* @param defaultParent 默认上级对象
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
function forEachTree<T>(tree: T[], callback: (t: T, p?: T) => void, children?: string, defaultParent?: T): T[];
|
|
18
|
+
/**
|
|
19
|
+
* @name 查找树
|
|
20
|
+
* @param tree tree数组
|
|
21
|
+
* @param callback 回调(item:当前循环项,parent: 父对象)
|
|
22
|
+
* @param children children字段
|
|
23
|
+
* @param defaultParent 默认上级对象
|
|
24
|
+
*/
|
|
25
|
+
function findTreeData<T>(tree: T[], callback: (t: T, p?: T) => boolean, children?: string, defaultParent?: T): T | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* @name 筛选树
|
|
28
|
+
* @param tree tree数组
|
|
29
|
+
* @param callback 回调(item:当前循环项,parent: 父对象)
|
|
30
|
+
* @param children children字段
|
|
31
|
+
* @param defaultParent 默认上级对象
|
|
32
|
+
*/
|
|
33
|
+
function filterTreeData<T>(tree: T[], callback: (t: T, p?: T) => boolean, children?: string, defaultParent?: T): T[];
|
|
34
|
+
/**
|
|
35
|
+
* @name 列表转树
|
|
36
|
+
* @param list 数组
|
|
37
|
+
* @param fieldNames 映射对象
|
|
38
|
+
*/
|
|
39
|
+
function getTreeByList<T>(list: T[], fieldNames?: FieldNames): (T & TreeData)[];
|
|
40
|
+
/**
|
|
41
|
+
* @name 树转列表
|
|
42
|
+
* @param tree 数组
|
|
43
|
+
*/
|
|
44
|
+
function getListByTree<T>(tree: T[], children?: string): T[];
|
|
45
|
+
/**
|
|
46
|
+
* @name 获取树中id路径数组
|
|
47
|
+
* @param tree 树
|
|
48
|
+
* @param id 主键
|
|
49
|
+
* @param fieldNames 映射对象
|
|
50
|
+
*/
|
|
51
|
+
function getIdPathByTree<T>(tree: T[], id: ValueType, fieldNames?: FieldNames): ValueType[];
|
|
52
|
+
/**
|
|
53
|
+
* @name 字符串分割转为树结构
|
|
54
|
+
* @param str string
|
|
55
|
+
* @param splitStr /
|
|
56
|
+
* @param fieldNames 映射对象
|
|
57
|
+
* @example
|
|
58
|
+
*
|
|
59
|
+
* `一级选项 1/二级选项 1/三级选项 1
|
|
60
|
+
一级选项 1/二级选项 1/三级选项 2
|
|
61
|
+
一级选项 2/二级选项 1`
|
|
62
|
+
//转换后
|
|
63
|
+
[
|
|
64
|
+
{
|
|
65
|
+
name: '一级选项 1',
|
|
66
|
+
id: '一级选项 1',
|
|
67
|
+
children: [
|
|
68
|
+
{
|
|
69
|
+
name: '二级选项 1',
|
|
70
|
+
id: '二级选项 1',
|
|
71
|
+
pid: '一级选项 1',
|
|
72
|
+
children: [
|
|
73
|
+
{
|
|
74
|
+
name: '三级选项 1',
|
|
75
|
+
id: '三级选项 1',
|
|
76
|
+
pid: '二级选项 1',
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: '三级选项 2',
|
|
80
|
+
id: '三级选项 2',
|
|
81
|
+
pid: '二级选项 1',
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
name: '一级选项 2',
|
|
89
|
+
id: '一级选项 2',
|
|
90
|
+
children: [
|
|
91
|
+
{
|
|
92
|
+
name: '二级选项 1',
|
|
93
|
+
id: '二级选项 1',
|
|
94
|
+
pid: '一级选项 2',
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
},
|
|
98
|
+
]
|
|
99
|
+
*/
|
|
100
|
+
function strSplit2tree(str: string, splitStr?: string, fieldNames?: FieldNames): Record<string, any>[];
|
|
101
|
+
/**
|
|
102
|
+
* @name 树结构转为字符串分割
|
|
103
|
+
* @param tree 树
|
|
104
|
+
* @param splitStr /
|
|
105
|
+
* @param fieldNames 映射对象
|
|
106
|
+
* @example
|
|
107
|
+
*
|
|
108
|
+
* [
|
|
109
|
+
{
|
|
110
|
+
name: '一级选项 1',
|
|
111
|
+
id: '一级选项 1',
|
|
112
|
+
children: [
|
|
113
|
+
{
|
|
114
|
+
name: '二级选项 1',
|
|
115
|
+
id: '二级选项 1',
|
|
116
|
+
pid: '一级选项 1',
|
|
117
|
+
children: [
|
|
118
|
+
{
|
|
119
|
+
name: '三级选项 1',
|
|
120
|
+
id: '三级选项 1',
|
|
121
|
+
pid: '二级选项 1',
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: '三级选项 2',
|
|
125
|
+
id: '三级选项 2',
|
|
126
|
+
pid: '二级选项 1',
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
name: '一级选项 2',
|
|
134
|
+
id: '一级选项 2',
|
|
135
|
+
children: [
|
|
136
|
+
{
|
|
137
|
+
name: '二级选项 1',
|
|
138
|
+
id: '二级选项 1',
|
|
139
|
+
pid: '一级选项 2',
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
},
|
|
143
|
+
]
|
|
144
|
+
//转换后
|
|
145
|
+
`一级选项 1/二级选项 1/三级选项 1
|
|
146
|
+
一级选项 1/二级选项 1/三级选项 2
|
|
147
|
+
一级选项 2/二级选项 1`
|
|
148
|
+
*/
|
|
149
|
+
function tree2strSplit<T>(tree: T[], splitStr?: string, fieldNames?: FieldNames): string;
|
|
150
|
+
}
|
|
151
|
+
export default TreeUtil;
|