@mudbean/utils 2.0.5
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/CHANGELOG.md +170 -0
- package/LICENSE +15 -0
- package/README.md +51 -0
- package/cjs/array/difference.js +48 -0
- package/cjs/array/index.js +133 -0
- package/cjs/array/intersection.js +40 -0
- package/cjs/array/symmetricDifference.js +45 -0
- package/cjs/array/union.js +62 -0
- package/cjs/className.js +55 -0
- package/cjs/createBezier.js +135 -0
- package/cjs/getRandomNumber.js +70 -0
- package/cjs/getRandomString.js +143 -0
- package/cjs/index.js +40 -0
- package/cjs/isNode.js +23 -0
- package/cjs/object/createConstructor.js +59 -0
- package/cjs/performance.js +138 -0
- package/cjs/regexp/autoEscapedRegExp.js +44 -0
- package/cjs/regexp/escapeRegExp.js +22 -0
- package/cjs/regexp/parse.js +31 -0
- package/cjs/sleep.js +37 -0
- package/es/array/difference.d.ts +29 -0
- package/es/array/difference.js +46 -0
- package/es/array/index.d.ts +125 -0
- package/es/array/index.js +127 -0
- package/es/array/intersection.d.ts +17 -0
- package/es/array/intersection.js +38 -0
- package/es/array/symmetricDifference.d.ts +27 -0
- package/es/array/symmetricDifference.js +43 -0
- package/es/array/union.d.ts +39 -0
- package/es/array/union.js +60 -0
- package/es/className.d.ts +26 -0
- package/es/className.js +52 -0
- package/es/createBezier.d.ts +64 -0
- package/es/createBezier.js +133 -0
- package/es/getRandomNumber.d.ts +24 -0
- package/es/getRandomNumber.js +67 -0
- package/es/getRandomString.d.ts +73 -0
- package/es/getRandomString.js +141 -0
- package/es/index.d.ts +12 -0
- package/es/index.js +15 -0
- package/es/isNode.d.ts +8 -0
- package/es/isNode.js +20 -0
- package/es/object/createConstructor.d.ts +47 -0
- package/es/object/createConstructor.js +56 -0
- package/es/object/index.d.ts +2 -0
- package/es/performance.d.ts +61 -0
- package/es/performance.js +135 -0
- package/es/regexp/autoEscapedRegExp.d.ts +28 -0
- package/es/regexp/autoEscapedRegExp.js +42 -0
- package/es/regexp/escapeRegExp.d.ts +16 -0
- package/es/regexp/escapeRegExp.js +20 -0
- package/es/regexp/index.d.ts +2 -0
- package/es/regexp/parse.d.ts +6 -0
- package/es/regexp/parse.js +29 -0
- package/es/regexp/types.d.ts +10 -0
- package/es/sleep.d.ts +25 -0
- package/es/sleep.js +35 -0
- package/package.json +118 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var is = require('@mudbean/is');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* # 解析 options
|
|
7
|
+
* @param options
|
|
8
|
+
*/
|
|
9
|
+
function parse(options) {
|
|
10
|
+
// 处理 options
|
|
11
|
+
if (is.isString(options)) {
|
|
12
|
+
options = {
|
|
13
|
+
flags: options,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
// 处理 flags
|
|
17
|
+
if (!is.isString(options.flags))
|
|
18
|
+
options.flags = '';
|
|
19
|
+
else {
|
|
20
|
+
// 需求是保留字符串中的某一部分,使用
|
|
21
|
+
const regexp = /[migsuy]/g;
|
|
22
|
+
const matchResult = options.flags.match(regexp);
|
|
23
|
+
if (is.isNull(matchResult))
|
|
24
|
+
options.flags = '';
|
|
25
|
+
else
|
|
26
|
+
options.flags = [...new Set(matchResult)].join('');
|
|
27
|
+
}
|
|
28
|
+
return options;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
exports.parse = parse;
|
package/cjs/sleep.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var is = require('@mudbean/is');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* # 线程休息
|
|
7
|
+
*
|
|
8
|
+
* 但从调用到执行完毕总是与期望的时间并不相吻合,除非执行是线型的(也不保证时间的严格性)
|
|
9
|
+
*
|
|
10
|
+
* - 宏任务:整体代码、setTimeout、DOM 事件回调、requestAnimationFrame、setImmediate、setInterval、I/O操作、UI渲染等
|
|
11
|
+
* - 微任务:Promise的then/catch/finally、process.nextTick(Node.js)、MutationObserver、queueMicrotask(显示添加微任务)等
|
|
12
|
+
*
|
|
13
|
+
* <span style="color:#ff0;">*Node.js 中的 process.nextTick 优先级高于其他微任务*</span>
|
|
14
|
+
*
|
|
15
|
+
* *如果参数为非法数值,将会抛出 TypeError *
|
|
16
|
+
*
|
|
17
|
+
* @param delay 睡觉时长(机器时间,毫秒为单位)
|
|
18
|
+
* @returns 🈳
|
|
19
|
+
* @example
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* import { sleep } from '@mudbean/utils';
|
|
23
|
+
*
|
|
24
|
+
* console.log(Date.now()); // 1748058118471
|
|
25
|
+
* await sleep(1000);
|
|
26
|
+
* console.log(Date.now()); // 1748058119473
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
async function sleep(delay = 1000) {
|
|
30
|
+
if (!isFinite(delay) || delay < 4)
|
|
31
|
+
throw new TypeError('delay 应该是一个正常的数值');
|
|
32
|
+
if (is.isZero(delay))
|
|
33
|
+
return Promise.resolve();
|
|
34
|
+
return new Promise(resolve => setTimeout(resolve, delay));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
exports.sleep = sleep;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ## 求给出的两个数组的差值(A - B)
|
|
3
|
+
*
|
|
4
|
+
* @param a - 第一个数组
|
|
5
|
+
* @param b - 第二个数组
|
|
6
|
+
* @throws {TypeError} 当两个参数有一个不是
|
|
7
|
+
* @description 当两个参数有一个不是数组时将抛出
|
|
8
|
+
* @returns 返回第一个参数相对第二个参数的差值
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { difference} from '@mudbean/utils';
|
|
12
|
+
*
|
|
13
|
+
* const log = console.log;
|
|
14
|
+
*
|
|
15
|
+
* log(difference([], [1, 2, 3])); // []
|
|
16
|
+
*
|
|
17
|
+
* log([1, 2, 3], []); //[1, 2, 3]
|
|
18
|
+
*
|
|
19
|
+
* log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
|
|
20
|
+
*
|
|
21
|
+
* // 将抛出 TypeError
|
|
22
|
+
* log(difference([1, 2, 3], 'a'));
|
|
23
|
+
* log(difference([1, 2, 3], 1));
|
|
24
|
+
* log(difference([1, 2, 3], undefined));
|
|
25
|
+
* log(difference([1, 2, 3], null));
|
|
26
|
+
* log(difference([1, 2, 3], true));
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function difference<T>(a: T[], b: T[]): T[];
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { isArray, isEmptyArray } from '@mudbean/is';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ## 求给出的两个数组的差值(A - B)
|
|
5
|
+
*
|
|
6
|
+
* @param a - 第一个数组
|
|
7
|
+
* @param b - 第二个数组
|
|
8
|
+
* @throws {TypeError} 当两个参数有一个不是
|
|
9
|
+
* @description 当两个参数有一个不是数组时将抛出
|
|
10
|
+
* @returns 返回第一个参数相对第二个参数的差值
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { difference} from '@mudbean/utils';
|
|
14
|
+
*
|
|
15
|
+
* const log = console.log;
|
|
16
|
+
*
|
|
17
|
+
* log(difference([], [1, 2, 3])); // []
|
|
18
|
+
*
|
|
19
|
+
* log([1, 2, 3], []); //[1, 2, 3]
|
|
20
|
+
*
|
|
21
|
+
* log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
|
|
22
|
+
*
|
|
23
|
+
* // 将抛出 TypeError
|
|
24
|
+
* log(difference([1, 2, 3], 'a'));
|
|
25
|
+
* log(difference([1, 2, 3], 1));
|
|
26
|
+
* log(difference([1, 2, 3], undefined));
|
|
27
|
+
* log(difference([1, 2, 3], null));
|
|
28
|
+
* log(difference([1, 2, 3], true));
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
function difference(a, b) {
|
|
32
|
+
if (!isArray(a) || !isArray(b)) {
|
|
33
|
+
throw new TypeError('参数需为数组');
|
|
34
|
+
}
|
|
35
|
+
if (isEmptyArray(a) || isEmptyArray(b)) {
|
|
36
|
+
return a;
|
|
37
|
+
}
|
|
38
|
+
/** 获取两个数组中长度较小的 */
|
|
39
|
+
// 参数有顺序要求
|
|
40
|
+
// const [shorter, longer] = a.length > b.length ? [b, a] : [a, b];
|
|
41
|
+
// const shorterSet = new Set(shorter);
|
|
42
|
+
const bSet = new Set(b);
|
|
43
|
+
return a.filter(i => !bSet.has(i));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { difference };
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { intersection } from './intersection';
|
|
2
|
+
import { union } from './union';
|
|
3
|
+
import { difference } from './difference';
|
|
4
|
+
import { symmetricDifference } from './symmetricDifference';
|
|
5
|
+
export { union, intersection, difference, symmetricDifference };
|
|
6
|
+
/**
|
|
7
|
+
* # 数组的一些方法
|
|
8
|
+
*
|
|
9
|
+
* - `union` 两个数组的并集(排除共有项)
|
|
10
|
+
* - `intersection` 两个数组的交集(共有项)
|
|
11
|
+
* - `difference` 两个数组的差集 (A - B)
|
|
12
|
+
* - `symmetricDifference` 对称差集 ( A △ B)
|
|
13
|
+
*/
|
|
14
|
+
export declare const enArr: {
|
|
15
|
+
/**
|
|
16
|
+
* ## 数组的并集
|
|
17
|
+
* <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
|
|
18
|
+
* @param arrays - 多个数组
|
|
19
|
+
* @returns 联合后的数组
|
|
20
|
+
* @example
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { union } from '@mudbean/utils';
|
|
24
|
+
*
|
|
25
|
+
* const log = console.log;
|
|
26
|
+
*
|
|
27
|
+
* // []
|
|
28
|
+
* log(union());
|
|
29
|
+
*
|
|
30
|
+
* // [1, 2, 3]
|
|
31
|
+
* log(union([1, 2, 3]));
|
|
32
|
+
*
|
|
33
|
+
* // TypeError
|
|
34
|
+
* log(union([1, 2, 3], 'i'));
|
|
35
|
+
* log(union([1, 2, 3], undefined));
|
|
36
|
+
* log(union([1, 2, 3], null));
|
|
37
|
+
* log(union([1, 2, 3], 4));
|
|
38
|
+
* log(union([1, 2, 3], {}));
|
|
39
|
+
* log(union([1, 2, 3], false));
|
|
40
|
+
*
|
|
41
|
+
* // [1, 2, 3, 4, 6]
|
|
42
|
+
* log(union([1, 2, 3], [2, 4, 6]));
|
|
43
|
+
*
|
|
44
|
+
* // [1, 2, 3, 4, 6]
|
|
45
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
|
|
46
|
+
*
|
|
47
|
+
* // [1, 2, 3, 4, 6]
|
|
48
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
union: typeof union;
|
|
52
|
+
/**
|
|
53
|
+
* ## 两个数组的交集
|
|
54
|
+
*
|
|
55
|
+
* @param a 数组 1️⃣
|
|
56
|
+
* @param b 数组 2️⃣
|
|
57
|
+
* @returns 返回两个数组的交集
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { intersection } from '@mudbean/utils';
|
|
61
|
+
*
|
|
62
|
+
* const log = console.log;
|
|
63
|
+
*
|
|
64
|
+
* log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
|
|
65
|
+
* log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
intersection: typeof intersection;
|
|
69
|
+
/**
|
|
70
|
+
* ## 求给出的两个数组的差值(A - B)
|
|
71
|
+
*
|
|
72
|
+
* @param a - 第一个数组
|
|
73
|
+
* @param b - 第二个数组
|
|
74
|
+
* @throws {TypeError} 当两个参数有一个不是
|
|
75
|
+
* @description 当两个参数有一个不是数组时将抛出
|
|
76
|
+
* @returns 返回第一个参数相对第二个参数的差值
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* import { difference} from '@mudbean/utils';
|
|
80
|
+
*
|
|
81
|
+
* const log = console.log;
|
|
82
|
+
*
|
|
83
|
+
* log(difference([], [1, 2, 3])); // []
|
|
84
|
+
*
|
|
85
|
+
* log([1, 2, 3], []); //[1, 2, 3]
|
|
86
|
+
*
|
|
87
|
+
* log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
|
|
88
|
+
*
|
|
89
|
+
* // 将抛出 TypeError
|
|
90
|
+
* log(difference([1, 2, 3], 'a'));
|
|
91
|
+
* log(difference([1, 2, 3], 1));
|
|
92
|
+
* log(difference([1, 2, 3], undefined));
|
|
93
|
+
* log(difference([1, 2, 3], null));
|
|
94
|
+
* log(difference([1, 2, 3], true));
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
difference: typeof difference;
|
|
98
|
+
/**
|
|
99
|
+
* ## 对称差集 ( A △ B)
|
|
100
|
+
*
|
|
101
|
+
* @param a - 数组 a
|
|
102
|
+
* @param b - 数组 b
|
|
103
|
+
* @returns - 返回一个全新的数组
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* import { symmetricDifference } from '@mudbean/utils';
|
|
107
|
+
*
|
|
108
|
+
* const log = console.log;
|
|
109
|
+
*
|
|
110
|
+
* log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
|
|
111
|
+
*
|
|
112
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
|
|
113
|
+
*
|
|
114
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
|
|
115
|
+
*
|
|
116
|
+
* /// TypeError
|
|
117
|
+
* log(symmetricDifference(1, []));
|
|
118
|
+
* log(symmetricDifference(undefined, []));
|
|
119
|
+
* log(symmetricDifference(null, []));
|
|
120
|
+
* log(symmetricDifference('a', []));
|
|
121
|
+
* log(symmetricDifference(true, []));
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
symmetricDifference: typeof symmetricDifference;
|
|
125
|
+
};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { intersection } from './intersection.js';
|
|
2
|
+
import { union } from './union.js';
|
|
3
|
+
import { difference } from './difference.js';
|
|
4
|
+
import { symmetricDifference } from './symmetricDifference.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* # 数组的一些方法
|
|
8
|
+
*
|
|
9
|
+
* - `union` 两个数组的并集(排除共有项)
|
|
10
|
+
* - `intersection` 两个数组的交集(共有项)
|
|
11
|
+
* - `difference` 两个数组的差集 (A - B)
|
|
12
|
+
* - `symmetricDifference` 对称差集 ( A △ B)
|
|
13
|
+
*/
|
|
14
|
+
const enArr = {
|
|
15
|
+
/**
|
|
16
|
+
* ## 数组的并集
|
|
17
|
+
* <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
|
|
18
|
+
* @param arrays - 多个数组
|
|
19
|
+
* @returns 联合后的数组
|
|
20
|
+
* @example
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { union } from '@mudbean/utils';
|
|
24
|
+
*
|
|
25
|
+
* const log = console.log;
|
|
26
|
+
*
|
|
27
|
+
* // []
|
|
28
|
+
* log(union());
|
|
29
|
+
*
|
|
30
|
+
* // [1, 2, 3]
|
|
31
|
+
* log(union([1, 2, 3]));
|
|
32
|
+
*
|
|
33
|
+
* // TypeError
|
|
34
|
+
* log(union([1, 2, 3], 'i'));
|
|
35
|
+
* log(union([1, 2, 3], undefined));
|
|
36
|
+
* log(union([1, 2, 3], null));
|
|
37
|
+
* log(union([1, 2, 3], 4));
|
|
38
|
+
* log(union([1, 2, 3], {}));
|
|
39
|
+
* log(union([1, 2, 3], false));
|
|
40
|
+
*
|
|
41
|
+
* // [1, 2, 3, 4, 6]
|
|
42
|
+
* log(union([1, 2, 3], [2, 4, 6]));
|
|
43
|
+
*
|
|
44
|
+
* // [1, 2, 3, 4, 6]
|
|
45
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
|
|
46
|
+
*
|
|
47
|
+
* // [1, 2, 3, 4, 6]
|
|
48
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
union,
|
|
52
|
+
/**
|
|
53
|
+
* ## 两个数组的交集
|
|
54
|
+
*
|
|
55
|
+
* @param a 数组 1️⃣
|
|
56
|
+
* @param b 数组 2️⃣
|
|
57
|
+
* @returns 返回两个数组的交集
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { intersection } from '@mudbean/utils';
|
|
61
|
+
*
|
|
62
|
+
* const log = console.log;
|
|
63
|
+
*
|
|
64
|
+
* log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
|
|
65
|
+
* log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
intersection,
|
|
69
|
+
/**
|
|
70
|
+
* ## 求给出的两个数组的差值(A - B)
|
|
71
|
+
*
|
|
72
|
+
* @param a - 第一个数组
|
|
73
|
+
* @param b - 第二个数组
|
|
74
|
+
* @throws {TypeError} 当两个参数有一个不是
|
|
75
|
+
* @description 当两个参数有一个不是数组时将抛出
|
|
76
|
+
* @returns 返回第一个参数相对第二个参数的差值
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* import { difference} from '@mudbean/utils';
|
|
80
|
+
*
|
|
81
|
+
* const log = console.log;
|
|
82
|
+
*
|
|
83
|
+
* log(difference([], [1, 2, 3])); // []
|
|
84
|
+
*
|
|
85
|
+
* log([1, 2, 3], []); //[1, 2, 3]
|
|
86
|
+
*
|
|
87
|
+
* log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
|
|
88
|
+
*
|
|
89
|
+
* // 将抛出 TypeError
|
|
90
|
+
* log(difference([1, 2, 3], 'a'));
|
|
91
|
+
* log(difference([1, 2, 3], 1));
|
|
92
|
+
* log(difference([1, 2, 3], undefined));
|
|
93
|
+
* log(difference([1, 2, 3], null));
|
|
94
|
+
* log(difference([1, 2, 3], true));
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
difference,
|
|
98
|
+
/**
|
|
99
|
+
* ## 对称差集 ( A △ B)
|
|
100
|
+
*
|
|
101
|
+
* @param a - 数组 a
|
|
102
|
+
* @param b - 数组 b
|
|
103
|
+
* @returns - 返回一个全新的数组
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* import { symmetricDifference } from '@mudbean/utils';
|
|
107
|
+
*
|
|
108
|
+
* const log = console.log;
|
|
109
|
+
*
|
|
110
|
+
* log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
|
|
111
|
+
*
|
|
112
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
|
|
113
|
+
*
|
|
114
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
|
|
115
|
+
*
|
|
116
|
+
* /// TypeError
|
|
117
|
+
* log(symmetricDifference(1, []));
|
|
118
|
+
* log(symmetricDifference(undefined, []));
|
|
119
|
+
* log(symmetricDifference(null, []));
|
|
120
|
+
* log(symmetricDifference('a', []));
|
|
121
|
+
* log(symmetricDifference(true, []));
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
124
|
+
symmetricDifference,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export { difference, enArr, intersection, symmetricDifference, union };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* # 两个数组的交集
|
|
3
|
+
*
|
|
4
|
+
* @param a 数组 1️⃣
|
|
5
|
+
* @param b 数组 2️⃣
|
|
6
|
+
* @returns 返回两个数组的交集
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { intersection } from '@mudbean/utils';
|
|
10
|
+
*
|
|
11
|
+
* const log = console.log;
|
|
12
|
+
*
|
|
13
|
+
* log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
|
|
14
|
+
* log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function intersection<T>(a: T[], b: T[]): T[];
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { isArray, isEmptyArray } from '@mudbean/is';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* # 两个数组的交集
|
|
5
|
+
*
|
|
6
|
+
* @param a 数组 1️⃣
|
|
7
|
+
* @param b 数组 2️⃣
|
|
8
|
+
* @returns 返回两个数组的交集
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { intersection } from '@mudbean/utils';
|
|
12
|
+
*
|
|
13
|
+
* const log = console.log;
|
|
14
|
+
*
|
|
15
|
+
* log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
|
|
16
|
+
* log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function intersection(a, b) {
|
|
20
|
+
if (!isArray(a) || !isArray(b)) {
|
|
21
|
+
throw new TypeError('参数必须是数组类型数据');
|
|
22
|
+
}
|
|
23
|
+
// 任意数组为空,则返回空数组
|
|
24
|
+
if (isEmptyArray(a) || isEmptyArray(b)) {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 在实际运算中, new Set() 的 开销为 O(n) ,filter 的开销也为 O(n)
|
|
29
|
+
*
|
|
30
|
+
* 但是以较短的数组创建 Set ,可以节省内存开销
|
|
31
|
+
*/
|
|
32
|
+
const [shorter, longer] = a.length <= b.length ? [a, b] : [b, a];
|
|
33
|
+
// 提前创建工具 Set
|
|
34
|
+
const shortSet = new Set(shorter);
|
|
35
|
+
return longer.filter(item => shortSet.has(item));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export { intersection };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* # 对称差集 ( A △ B)
|
|
3
|
+
*
|
|
4
|
+
* @param a - 数组 a
|
|
5
|
+
* @param b - 数组 b
|
|
6
|
+
* @returns - 返回一个全新的数组
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { symmetricDifference } from '@mudbean/utils';
|
|
10
|
+
*
|
|
11
|
+
* const log = console.log;
|
|
12
|
+
*
|
|
13
|
+
* log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
|
|
14
|
+
*
|
|
15
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
|
|
16
|
+
*
|
|
17
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
|
|
18
|
+
*
|
|
19
|
+
* /// TypeError
|
|
20
|
+
* log(symmetricDifference(1, []));
|
|
21
|
+
* log(symmetricDifference(undefined, []));
|
|
22
|
+
* log(symmetricDifference(null, []));
|
|
23
|
+
* log(symmetricDifference('a', []));
|
|
24
|
+
* log(symmetricDifference(true, []));
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare function symmetricDifference<T>(a: T[], b: T[]): T[];
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { isArray, isEmptyArray } from '@mudbean/is';
|
|
2
|
+
import { difference } from './difference.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* # 对称差集 ( A △ B)
|
|
6
|
+
*
|
|
7
|
+
* @param a - 数组 a
|
|
8
|
+
* @param b - 数组 b
|
|
9
|
+
* @returns - 返回一个全新的数组
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { symmetricDifference } from '@mudbean/utils';
|
|
13
|
+
*
|
|
14
|
+
* const log = console.log;
|
|
15
|
+
*
|
|
16
|
+
* log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
|
|
17
|
+
*
|
|
18
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
|
|
19
|
+
*
|
|
20
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
|
|
21
|
+
*
|
|
22
|
+
* /// TypeError
|
|
23
|
+
* log(symmetricDifference(1, []));
|
|
24
|
+
* log(symmetricDifference(undefined, []));
|
|
25
|
+
* log(symmetricDifference(null, []));
|
|
26
|
+
* log(symmetricDifference('a', []));
|
|
27
|
+
* log(symmetricDifference(true, []));
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
function symmetricDifference(a, b) {
|
|
31
|
+
if (!isArray(a) || !isArray(b)) {
|
|
32
|
+
throw new TypeError('参数必须是数组');
|
|
33
|
+
}
|
|
34
|
+
if (isEmptyArray(a)) {
|
|
35
|
+
return [...b];
|
|
36
|
+
}
|
|
37
|
+
if (isEmptyArray(b)) {
|
|
38
|
+
return [...a];
|
|
39
|
+
}
|
|
40
|
+
return [...difference(a, b), ...difference(b, a)];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { symmetricDifference };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* # 数组的并集
|
|
3
|
+
*
|
|
4
|
+
* <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
|
|
5
|
+
* @param arrays - 多个数组
|
|
6
|
+
* @returns 联合后的数组
|
|
7
|
+
* @example
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { union } from '@mudbean/is';
|
|
11
|
+
*
|
|
12
|
+
* const log = console.log;
|
|
13
|
+
*
|
|
14
|
+
* // []
|
|
15
|
+
* log(union());
|
|
16
|
+
*
|
|
17
|
+
* // [1, 2, 3]
|
|
18
|
+
* log(union([1, 2, 3]));
|
|
19
|
+
*
|
|
20
|
+
* // TypeError
|
|
21
|
+
* log(union([1, 2, 3], 'i'));
|
|
22
|
+
* log(union([1, 2, 3], undefined));
|
|
23
|
+
* log(union([1, 2, 3], null));
|
|
24
|
+
* log(union([1, 2, 3], 4));
|
|
25
|
+
* log(union([1, 2, 3], {}));
|
|
26
|
+
* log(union([1, 2, 3], false));
|
|
27
|
+
*
|
|
28
|
+
* // [1, 2, 3, 4, 6]
|
|
29
|
+
* log(union([1, 2, 3], [2, 4, 6]));
|
|
30
|
+
*
|
|
31
|
+
* // [1, 2, 3, 4, 6]
|
|
32
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
|
|
33
|
+
*
|
|
34
|
+
* // [1, 2, 3, 4, 6]
|
|
35
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
39
|
+
export declare function union<T>(...arrays: T[][]): T[];
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { isEmptyArray, isArray } from '@mudbean/is';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* # 数组的并集
|
|
5
|
+
*
|
|
6
|
+
* <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
|
|
7
|
+
* @param arrays - 多个数组
|
|
8
|
+
* @returns 联合后的数组
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { union } from '@mudbean/is';
|
|
13
|
+
*
|
|
14
|
+
* const log = console.log;
|
|
15
|
+
*
|
|
16
|
+
* // []
|
|
17
|
+
* log(union());
|
|
18
|
+
*
|
|
19
|
+
* // [1, 2, 3]
|
|
20
|
+
* log(union([1, 2, 3]));
|
|
21
|
+
*
|
|
22
|
+
* // TypeError
|
|
23
|
+
* log(union([1, 2, 3], 'i'));
|
|
24
|
+
* log(union([1, 2, 3], undefined));
|
|
25
|
+
* log(union([1, 2, 3], null));
|
|
26
|
+
* log(union([1, 2, 3], 4));
|
|
27
|
+
* log(union([1, 2, 3], {}));
|
|
28
|
+
* log(union([1, 2, 3], false));
|
|
29
|
+
*
|
|
30
|
+
* // [1, 2, 3, 4, 6]
|
|
31
|
+
* log(union([1, 2, 3], [2, 4, 6]));
|
|
32
|
+
*
|
|
33
|
+
* // [1, 2, 3, 4, 6]
|
|
34
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
|
|
35
|
+
*
|
|
36
|
+
* // [1, 2, 3, 4, 6]
|
|
37
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
*/
|
|
41
|
+
function union(...arrays) {
|
|
42
|
+
if (isEmptyArray(arrays)) {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
if (arrays.some(i => !isArray(i))) {
|
|
46
|
+
throw new TypeError('参数必须都是数组形式的元素');
|
|
47
|
+
}
|
|
48
|
+
if (arrays.length === 1) {
|
|
49
|
+
return [...arrays[0]];
|
|
50
|
+
}
|
|
51
|
+
const resultSet = new Set();
|
|
52
|
+
for (const array of arrays) {
|
|
53
|
+
for (const item of array) {
|
|
54
|
+
resultSet.add(item);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return Array.from(resultSet);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { union };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 驼峰命名与连字符命名法的互换
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* # 连字符连接转化为小/大驼峰命名法
|
|
6
|
+
*
|
|
7
|
+
* @param str 待转化文本
|
|
8
|
+
* @param dividingType 连字符,缺省值为 "-"
|
|
9
|
+
* @param initial 是否转换第一个字符。默认值为 false (小驼峰类型)
|
|
10
|
+
* @returns 驼峰命名法字符串(e.g. “helloWorld”)
|
|
11
|
+
*/
|
|
12
|
+
export declare function toLowerCamelCase(
|
|
13
|
+
/** 待转化文本 */
|
|
14
|
+
str: string,
|
|
15
|
+
/** 连字符,缺省值为 "-" */
|
|
16
|
+
dividingType?: string,
|
|
17
|
+
/** 是否转换第一个字符。默认值为 false (小驼峰类型) */
|
|
18
|
+
initial?: boolean): string;
|
|
19
|
+
/**
|
|
20
|
+
* # 驼峰命名法转化为连字符连接
|
|
21
|
+
*
|
|
22
|
+
* @param str 待转化文本
|
|
23
|
+
* @param dividingType 分割符
|
|
24
|
+
* @returns 分割符转化的文本 (e.g. 'hello-world')
|
|
25
|
+
*/
|
|
26
|
+
export declare function toSplitCase(str: string, dividingType?: string): string;
|