a-js-tools 0.5.4 → 0.6.1
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 +9 -1
- package/cjs/index.cjs +11 -1
- package/cjs/src/array/difference.cjs +51 -0
- package/cjs/src/array/index.cjs +154 -0
- package/cjs/src/array/intersection.cjs +45 -0
- package/cjs/src/array/symmetricDifference.cjs +51 -0
- package/cjs/src/array/union.cjs +65 -0
- package/mjs/index.mjs +6 -1
- package/mjs/src/array/difference.mjs +49 -0
- package/mjs/src/array/index.mjs +148 -0
- package/mjs/src/array/intersection.mjs +43 -0
- package/mjs/src/array/symmetricDifference.mjs +49 -0
- package/mjs/src/array/union.mjs +63 -0
- package/package.json +1 -1
- package/types/index.d.ts +3 -2
- package/types/src/array/difference.d.ts +32 -0
- package/types/src/array/index.d.ts +146 -0
- package/types/src/array/intersection.d.ts +22 -0
- package/types/src/array/symmetricDifference.d.ts +33 -0
- package/types/src/array/union.d.ts +42 -0
- /package/cjs/src/{createConstructor.cjs → object/createConstructor.cjs} +0 -0
- /package/mjs/src/{createConstructor.mjs → object/createConstructor.mjs} +0 -0
- /package/types/src/{createConstructor.d.ts → object/createConstructor.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -27,6 +27,14 @@ npm install a-js-tools --save
|
|
|
27
27
|
- `toLowerCamelCase` 转化为小驼峰
|
|
28
28
|
- `toSplitCase` 转化为连接符分隔
|
|
29
29
|
|
|
30
|
+
## 数组相关
|
|
31
|
+
|
|
32
|
+
- `intersection` 方法,计算两个数组的交集(两个数组共有的元素)
|
|
33
|
+
- `union` 方法,计算两个数组的并集(两个数组合并在一起并去重)
|
|
34
|
+
- `difference` 方法,计算两个数组的差集(以第一个数组为基准)
|
|
35
|
+
- `symmetricDifference` 方法,计算两个数组的对称差集(在两个数组都不共有的元素)
|
|
36
|
+
- `enArr` 对象,包含上面的方法
|
|
37
|
+
|
|
30
38
|
## 查看文档
|
|
31
39
|
|
|
32
|
-
|
|
40
|
+
查看 [https://earthnut.dev/a-js-tools](https://earthnut.dev/a-js-tools)
|
package/cjs/index.cjs
CHANGED
|
@@ -7,7 +7,12 @@ var performance = require('./src/performance.cjs');
|
|
|
7
7
|
var escapeRegExp = require('./src/regexp/escapeRegExp.cjs');
|
|
8
8
|
var autoEscapedRegExp = require('./src/regexp/autoEscapedRegExp.cjs');
|
|
9
9
|
var isNode = require('./src/isNode.cjs');
|
|
10
|
-
var createConstructor = require('./src/createConstructor.cjs');
|
|
10
|
+
var createConstructor = require('./src/object/createConstructor.cjs');
|
|
11
|
+
var index = require('./src/array/index.cjs');
|
|
12
|
+
var intersection = require('./src/array/intersection.cjs');
|
|
13
|
+
var union = require('./src/array/union.cjs');
|
|
14
|
+
var difference = require('./src/array/difference.cjs');
|
|
15
|
+
var symmetricDifference = require('./src/array/symmetricDifference.cjs');
|
|
11
16
|
|
|
12
17
|
|
|
13
18
|
|
|
@@ -23,3 +28,8 @@ exports.autoEscapedRegExp = autoEscapedRegExp.autoEscapedRegExp;
|
|
|
23
28
|
exports.isBrowser = isNode.isBrowser;
|
|
24
29
|
exports.isNode = isNode.isNode;
|
|
25
30
|
exports.createConstructor = createConstructor.createConstructor;
|
|
31
|
+
exports.enArr = index.enArr;
|
|
32
|
+
exports.intersection = intersection.intersection;
|
|
33
|
+
exports.union = union.union;
|
|
34
|
+
exports.difference = difference.difference;
|
|
35
|
+
exports.symmetricDifference = symmetricDifference.symmetricDifference;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var aTypeOfJs = require('a-type-of-js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 求给出的两个数组的差值(A - B)
|
|
7
|
+
*
|
|
8
|
+
* @param a - 第一个数组
|
|
9
|
+
* @param b - 第二个数组
|
|
10
|
+
* @throws {TypeError} 当两个参数有一个不是
|
|
11
|
+
* @description 当两个参数有一个不是数组时将抛出
|
|
12
|
+
* @returns 返回第一个参数相对第二个参数的差值
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { difference} from 'a-js-tools';
|
|
17
|
+
*
|
|
18
|
+
* const log = console.log;
|
|
19
|
+
*
|
|
20
|
+
*
|
|
21
|
+
* log(difference([], [1, 2, 3])); // []
|
|
22
|
+
*
|
|
23
|
+
* log([1, 2, 3], []); //[1, 2, 3]
|
|
24
|
+
*
|
|
25
|
+
* log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
|
|
26
|
+
*
|
|
27
|
+
* // 将抛出 TypeError
|
|
28
|
+
* log(difference([1, 2, 3], 'a'));
|
|
29
|
+
* log(difference([1, 2, 3], 1));
|
|
30
|
+
* log(difference([1, 2, 3], undefined));
|
|
31
|
+
* log(difference([1, 2, 3], null));
|
|
32
|
+
* log(difference([1, 2, 3], true));
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
*/
|
|
36
|
+
function difference(a, b) {
|
|
37
|
+
if (!aTypeOfJs.isArray(a) || !aTypeOfJs.isArray(b)) {
|
|
38
|
+
throw new TypeError('参数需为数组');
|
|
39
|
+
}
|
|
40
|
+
if (a.length === 0 || b.length === 0) {
|
|
41
|
+
return a;
|
|
42
|
+
}
|
|
43
|
+
/** 获取两个数组中长度较小的 */
|
|
44
|
+
// 参数有顺序要求
|
|
45
|
+
// const [shorter, longer] = a.length > b.length ? [b, a] : [a, b];
|
|
46
|
+
// const shorterSet = new Set(shorter);
|
|
47
|
+
const bSet = new Set(b);
|
|
48
|
+
return a.filter(i => !bSet.has(i));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
exports.difference = difference;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var intersection = require('./intersection.cjs');
|
|
4
|
+
var union = require('./union.cjs');
|
|
5
|
+
var difference = require('./difference.cjs');
|
|
6
|
+
var symmetricDifference = require('./symmetricDifference.cjs');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* 数组的一些方法
|
|
11
|
+
*
|
|
12
|
+
* - union 两个数组的并集(排除共有项)
|
|
13
|
+
* - intersection 两个数组的交集(共有项)
|
|
14
|
+
* - difference 两个数组的差集 (A - B)
|
|
15
|
+
* - symmetricDifference 对称差集 ( A △ B)
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
18
|
+
const enArr = {
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* 数组的并集
|
|
22
|
+
*
|
|
23
|
+
* <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
|
|
24
|
+
* @param arrays - 多个数组
|
|
25
|
+
* @returns 联合后的数组
|
|
26
|
+
*
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { union } from 'a-js-tools';
|
|
32
|
+
*
|
|
33
|
+
* const log = console.log;
|
|
34
|
+
*
|
|
35
|
+
* // []
|
|
36
|
+
* log(union());
|
|
37
|
+
*
|
|
38
|
+
* // [1, 2, 3]
|
|
39
|
+
* log(union([1, 2, 3]));
|
|
40
|
+
*
|
|
41
|
+
* // TypeError
|
|
42
|
+
* log(union([1, 2, 3], 'i'));
|
|
43
|
+
* log(union([1, 2, 3], undefined));
|
|
44
|
+
* log(union([1, 2, 3], null));
|
|
45
|
+
* log(union([1, 2, 3], 4));
|
|
46
|
+
* log(union([1, 2, 3], {}));
|
|
47
|
+
* log(union([1, 2, 3], false));
|
|
48
|
+
*
|
|
49
|
+
* // [1, 2, 3, 4, 6]
|
|
50
|
+
* log(union([1, 2, 3], [2, 4, 6]));
|
|
51
|
+
*
|
|
52
|
+
* // [1, 2, 3, 4, 6]
|
|
53
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
|
|
54
|
+
*
|
|
55
|
+
* // [1, 2, 3, 4, 6]
|
|
56
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
*/
|
|
60
|
+
union: union.union,
|
|
61
|
+
/**
|
|
62
|
+
*
|
|
63
|
+
* 两个数组的交集
|
|
64
|
+
*
|
|
65
|
+
* @param a 数组 1️⃣
|
|
66
|
+
* @param b 数组 2️⃣
|
|
67
|
+
* @returns 返回两个数组的交集
|
|
68
|
+
* @example
|
|
69
|
+
*
|
|
70
|
+
* ```ts
|
|
71
|
+
* import { intersection } from 'a-js-tools';
|
|
72
|
+
*
|
|
73
|
+
* const log = console.log;
|
|
74
|
+
*
|
|
75
|
+
*
|
|
76
|
+
* log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
|
|
77
|
+
* log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
|
|
78
|
+
*
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
*/
|
|
82
|
+
intersection: intersection.intersection,
|
|
83
|
+
/**
|
|
84
|
+
* 求给出的两个数组的差值(A - B)
|
|
85
|
+
*
|
|
86
|
+
* @param a - 第一个数组
|
|
87
|
+
* @param b - 第二个数组
|
|
88
|
+
* @throws {TypeError} 当两个参数有一个不是
|
|
89
|
+
* @description 当两个参数有一个不是数组时将抛出
|
|
90
|
+
* @returns 返回第一个参数相对第二个参数的差值
|
|
91
|
+
* @example
|
|
92
|
+
*
|
|
93
|
+
* ```ts
|
|
94
|
+
* import { difference} from 'a-js-tools';
|
|
95
|
+
*
|
|
96
|
+
* const log = console.log;
|
|
97
|
+
*
|
|
98
|
+
*
|
|
99
|
+
* log(difference([], [1, 2, 3])); // []
|
|
100
|
+
*
|
|
101
|
+
* log([1, 2, 3], []); //[1, 2, 3]
|
|
102
|
+
*
|
|
103
|
+
* log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
|
|
104
|
+
*
|
|
105
|
+
* // 将抛出 TypeError
|
|
106
|
+
* log(difference([1, 2, 3], 'a'));
|
|
107
|
+
* log(difference([1, 2, 3], 1));
|
|
108
|
+
* log(difference([1, 2, 3], undefined));
|
|
109
|
+
* log(difference([1, 2, 3], null));
|
|
110
|
+
* log(difference([1, 2, 3], true));
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
*/
|
|
114
|
+
difference: difference.difference,
|
|
115
|
+
/**
|
|
116
|
+
*
|
|
117
|
+
* 对称差集 ( A △ B)
|
|
118
|
+
*
|
|
119
|
+
* @param a - 数组 a
|
|
120
|
+
* @param b - 数组 b
|
|
121
|
+
* @returns - 返回一个全新的数组
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
*
|
|
125
|
+
* ```ts
|
|
126
|
+
* import { symmetricDifference } from 'a-js-tools';
|
|
127
|
+
*
|
|
128
|
+
* const log = console.log;
|
|
129
|
+
*
|
|
130
|
+
* log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
|
|
131
|
+
*
|
|
132
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
|
|
133
|
+
*
|
|
134
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
|
|
135
|
+
*
|
|
136
|
+
*
|
|
137
|
+
* /// TypeError
|
|
138
|
+
* log(symmetricDifference(1, []));
|
|
139
|
+
* log(symmetricDifference(undefined, []));
|
|
140
|
+
* log(symmetricDifference(null, []));
|
|
141
|
+
* log(symmetricDifference('a', []));
|
|
142
|
+
* log(symmetricDifference(true, []));
|
|
143
|
+
*
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
*/
|
|
147
|
+
symmetricDifference: symmetricDifference.symmetricDifference,
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
exports.intersection = intersection.intersection;
|
|
151
|
+
exports.union = union.union;
|
|
152
|
+
exports.difference = difference.difference;
|
|
153
|
+
exports.symmetricDifference = symmetricDifference.symmetricDifference;
|
|
154
|
+
exports.enArr = enArr;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var aTypeOfJs = require('a-type-of-js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* 两个数组的交集
|
|
8
|
+
*
|
|
9
|
+
* @param a 数组 1️⃣
|
|
10
|
+
* @param b 数组 2️⃣
|
|
11
|
+
* @returns 返回两个数组的交集
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { intersection } from 'a-js-tools';
|
|
16
|
+
*
|
|
17
|
+
* const log = console.log;
|
|
18
|
+
*
|
|
19
|
+
*
|
|
20
|
+
* log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
|
|
21
|
+
* log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
|
|
22
|
+
*
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
function intersection(a, b) {
|
|
27
|
+
if (!aTypeOfJs.isArray(a) || !aTypeOfJs.isArray(b)) {
|
|
28
|
+
throw new TypeError('参数必须是数组类型数据');
|
|
29
|
+
}
|
|
30
|
+
// 任意数组为空,则返回空数组
|
|
31
|
+
if (a.length === 0 || b.length === 0) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 在实际运算中, new Set() 的 开销为 O(n) ,filter 的开销也为 O(n)
|
|
36
|
+
*
|
|
37
|
+
* 但是以较短的数组创建 Set ,可以节省内存开销
|
|
38
|
+
*/
|
|
39
|
+
const [shorter, longer] = a.length <= b.length ? [a, b] : [b, a];
|
|
40
|
+
// 提前创建工具 Set
|
|
41
|
+
const shortSet = new Set(shorter);
|
|
42
|
+
return longer.filter(item => shortSet.has(item));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
exports.intersection = intersection;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var aTypeOfJs = require('a-type-of-js');
|
|
4
|
+
var difference = require('./difference.cjs');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* 对称差集 ( A △ B)
|
|
9
|
+
*
|
|
10
|
+
* @param a - 数组 a
|
|
11
|
+
* @param b - 数组 b
|
|
12
|
+
* @returns - 返回一个全新的数组
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { symmetricDifference } from 'a-js-tools';
|
|
18
|
+
*
|
|
19
|
+
* const log = console.log;
|
|
20
|
+
*
|
|
21
|
+
* log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
|
|
22
|
+
*
|
|
23
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
|
|
24
|
+
*
|
|
25
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
|
|
26
|
+
*
|
|
27
|
+
*
|
|
28
|
+
* /// TypeError
|
|
29
|
+
* log(symmetricDifference(1, []));
|
|
30
|
+
* log(symmetricDifference(undefined, []));
|
|
31
|
+
* log(symmetricDifference(null, []));
|
|
32
|
+
* log(symmetricDifference('a', []));
|
|
33
|
+
* log(symmetricDifference(true, []));
|
|
34
|
+
*
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
*/
|
|
38
|
+
function symmetricDifference(a, b) {
|
|
39
|
+
if (!aTypeOfJs.isArray(a) || !aTypeOfJs.isArray(b)) {
|
|
40
|
+
throw new TypeError('参数必须是数组');
|
|
41
|
+
}
|
|
42
|
+
if (a.length === 0) {
|
|
43
|
+
return [...b];
|
|
44
|
+
}
|
|
45
|
+
if (b.length === 0) {
|
|
46
|
+
return [...a];
|
|
47
|
+
}
|
|
48
|
+
return [...difference.difference(a, b), ...difference.difference(b, a)];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
exports.symmetricDifference = symmetricDifference;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var aTypeOfJs = require('a-type-of-js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* 数组的并集
|
|
8
|
+
*
|
|
9
|
+
* <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
|
|
10
|
+
* @param arrays - 多个数组
|
|
11
|
+
* @returns 联合后的数组
|
|
12
|
+
*
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { union } from 'a-js-tools';
|
|
18
|
+
*
|
|
19
|
+
* const log = console.log;
|
|
20
|
+
*
|
|
21
|
+
* // []
|
|
22
|
+
* log(union());
|
|
23
|
+
*
|
|
24
|
+
* // [1, 2, 3]
|
|
25
|
+
* log(union([1, 2, 3]));
|
|
26
|
+
*
|
|
27
|
+
* // TypeError
|
|
28
|
+
* log(union([1, 2, 3], 'i'));
|
|
29
|
+
* log(union([1, 2, 3], undefined));
|
|
30
|
+
* log(union([1, 2, 3], null));
|
|
31
|
+
* log(union([1, 2, 3], 4));
|
|
32
|
+
* log(union([1, 2, 3], {}));
|
|
33
|
+
* log(union([1, 2, 3], false));
|
|
34
|
+
*
|
|
35
|
+
* // [1, 2, 3, 4, 6]
|
|
36
|
+
* log(union([1, 2, 3], [2, 4, 6]));
|
|
37
|
+
*
|
|
38
|
+
* // [1, 2, 3, 4, 6]
|
|
39
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
|
|
40
|
+
*
|
|
41
|
+
* // [1, 2, 3, 4, 6]
|
|
42
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
*/
|
|
46
|
+
function union(...arrays) {
|
|
47
|
+
if (arrays.length === 0) {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
if (arrays.some(i => !aTypeOfJs.isArray(i))) {
|
|
51
|
+
throw new TypeError('参数必须都是数组形式的元素');
|
|
52
|
+
}
|
|
53
|
+
if (arrays.length === 1) {
|
|
54
|
+
return [...arrays[0]];
|
|
55
|
+
}
|
|
56
|
+
const resultSet = new Set();
|
|
57
|
+
for (const array of arrays) {
|
|
58
|
+
for (const item of array) {
|
|
59
|
+
resultSet.add(item);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return Array.from(resultSet);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
exports.union = union;
|
package/mjs/index.mjs
CHANGED
|
@@ -5,4 +5,9 @@ export { debounce, throttle } from './src/performance.mjs';
|
|
|
5
5
|
export { escapeRegExp } from './src/regexp/escapeRegExp.mjs';
|
|
6
6
|
export { autoEscapedRegExp } from './src/regexp/autoEscapedRegExp.mjs';
|
|
7
7
|
export { isBrowser, isNode } from './src/isNode.mjs';
|
|
8
|
-
export { createConstructor } from './src/createConstructor.mjs';
|
|
8
|
+
export { createConstructor } from './src/object/createConstructor.mjs';
|
|
9
|
+
export { enArr } from './src/array/index.mjs';
|
|
10
|
+
export { intersection } from './src/array/intersection.mjs';
|
|
11
|
+
export { union } from './src/array/union.mjs';
|
|
12
|
+
export { difference } from './src/array/difference.mjs';
|
|
13
|
+
export { symmetricDifference } from './src/array/symmetricDifference.mjs';
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { isArray } from 'a-type-of-js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 求给出的两个数组的差值(A - B)
|
|
5
|
+
*
|
|
6
|
+
* @param a - 第一个数组
|
|
7
|
+
* @param b - 第二个数组
|
|
8
|
+
* @throws {TypeError} 当两个参数有一个不是
|
|
9
|
+
* @description 当两个参数有一个不是数组时将抛出
|
|
10
|
+
* @returns 返回第一个参数相对第二个参数的差值
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { difference} from 'a-js-tools';
|
|
15
|
+
*
|
|
16
|
+
* const log = console.log;
|
|
17
|
+
*
|
|
18
|
+
*
|
|
19
|
+
* log(difference([], [1, 2, 3])); // []
|
|
20
|
+
*
|
|
21
|
+
* log([1, 2, 3], []); //[1, 2, 3]
|
|
22
|
+
*
|
|
23
|
+
* log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
|
|
24
|
+
*
|
|
25
|
+
* // 将抛出 TypeError
|
|
26
|
+
* log(difference([1, 2, 3], 'a'));
|
|
27
|
+
* log(difference([1, 2, 3], 1));
|
|
28
|
+
* log(difference([1, 2, 3], undefined));
|
|
29
|
+
* log(difference([1, 2, 3], null));
|
|
30
|
+
* log(difference([1, 2, 3], true));
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
*/
|
|
34
|
+
function difference(a, b) {
|
|
35
|
+
if (!isArray(a) || !isArray(b)) {
|
|
36
|
+
throw new TypeError('参数需为数组');
|
|
37
|
+
}
|
|
38
|
+
if (a.length === 0 || b.length === 0) {
|
|
39
|
+
return a;
|
|
40
|
+
}
|
|
41
|
+
/** 获取两个数组中长度较小的 */
|
|
42
|
+
// 参数有顺序要求
|
|
43
|
+
// const [shorter, longer] = a.length > b.length ? [b, a] : [a, b];
|
|
44
|
+
// const shorterSet = new Set(shorter);
|
|
45
|
+
const bSet = new Set(b);
|
|
46
|
+
return a.filter(i => !bSet.has(i));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { difference };
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { intersection } from './intersection.mjs';
|
|
2
|
+
import { union } from './union.mjs';
|
|
3
|
+
import { difference } from './difference.mjs';
|
|
4
|
+
import { symmetricDifference } from './symmetricDifference.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* 数组的一些方法
|
|
9
|
+
*
|
|
10
|
+
* - union 两个数组的并集(排除共有项)
|
|
11
|
+
* - intersection 两个数组的交集(共有项)
|
|
12
|
+
* - difference 两个数组的差集 (A - B)
|
|
13
|
+
* - symmetricDifference 对称差集 ( A △ B)
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
const enArr = {
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* 数组的并集
|
|
20
|
+
*
|
|
21
|
+
* <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
|
|
22
|
+
* @param arrays - 多个数组
|
|
23
|
+
* @returns 联合后的数组
|
|
24
|
+
*
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { union } from 'a-js-tools';
|
|
30
|
+
*
|
|
31
|
+
* const log = console.log;
|
|
32
|
+
*
|
|
33
|
+
* // []
|
|
34
|
+
* log(union());
|
|
35
|
+
*
|
|
36
|
+
* // [1, 2, 3]
|
|
37
|
+
* log(union([1, 2, 3]));
|
|
38
|
+
*
|
|
39
|
+
* // TypeError
|
|
40
|
+
* log(union([1, 2, 3], 'i'));
|
|
41
|
+
* log(union([1, 2, 3], undefined));
|
|
42
|
+
* log(union([1, 2, 3], null));
|
|
43
|
+
* log(union([1, 2, 3], 4));
|
|
44
|
+
* log(union([1, 2, 3], {}));
|
|
45
|
+
* log(union([1, 2, 3], false));
|
|
46
|
+
*
|
|
47
|
+
* // [1, 2, 3, 4, 6]
|
|
48
|
+
* log(union([1, 2, 3], [2, 4, 6]));
|
|
49
|
+
*
|
|
50
|
+
* // [1, 2, 3, 4, 6]
|
|
51
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
|
|
52
|
+
*
|
|
53
|
+
* // [1, 2, 3, 4, 6]
|
|
54
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
58
|
+
union,
|
|
59
|
+
/**
|
|
60
|
+
*
|
|
61
|
+
* 两个数组的交集
|
|
62
|
+
*
|
|
63
|
+
* @param a 数组 1️⃣
|
|
64
|
+
* @param b 数组 2️⃣
|
|
65
|
+
* @returns 返回两个数组的交集
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { intersection } from 'a-js-tools';
|
|
70
|
+
*
|
|
71
|
+
* const log = console.log;
|
|
72
|
+
*
|
|
73
|
+
*
|
|
74
|
+
* log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
|
|
75
|
+
* log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
|
|
76
|
+
*
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
*/
|
|
80
|
+
intersection,
|
|
81
|
+
/**
|
|
82
|
+
* 求给出的两个数组的差值(A - B)
|
|
83
|
+
*
|
|
84
|
+
* @param a - 第一个数组
|
|
85
|
+
* @param b - 第二个数组
|
|
86
|
+
* @throws {TypeError} 当两个参数有一个不是
|
|
87
|
+
* @description 当两个参数有一个不是数组时将抛出
|
|
88
|
+
* @returns 返回第一个参数相对第二个参数的差值
|
|
89
|
+
* @example
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { difference} from 'a-js-tools';
|
|
93
|
+
*
|
|
94
|
+
* const log = console.log;
|
|
95
|
+
*
|
|
96
|
+
*
|
|
97
|
+
* log(difference([], [1, 2, 3])); // []
|
|
98
|
+
*
|
|
99
|
+
* log([1, 2, 3], []); //[1, 2, 3]
|
|
100
|
+
*
|
|
101
|
+
* log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
|
|
102
|
+
*
|
|
103
|
+
* // 将抛出 TypeError
|
|
104
|
+
* log(difference([1, 2, 3], 'a'));
|
|
105
|
+
* log(difference([1, 2, 3], 1));
|
|
106
|
+
* log(difference([1, 2, 3], undefined));
|
|
107
|
+
* log(difference([1, 2, 3], null));
|
|
108
|
+
* log(difference([1, 2, 3], true));
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
*/
|
|
112
|
+
difference,
|
|
113
|
+
/**
|
|
114
|
+
*
|
|
115
|
+
* 对称差集 ( A △ B)
|
|
116
|
+
*
|
|
117
|
+
* @param a - 数组 a
|
|
118
|
+
* @param b - 数组 b
|
|
119
|
+
* @returns - 返回一个全新的数组
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { symmetricDifference } from 'a-js-tools';
|
|
125
|
+
*
|
|
126
|
+
* const log = console.log;
|
|
127
|
+
*
|
|
128
|
+
* log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
|
|
129
|
+
*
|
|
130
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
|
|
131
|
+
*
|
|
132
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
|
|
133
|
+
*
|
|
134
|
+
*
|
|
135
|
+
* /// TypeError
|
|
136
|
+
* log(symmetricDifference(1, []));
|
|
137
|
+
* log(symmetricDifference(undefined, []));
|
|
138
|
+
* log(symmetricDifference(null, []));
|
|
139
|
+
* log(symmetricDifference('a', []));
|
|
140
|
+
* log(symmetricDifference(true, []));
|
|
141
|
+
*
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
*/
|
|
145
|
+
symmetricDifference,
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
export { difference, enArr, intersection, symmetricDifference, union };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { isArray } from 'a-type-of-js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* 两个数组的交集
|
|
6
|
+
*
|
|
7
|
+
* @param a 数组 1️⃣
|
|
8
|
+
* @param b 数组 2️⃣
|
|
9
|
+
* @returns 返回两个数组的交集
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { intersection } from 'a-js-tools';
|
|
14
|
+
*
|
|
15
|
+
* const log = console.log;
|
|
16
|
+
*
|
|
17
|
+
*
|
|
18
|
+
* log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
|
|
19
|
+
* log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
|
|
20
|
+
*
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
function intersection(a, b) {
|
|
25
|
+
if (!isArray(a) || !isArray(b)) {
|
|
26
|
+
throw new TypeError('参数必须是数组类型数据');
|
|
27
|
+
}
|
|
28
|
+
// 任意数组为空,则返回空数组
|
|
29
|
+
if (a.length === 0 || b.length === 0) {
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 在实际运算中, new Set() 的 开销为 O(n) ,filter 的开销也为 O(n)
|
|
34
|
+
*
|
|
35
|
+
* 但是以较短的数组创建 Set ,可以节省内存开销
|
|
36
|
+
*/
|
|
37
|
+
const [shorter, longer] = a.length <= b.length ? [a, b] : [b, a];
|
|
38
|
+
// 提前创建工具 Set
|
|
39
|
+
const shortSet = new Set(shorter);
|
|
40
|
+
return longer.filter(item => shortSet.has(item));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export { intersection };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { isArray } from 'a-type-of-js';
|
|
2
|
+
import { difference } from './difference.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* 对称差集 ( A △ B)
|
|
7
|
+
*
|
|
8
|
+
* @param a - 数组 a
|
|
9
|
+
* @param b - 数组 b
|
|
10
|
+
* @returns - 返回一个全新的数组
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { symmetricDifference } from 'a-js-tools';
|
|
16
|
+
*
|
|
17
|
+
* const log = console.log;
|
|
18
|
+
*
|
|
19
|
+
* log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
|
|
20
|
+
*
|
|
21
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
|
|
22
|
+
*
|
|
23
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
|
|
24
|
+
*
|
|
25
|
+
*
|
|
26
|
+
* /// TypeError
|
|
27
|
+
* log(symmetricDifference(1, []));
|
|
28
|
+
* log(symmetricDifference(undefined, []));
|
|
29
|
+
* log(symmetricDifference(null, []));
|
|
30
|
+
* log(symmetricDifference('a', []));
|
|
31
|
+
* log(symmetricDifference(true, []));
|
|
32
|
+
*
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
*/
|
|
36
|
+
function symmetricDifference(a, b) {
|
|
37
|
+
if (!isArray(a) || !isArray(b)) {
|
|
38
|
+
throw new TypeError('参数必须是数组');
|
|
39
|
+
}
|
|
40
|
+
if (a.length === 0) {
|
|
41
|
+
return [...b];
|
|
42
|
+
}
|
|
43
|
+
if (b.length === 0) {
|
|
44
|
+
return [...a];
|
|
45
|
+
}
|
|
46
|
+
return [...difference(a, b), ...difference(b, a)];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export { symmetricDifference };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { isArray } from 'a-type-of-js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* 数组的并集
|
|
6
|
+
*
|
|
7
|
+
* <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
|
|
8
|
+
* @param arrays - 多个数组
|
|
9
|
+
* @returns 联合后的数组
|
|
10
|
+
*
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { union } from 'a-js-tools';
|
|
16
|
+
*
|
|
17
|
+
* const log = console.log;
|
|
18
|
+
*
|
|
19
|
+
* // []
|
|
20
|
+
* log(union());
|
|
21
|
+
*
|
|
22
|
+
* // [1, 2, 3]
|
|
23
|
+
* log(union([1, 2, 3]));
|
|
24
|
+
*
|
|
25
|
+
* // TypeError
|
|
26
|
+
* log(union([1, 2, 3], 'i'));
|
|
27
|
+
* log(union([1, 2, 3], undefined));
|
|
28
|
+
* log(union([1, 2, 3], null));
|
|
29
|
+
* log(union([1, 2, 3], 4));
|
|
30
|
+
* log(union([1, 2, 3], {}));
|
|
31
|
+
* log(union([1, 2, 3], false));
|
|
32
|
+
*
|
|
33
|
+
* // [1, 2, 3, 4, 6]
|
|
34
|
+
* log(union([1, 2, 3], [2, 4, 6]));
|
|
35
|
+
*
|
|
36
|
+
* // [1, 2, 3, 4, 6]
|
|
37
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
|
|
38
|
+
*
|
|
39
|
+
* // [1, 2, 3, 4, 6]
|
|
40
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
function union(...arrays) {
|
|
45
|
+
if (arrays.length === 0) {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
if (arrays.some(i => !isArray(i))) {
|
|
49
|
+
throw new TypeError('参数必须都是数组形式的元素');
|
|
50
|
+
}
|
|
51
|
+
if (arrays.length === 1) {
|
|
52
|
+
return [...arrays[0]];
|
|
53
|
+
}
|
|
54
|
+
const resultSet = new Set();
|
|
55
|
+
for (const array of arrays) {
|
|
56
|
+
for (const item of array) {
|
|
57
|
+
resultSet.add(item);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return Array.from(resultSet);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export { union };
|
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -3,5 +3,6 @@ export { throttle, debounce } from './src/performance';
|
|
|
3
3
|
export type { DebounceAndThrottleReturnType } from './src/performance';
|
|
4
4
|
export { escapeRegExp, autoEscapedRegExp } from './src/regexp';
|
|
5
5
|
export { isBrowser, isNode } from './src/isNode';
|
|
6
|
-
export { createConstructor } from './src/createConstructor';
|
|
7
|
-
export type { CreateConstructor } from './src/createConstructor';
|
|
6
|
+
export { createConstructor } from './src/object/createConstructor';
|
|
7
|
+
export type { CreateConstructor } from './src/object/createConstructor';
|
|
8
|
+
export { intersection, enArr, union, difference, symmetricDifference, } from './src/array/';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 求给出的两个数组的差值(A - B)
|
|
3
|
+
*
|
|
4
|
+
* @param a - 第一个数组
|
|
5
|
+
* @param b - 第二个数组
|
|
6
|
+
* @throws {TypeError} 当两个参数有一个不是
|
|
7
|
+
* @description 当两个参数有一个不是数组时将抛出
|
|
8
|
+
* @returns 返回第一个参数相对第二个参数的差值
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { difference} from 'a-js-tools';
|
|
13
|
+
*
|
|
14
|
+
* const log = console.log;
|
|
15
|
+
*
|
|
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
|
+
*/
|
|
32
|
+
export declare function difference<T>(a: T[], b: T[]): T[];
|
|
@@ -0,0 +1,146 @@
|
|
|
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
|
+
*
|
|
10
|
+
* - union 两个数组的并集(排除共有项)
|
|
11
|
+
* - intersection 两个数组的交集(共有项)
|
|
12
|
+
* - difference 两个数组的差集 (A - B)
|
|
13
|
+
* - symmetricDifference 对称差集 ( A △ B)
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
export declare const enArr: {
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* 数组的并集
|
|
20
|
+
*
|
|
21
|
+
* <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
|
|
22
|
+
* @param arrays - 多个数组
|
|
23
|
+
* @returns 联合后的数组
|
|
24
|
+
*
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { union } from 'a-js-tools';
|
|
30
|
+
*
|
|
31
|
+
* const log = console.log;
|
|
32
|
+
*
|
|
33
|
+
* // []
|
|
34
|
+
* log(union());
|
|
35
|
+
*
|
|
36
|
+
* // [1, 2, 3]
|
|
37
|
+
* log(union([1, 2, 3]));
|
|
38
|
+
*
|
|
39
|
+
* // TypeError
|
|
40
|
+
* log(union([1, 2, 3], 'i'));
|
|
41
|
+
* log(union([1, 2, 3], undefined));
|
|
42
|
+
* log(union([1, 2, 3], null));
|
|
43
|
+
* log(union([1, 2, 3], 4));
|
|
44
|
+
* log(union([1, 2, 3], {}));
|
|
45
|
+
* log(union([1, 2, 3], false));
|
|
46
|
+
*
|
|
47
|
+
* // [1, 2, 3, 4, 6]
|
|
48
|
+
* log(union([1, 2, 3], [2, 4, 6]));
|
|
49
|
+
*
|
|
50
|
+
* // [1, 2, 3, 4, 6]
|
|
51
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
|
|
52
|
+
*
|
|
53
|
+
* // [1, 2, 3, 4, 6]
|
|
54
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
58
|
+
union: typeof union;
|
|
59
|
+
/**
|
|
60
|
+
*
|
|
61
|
+
* 两个数组的交集
|
|
62
|
+
*
|
|
63
|
+
* @param a 数组 1️⃣
|
|
64
|
+
* @param b 数组 2️⃣
|
|
65
|
+
* @returns 返回两个数组的交集
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { intersection } from 'a-js-tools';
|
|
70
|
+
*
|
|
71
|
+
* const log = console.log;
|
|
72
|
+
*
|
|
73
|
+
*
|
|
74
|
+
* log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
|
|
75
|
+
* log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
|
|
76
|
+
*
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
*/
|
|
80
|
+
intersection: typeof intersection;
|
|
81
|
+
/**
|
|
82
|
+
* 求给出的两个数组的差值(A - B)
|
|
83
|
+
*
|
|
84
|
+
* @param a - 第一个数组
|
|
85
|
+
* @param b - 第二个数组
|
|
86
|
+
* @throws {TypeError} 当两个参数有一个不是
|
|
87
|
+
* @description 当两个参数有一个不是数组时将抛出
|
|
88
|
+
* @returns 返回第一个参数相对第二个参数的差值
|
|
89
|
+
* @example
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* import { difference} from 'a-js-tools';
|
|
93
|
+
*
|
|
94
|
+
* const log = console.log;
|
|
95
|
+
*
|
|
96
|
+
*
|
|
97
|
+
* log(difference([], [1, 2, 3])); // []
|
|
98
|
+
*
|
|
99
|
+
* log([1, 2, 3], []); //[1, 2, 3]
|
|
100
|
+
*
|
|
101
|
+
* log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
|
|
102
|
+
*
|
|
103
|
+
* // 将抛出 TypeError
|
|
104
|
+
* log(difference([1, 2, 3], 'a'));
|
|
105
|
+
* log(difference([1, 2, 3], 1));
|
|
106
|
+
* log(difference([1, 2, 3], undefined));
|
|
107
|
+
* log(difference([1, 2, 3], null));
|
|
108
|
+
* log(difference([1, 2, 3], true));
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
*/
|
|
112
|
+
difference: typeof difference;
|
|
113
|
+
/**
|
|
114
|
+
*
|
|
115
|
+
* 对称差集 ( A △ B)
|
|
116
|
+
*
|
|
117
|
+
* @param a - 数组 a
|
|
118
|
+
* @param b - 数组 b
|
|
119
|
+
* @returns - 返回一个全新的数组
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
*
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { symmetricDifference } from 'a-js-tools';
|
|
125
|
+
*
|
|
126
|
+
* const log = console.log;
|
|
127
|
+
*
|
|
128
|
+
* log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
|
|
129
|
+
*
|
|
130
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
|
|
131
|
+
*
|
|
132
|
+
* log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
|
|
133
|
+
*
|
|
134
|
+
*
|
|
135
|
+
* /// TypeError
|
|
136
|
+
* log(symmetricDifference(1, []));
|
|
137
|
+
* log(symmetricDifference(undefined, []));
|
|
138
|
+
* log(symmetricDifference(null, []));
|
|
139
|
+
* log(symmetricDifference('a', []));
|
|
140
|
+
* log(symmetricDifference(true, []));
|
|
141
|
+
*
|
|
142
|
+
* ```
|
|
143
|
+
*
|
|
144
|
+
*/
|
|
145
|
+
symmetricDifference: typeof symmetricDifference;
|
|
146
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* 两个数组的交集
|
|
4
|
+
*
|
|
5
|
+
* @param a 数组 1️⃣
|
|
6
|
+
* @param b 数组 2️⃣
|
|
7
|
+
* @returns 返回两个数组的交集
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { intersection } from 'a-js-tools';
|
|
12
|
+
*
|
|
13
|
+
* const log = console.log;
|
|
14
|
+
*
|
|
15
|
+
*
|
|
16
|
+
* log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
|
|
17
|
+
* log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
|
|
18
|
+
*
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
export declare function intersection<T>(a: T[], b: T[]): T[];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* 对称差集 ( A △ B)
|
|
4
|
+
*
|
|
5
|
+
* @param a - 数组 a
|
|
6
|
+
* @param b - 数组 b
|
|
7
|
+
* @returns - 返回一个全新的数组
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { symmetricDifference } from 'a-js-tools';
|
|
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
|
+
*
|
|
23
|
+
* /// TypeError
|
|
24
|
+
* log(symmetricDifference(1, []));
|
|
25
|
+
* log(symmetricDifference(undefined, []));
|
|
26
|
+
* log(symmetricDifference(null, []));
|
|
27
|
+
* log(symmetricDifference('a', []));
|
|
28
|
+
* log(symmetricDifference(true, []));
|
|
29
|
+
*
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
export declare function symmetricDifference<T>(a: T[], b: T[]): T[];
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* 数组的并集
|
|
4
|
+
*
|
|
5
|
+
* <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
|
|
6
|
+
* @param arrays - 多个数组
|
|
7
|
+
* @returns 联合后的数组
|
|
8
|
+
*
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { union } from 'a-js-tools';
|
|
14
|
+
*
|
|
15
|
+
* const log = console.log;
|
|
16
|
+
*
|
|
17
|
+
* // []
|
|
18
|
+
* log(union());
|
|
19
|
+
*
|
|
20
|
+
* // [1, 2, 3]
|
|
21
|
+
* log(union([1, 2, 3]));
|
|
22
|
+
*
|
|
23
|
+
* // TypeError
|
|
24
|
+
* log(union([1, 2, 3], 'i'));
|
|
25
|
+
* log(union([1, 2, 3], undefined));
|
|
26
|
+
* log(union([1, 2, 3], null));
|
|
27
|
+
* log(union([1, 2, 3], 4));
|
|
28
|
+
* log(union([1, 2, 3], {}));
|
|
29
|
+
* log(union([1, 2, 3], false));
|
|
30
|
+
*
|
|
31
|
+
* // [1, 2, 3, 4, 6]
|
|
32
|
+
* log(union([1, 2, 3], [2, 4, 6]));
|
|
33
|
+
*
|
|
34
|
+
* // [1, 2, 3, 4, 6]
|
|
35
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
|
|
36
|
+
*
|
|
37
|
+
* // [1, 2, 3, 4, 6]
|
|
38
|
+
* log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
*/
|
|
42
|
+
export declare function union<T>(...arrays: T[][]): T[];
|
|
File without changes
|
|
File without changes
|
|
File without changes
|