a-js-tools 1.0.13-beta.0 → 1.0.13-beta.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/index.cjs.js +881 -35
- package/index.d.ts +2 -3
- package/package.json +1 -1
- package/src/object/createConstructor.d.ts +2 -0
- package/src/array/difference.cjs.js +0 -49
- package/src/array/difference.mjs.js +0 -47
- package/src/array/index.cjs.js +0 -154
- package/src/array/index.mjs.js +0 -148
- package/src/array/intersection.cjs.js +0 -43
- package/src/array/intersection.mjs.js +0 -41
- package/src/array/symmetricDifference.cjs.js +0 -48
- package/src/array/symmetricDifference.mjs.js +0 -46
- package/src/array/union.cjs.js +0 -61
- package/src/array/union.mjs.js +0 -59
- package/src/className.cjs.js +0 -58
- package/src/className.mjs.js +0 -55
- package/src/getRandomNumber.cjs.js +0 -76
- package/src/getRandomNumber.mjs.js +0 -73
- package/src/getRandomString.cjs.js +0 -107
- package/src/getRandomString.mjs.js +0 -105
- package/src/isNode.cjs.js +0 -23
- package/src/isNode.mjs.js +0 -20
- package/src/object/createConstructor.cjs.js +0 -51
- package/src/object/createConstructor.mjs.js +0 -49
- package/src/performance.cjs.js +0 -116
- package/src/performance.mjs.js +0 -113
- package/src/regexp/autoEscapedRegExp.cjs.js +0 -48
- package/src/regexp/autoEscapedRegExp.mjs.js +0 -46
- package/src/regexp/escapeRegExp.cjs.js +0 -24
- package/src/regexp/escapeRegExp.mjs.js +0 -22
- package/src/regexp/parse.cjs.js +0 -32
- package/src/regexp/parse.mjs.js +0 -30
- package/src/sleep.cjs.js +0 -38
- package/src/sleep.mjs.js +0 -36
package/index.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
export type { CreateConstructor } from './src/object/createConstructor';
|
|
2
|
+
export { createConstructor, ObjectAssign, } from './src/object/createConstructor';
|
|
2
3
|
export { toLowerCamelCase, toSplitCase, getRandomFloat, getRandomInt, getRandomString, } from './src/index';
|
|
3
4
|
export { throttle, debounce } from './src/performance';
|
|
4
5
|
export type { DebounceAndThrottleReturnType } from './src/performance';
|
|
5
6
|
export { escapeRegExp, autoEscapedRegExp } from './src/regexp';
|
|
6
7
|
export { isBrowser, isNode } from './src/isNode';
|
|
7
8
|
export { intersection, enArr, union, difference, symmetricDifference, } from './src/array/';
|
|
8
|
-
export { createConstructor };
|
|
9
|
-
export type { CreateConstructor };
|
|
10
9
|
export { sleep } from './src/sleep';
|
package/package.json
CHANGED
|
@@ -45,3 +45,5 @@ export interface CreateConstructor<T, Args extends unknown[] = unknown[]> {
|
|
|
45
45
|
*
|
|
46
46
|
*/
|
|
47
47
|
export declare function createConstructor<T, Args extends unknown[] = unknown[]>(constructor: (...argumentList: Args) => T): CreateConstructor<T, Args>;
|
|
48
|
+
/** 对象的 assign 用法 */
|
|
49
|
+
export declare function ObjectAssign(target: Record<string, unknown>, bar: Record<string, unknown>): Record<string, unknown>;
|
|
@@ -1,49 +0,0 @@
|
|
|
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
|
-
if (aTypeOfJs.isEmptyArray(a) || aTypeOfJs.isEmptyArray(b))
|
|
40
|
-
return a;
|
|
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
|
-
exports.difference = difference;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { isArray, isEmptyArray } 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
|
-
if (isEmptyArray(a) || isEmptyArray(b))
|
|
38
|
-
return a;
|
|
39
|
-
/** 获取两个数组中长度较小的 */
|
|
40
|
-
// 参数有顺序要求
|
|
41
|
-
// const [shorter, longer] = a.length > b.length ? [b, a] : [a, b];
|
|
42
|
-
// const shorterSet = new Set(shorter);
|
|
43
|
-
const bSet = new Set(b);
|
|
44
|
-
return a.filter(i => !bSet.has(i));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export { difference };
|
package/src/array/index.cjs.js
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var intersection = require('./intersection.cjs.js');
|
|
4
|
-
var union = require('./union.cjs.js');
|
|
5
|
-
var difference = require('./difference.cjs.js');
|
|
6
|
-
var symmetricDifference = require('./symmetricDifference.cjs.js');
|
|
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;
|
package/src/array/index.mjs.js
DELETED
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import { intersection } from './intersection.mjs.js';
|
|
2
|
-
import { union } from './union.mjs.js';
|
|
3
|
-
import { difference } from './difference.mjs.js';
|
|
4
|
-
import { symmetricDifference } from './symmetricDifference.mjs.js';
|
|
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 };
|
|
@@ -1,43 +0,0 @@
|
|
|
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
|
-
if (aTypeOfJs.isEmptyArray(a) || aTypeOfJs.isEmptyArray(b))
|
|
31
|
-
return [];
|
|
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
|
-
exports.intersection = intersection;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { isArray, isEmptyArray } 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
|
-
if (isEmptyArray(a) || isEmptyArray(b))
|
|
29
|
-
return [];
|
|
30
|
-
/**
|
|
31
|
-
* 在实际运算中, new Set() 的 开销为 O(n) ,filter 的开销也为 O(n)
|
|
32
|
-
*
|
|
33
|
-
* 但是以较短的数组创建 Set ,可以节省内存开销
|
|
34
|
-
*/
|
|
35
|
-
const [shorter, longer] = a.length <= b.length ? [a, b] : [b, a];
|
|
36
|
-
// 提前创建工具 Set
|
|
37
|
-
const shortSet = new Set(shorter);
|
|
38
|
-
return longer.filter(item => shortSet.has(item));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export { intersection };
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var aTypeOfJs = require('a-type-of-js');
|
|
4
|
-
var difference = require('./difference.cjs.js');
|
|
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
|
-
if (aTypeOfJs.isEmptyArray(a))
|
|
42
|
-
return [...b];
|
|
43
|
-
if (aTypeOfJs.isEmptyArray(b))
|
|
44
|
-
return [...a];
|
|
45
|
-
return [...difference.difference(a, b), ...difference.difference(b, a)];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
exports.symmetricDifference = symmetricDifference;
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { isArray, isEmptyArray } from 'a-type-of-js';
|
|
2
|
-
import { difference } from './difference.mjs.js';
|
|
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
|
-
if (isEmptyArray(a))
|
|
40
|
-
return [...b];
|
|
41
|
-
if (isEmptyArray(b))
|
|
42
|
-
return [...a];
|
|
43
|
-
return [...difference(a, b), ...difference(b, a)];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export { symmetricDifference };
|
package/src/array/union.cjs.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
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 (aTypeOfJs.isEmptyArray(arrays))
|
|
48
|
-
return [];
|
|
49
|
-
if (arrays.some(i => !aTypeOfJs.isArray(i)))
|
|
50
|
-
throw new TypeError('参数必须都是数组形式的元素');
|
|
51
|
-
if (arrays.length === 1)
|
|
52
|
-
return [...arrays[0]];
|
|
53
|
-
const resultSet = new Set();
|
|
54
|
-
for (const array of arrays) {
|
|
55
|
-
for (const item of array)
|
|
56
|
-
resultSet.add(item);
|
|
57
|
-
}
|
|
58
|
-
return Array.from(resultSet);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
exports.union = union;
|