a-js-tools 1.0.13-beta.5 → 2.0.0

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.
Files changed (57) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +1 -1
  3. package/cjs/array/difference.js +51 -0
  4. package/cjs/array/index.js +154 -0
  5. package/cjs/array/intersection.js +45 -0
  6. package/cjs/array/symmetricDifference.js +51 -0
  7. package/cjs/array/union.js +65 -0
  8. package/cjs/className.js +58 -0
  9. package/cjs/getRandomNumber.js +76 -0
  10. package/cjs/getRandomString.js +126 -0
  11. package/cjs/index.js +38 -0
  12. package/cjs/isNode.js +27 -0
  13. package/cjs/object/createConstructor.js +58 -0
  14. package/cjs/performance.js +124 -0
  15. package/cjs/regexp/autoEscapedRegExp.js +48 -0
  16. package/cjs/regexp/escapeRegExp.js +24 -0
  17. package/cjs/regexp/parse.js +32 -0
  18. package/cjs/sleep.js +38 -0
  19. package/es/array/difference.js +49 -0
  20. package/es/array/index.js +148 -0
  21. package/es/array/intersection.js +43 -0
  22. package/es/array/symmetricDifference.js +49 -0
  23. package/es/array/union.js +63 -0
  24. package/es/className.js +55 -0
  25. package/es/getRandomNumber.js +73 -0
  26. package/es/getRandomString.js +124 -0
  27. package/es/index.js +14 -0
  28. package/es/isNode.js +24 -0
  29. package/es/object/createConstructor.js +55 -0
  30. package/es/performance.js +121 -0
  31. package/es/regexp/autoEscapedRegExp.js +46 -0
  32. package/es/regexp/escapeRegExp.js +22 -0
  33. package/es/regexp/parse.js +30 -0
  34. package/es/sleep.js +36 -0
  35. package/es/src/index.d.ts +11 -0
  36. package/es/src/object/index.d.ts +2 -0
  37. package/package.json +91 -25
  38. package/index.cjs.js +0 -898
  39. package/index.d.ts +0 -9
  40. package/src/index.d.ts +0 -3
  41. /package/{src → es/src}/array/difference.d.ts +0 -0
  42. /package/{src → es/src}/array/index.d.ts +0 -0
  43. /package/{src → es/src}/array/intersection.d.ts +0 -0
  44. /package/{src → es/src}/array/symmetricDifference.d.ts +0 -0
  45. /package/{src → es/src}/array/union.d.ts +0 -0
  46. /package/{src → es/src}/className.d.ts +0 -0
  47. /package/{src → es/src}/getRandomNumber.d.ts +0 -0
  48. /package/{src → es/src}/getRandomString.d.ts +0 -0
  49. /package/{src → es/src}/isNode.d.ts +0 -0
  50. /package/{src → es/src}/object/createConstructor.d.ts +0 -0
  51. /package/{src → es/src}/performance.d.ts +0 -0
  52. /package/{src → es/src}/regexp/autoEscapedRegExp.d.ts +0 -0
  53. /package/{src → es/src}/regexp/escapeRegExp.d.ts +0 -0
  54. /package/{src → es/src}/regexp/index.d.ts +0 -0
  55. /package/{src → es/src}/regexp/parse.d.ts +0 -0
  56. /package/{src → es/src}/regexp/types.d.ts +0 -0
  57. /package/{src → es/src}/sleep.d.ts +0 -0
package/cjs/isNode.js ADDED
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ var aTypeOfJs = require('a-type-of-js');
4
+
5
+ /**
6
+ *
7
+ * 判断当前环境是否为 node 环境
8
+ *
9
+ */
10
+ function isNode() {
11
+ return !aTypeOfJs.isUndefined((globalThis &&
12
+ globalThis.process &&
13
+ globalThis.process.versions &&
14
+ globalThis.process.versions.node) ||
15
+ undefined);
16
+ }
17
+ /**
18
+ *
19
+ * 是否为浏览器环境
20
+ *
21
+ */
22
+ function isBrowser() {
23
+ return !isNode();
24
+ }
25
+
26
+ exports.isBrowser = isBrowser;
27
+ exports.isNode = isNode;
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ *
5
+ * 构建一个 Constructor 构造函数
6
+ *
7
+ * 接收一个构造函数,然后返回 TS 能识别的构造函数自身
8
+ *
9
+ * 函数本身并没有执行任何逻辑,仅是简单的返回了实参自身
10
+ *
11
+ * 而经过该函数的包装,构造函数成了能够被 TS 识别为可用 new 实例的构造函数
12
+ *
13
+ * @param constructor - 传入一个构造函数
14
+ * @returns 返回传入的构造函数
15
+ *
16
+ * ```ts
17
+ * import { createConstructor } from "a-js-tools";
18
+ *
19
+ * type Tom = {
20
+ * a: number
21
+ * }
22
+ *
23
+ * function _Tom (this: TomType): Tom {
24
+ * this.a = 1;
25
+ *
26
+ * return this;
27
+ * }
28
+ *
29
+ * // 逻辑上没有错,但是会造成 ts 显示
30
+ * // 其目标缺少构造签名的 "new" 表达式隐式具有 "any" 类型。ts(7009)
31
+ * const a = new _Tom();
32
+ *
33
+ * const tomConstructor = createConstructor(_tom);
34
+ *
35
+ * const b = new tomConstructor(); // 这时就不会显示错误
36
+ *
37
+ * ```
38
+ *
39
+ */
40
+ function createConstructor(constructor) {
41
+ constructor.prototype.apply = Function.apply;
42
+ constructor.prototype.bind = Function.bind;
43
+ constructor.prototype.call = Function.call;
44
+ constructor.prototype.length = Function.length;
45
+ // constructor.prototype.arguments = Function.arguments;
46
+ constructor.prototype.name = Function.name;
47
+ constructor.prototype.toString = Function.toString;
48
+ return constructor;
49
+ }
50
+ /** 对象的 assign 用法 */
51
+ function ObjectAssign(target, bar) {
52
+ const keys = Object.keys(bar);
53
+ keys.forEach(key => (target[key] = bar[key]));
54
+ return target;
55
+ }
56
+
57
+ exports.ObjectAssign = ObjectAssign;
58
+ exports.createConstructor = createConstructor;
@@ -0,0 +1,124 @@
1
+ 'use strict';
2
+
3
+ var aTypeOfJs = require('a-type-of-js');
4
+ var isFunction = require('a-type-of-js/isFunction');
5
+ var isNumber = require('a-type-of-js/isNumber');
6
+
7
+ /**
8
+ * 防抖和节流
9
+ *
10
+ * @packageDocumentation
11
+ * @module @a-js-tools/performance
12
+ * @license MIT
13
+ */
14
+ /**
15
+ *
16
+ * 防抖
17
+ *
18
+ * @param callback 回调函数
19
+ * @param options 延迟时间(毫秒),默认 200 (ms) 或包含 this 的配置
20
+ * @returns 返回的闭包函数
21
+ * @example
22
+ *
23
+ * ```ts
24
+ * const debounce = (callback: Function, delay = 300) => {
25
+ * let timer: any = null
26
+ * return (...args: any[]) => {
27
+ * clearTimeout(timer)
28
+ * }
29
+ * }
30
+ *
31
+ */
32
+ function debounce(callback, options = 200) {
33
+ if (!isFunction.isFunction(callback))
34
+ throw new TypeError('callback must be a function');
35
+ if (isNumber.isNumber(options))
36
+ options = {
37
+ delay: options,
38
+ this: null,
39
+ };
40
+ if (aTypeOfJs.isUndefined(options.delay) ||
41
+ !isFinite(options.delay) ||
42
+ options.delay < 0)
43
+ // 强制转换非数值
44
+ options.delay = 200;
45
+ /** 定时器返回的 id */
46
+ let timeoutId;
47
+ const clear = () => {
48
+ if (timeoutId) {
49
+ clearTimeout(timeoutId);
50
+ timeoutId = undefined;
51
+ }
52
+ };
53
+ const result = (...args) => {
54
+ clear();
55
+ timeoutId = setTimeout(() => {
56
+ try {
57
+ const _this = options && options.this ? options.this : null;
58
+ // 由于 Reflect.apply 并不能在 ES5 中使用,所以我们并不能保证能执行成功
59
+ callback.apply(_this, args);
60
+ // Reflect.apply(callback, options?.this ?? null, args);
61
+ }
62
+ catch (error) {
63
+ console.log('Debounce callback throw an error', error);
64
+ }
65
+ }, Math.max(options.delay || 5, 5));
66
+ };
67
+ result.cancel = () => clear();
68
+ return result;
69
+ }
70
+ /**
71
+ * 节流
72
+ *
73
+ * @param callback 回调函数
74
+ * @param options 延迟时间(毫秒),默认 200 (ms) 或设置 this
75
+ * @returns 返回的闭包函数
76
+ */
77
+ function throttle(callback, options = 200) {
78
+ if (!isFunction.isFunction(callback))
79
+ throw new TypeError('callback must be a function');
80
+ if (isNumber.isNumber(options))
81
+ options = {
82
+ delay: options,
83
+ this: null,
84
+ };
85
+ if (aTypeOfJs.isUndefined(options.delay) ||
86
+ !isFinite(options.delay) ||
87
+ options.delay < 0)
88
+ // 强制转换非数值
89
+ options.delay = 200;
90
+ /** 延迟控制插销 */
91
+ let inThrottle = false;
92
+ /** 延迟控制 */
93
+ let timeoutId = null;
94
+ const delay = options && options.delay ? options.delay : 5;
95
+ const throttled = (...args) => {
96
+ if (inThrottle)
97
+ return;
98
+ try {
99
+ const _this = options && options.this ? options.this : null;
100
+ callback.apply(_this, args);
101
+ }
102
+ catch (error) {
103
+ console.error('Throttle 执行回调抛出问题', error);
104
+ }
105
+ inThrottle = true;
106
+ if (!aTypeOfJs.isNull(timeoutId))
107
+ clearTimeout(timeoutId);
108
+ timeoutId = setTimeout(() => {
109
+ inThrottle = false;
110
+ timeoutId = null;
111
+ }, Math.max(delay, 5));
112
+ };
113
+ throttled.cancel = () => {
114
+ if (!aTypeOfJs.isNull(timeoutId)) {
115
+ clearTimeout(timeoutId);
116
+ }
117
+ inThrottle = false;
118
+ timeoutId = null;
119
+ };
120
+ return throttled;
121
+ }
122
+
123
+ exports.debounce = debounce;
124
+ exports.throttle = throttle;
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ var aTypeOfJs = require('a-type-of-js');
4
+ var escapeRegExp = require('./escapeRegExp.js');
5
+ var parse = require('./parse.js');
6
+
7
+ /**
8
+ *
9
+ * ## 适用于简单的文本字符串自动转化为简单模式正则表达式
10
+ *
11
+ * *若字符串包含且需保留字符类、组、反向引用、量词等时,该方法可能不适用*
12
+ *
13
+ * @param pattern 待转化的文本字符串
14
+ * @param options 转化选项。 为文本字符串时,默认为正则表达式的标志,还可指定是否匹配开头或结尾
15
+ * @returns 正则表达式
16
+ * @example
17
+ *
18
+ * ```ts
19
+ * import { autoEscapedRegExp } from 'a-regexp';
20
+ *
21
+ * autoEscapedRegExp('abc'); // => /abc/
22
+ * autoEscapedRegExp('abc', 'gim'); // => /abc/gim
23
+ * autoEscapedRegExp('abc', { flags: 'g', start: true }); // => /^abc/g
24
+ * autoEscapedRegExp('abc', { flags: 'g', end: true }); // => /abc$/g
25
+ * autoEscapedRegExp('abc', { start: true, end: true }); // => /^abc$/
26
+ *
27
+ * // 转化特殊字符类、组、反向引用、量词等
28
+ * // 无法保留匹配规则
29
+ * autoEscapedRegExp('a-zA-Z0-9'); // => /a-zA-Z0-9/
30
+ * autoEscapedRegExp('a-zA-Z0-9', 'g'); // => /a-zA-Z0-9/g
31
+ * autoEscapedRegExp('[a-zA-Z0-9]'); // => /\[a-zA-Z0-9\]/
32
+ * autoEscapedRegExp('^[a-zA-Z0-9]+$'); // => /\^\[a-zA-Z0-9\]\$/
33
+ *
34
+ * ```
35
+ *
36
+ */
37
+ function autoEscapedRegExp(pattern, options) {
38
+ if (!aTypeOfJs.isString(pattern))
39
+ throw new TypeError('pattern must be a 字符串');
40
+ pattern = escapeRegExp.escapeRegExp(pattern);
41
+ /** 简单转化 */
42
+ if (aTypeOfJs.isUndefined(options))
43
+ return new RegExp(pattern);
44
+ options = parse.parse(options);
45
+ return new RegExp(`${options.start ? '^' : ''}${pattern}${options.end ? '$' : ''}`, options.flags);
46
+ }
47
+
48
+ exports.autoEscapedRegExp = autoEscapedRegExp;
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ *
5
+ * 将一个字符串转化为符合正则要求的字符串
6
+ *
7
+ * @param str 需要转义的字符串
8
+ * @requires escapeRegExp 转化后字符串
9
+ * @example
10
+ *
11
+ * ```ts
12
+ * import { escapeRegExp } from 'a-js-tools';
13
+ *
14
+ * escapeRegExp('a.b.c'); // 'a\\.b\\.c'
15
+ * escapeRegExp('a\\.b\\.c'); // 'a\\\\.b\\\\.c'
16
+ * escapeRegExp('[a-z]'); // '\\[a-z\\]'
17
+ * escapeRegExp('^abc$'); // '\\^abc\\$'
18
+ * ```
19
+ */
20
+ function escapeRegExp(str) {
21
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
22
+ }
23
+
24
+ exports.escapeRegExp = escapeRegExp;
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ var aTypeOfJs = require('a-type-of-js');
4
+
5
+ /**
6
+ *
7
+ * 解析 options
8
+ *
9
+ */
10
+ function parse(options) {
11
+ // 处理 options
12
+ if (aTypeOfJs.isString(options)) {
13
+ options = {
14
+ flags: options,
15
+ };
16
+ }
17
+ // 处理 flags
18
+ if (!aTypeOfJs.isString(options.flags))
19
+ options.flags = '';
20
+ else {
21
+ // 需求是保留字符串中的某一部分,使用
22
+ const regexp = /[migsuy]/g;
23
+ const matchResult = options.flags.match(regexp);
24
+ if (aTypeOfJs.isNull(matchResult))
25
+ options.flags = '';
26
+ else
27
+ options.flags = [...new Set(matchResult)].join('');
28
+ }
29
+ return options;
30
+ }
31
+
32
+ exports.parse = parse;
package/cjs/sleep.js ADDED
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ var isNumber = require('a-type-of-js/isNumber');
4
+
5
+ /**
6
+ *
7
+ * ## 线程休息
8
+ *
9
+ * 但从调用到执行完毕总是与期望的时间并不相吻合,除非执行是线型的(也不保证时间的严格性)
10
+ *
11
+ * - 宏任务:整体代码、setTimeout、DOM 事件回调、requestAnimationFrame、setImmediate、setInterval、I/O操作、UI渲染等
12
+ * - 微任务:Promise的then/catch/finally、process.nextTick(Node.js)、MutationObserver、queueMicrotask(显示添加微任务)等
13
+ *
14
+ * <span style="color:#ff0;">*Node.js中的process.nextTick优先级高于其他微任务*</span>
15
+ *
16
+ * @param delay 睡觉时长(机器时间,毫秒为单位)
17
+ * @returns 🈳
18
+ * @example
19
+ *
20
+ * ```ts
21
+ * import { sleep } from 'a-js-tools';
22
+ *
23
+ * console.log(Date.now()); // 1748058118471
24
+ * await sleep(1000);
25
+ * console.log(Date.now()); // 1748058119473
26
+ *
27
+ * ```
28
+ *
29
+ */
30
+ async function sleep(delay = 1000) {
31
+ if (!isFinite(delay) || delay < 0)
32
+ throw new TypeError('delay 应该是一个正常的数值');
33
+ if (isNumber.isZero(delay))
34
+ return Promise.resolve();
35
+ return new Promise(resolve => setTimeout(resolve, delay));
36
+ }
37
+
38
+ exports.sleep = sleep;
@@ -0,0 +1,49 @@
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
+ }
38
+ if (isEmptyArray(a) || isEmptyArray(b)) {
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.js';
2
+ import { union } from './union.js';
3
+ import { difference } from './difference.js';
4
+ import { symmetricDifference } from './symmetricDifference.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 };
@@ -0,0 +1,43 @@
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
+ // 任意数组为空,则返回空数组
29
+ if (isEmptyArray(a) || isEmptyArray(b)) {
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, isEmptyArray } from 'a-type-of-js';
2
+ import { difference } from './difference.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
+ }
40
+ if (isEmptyArray(a)) {
41
+ return [...b];
42
+ }
43
+ if (isEmptyArray(b)) {
44
+ return [...a];
45
+ }
46
+ return [...difference(a, b), ...difference(b, a)];
47
+ }
48
+
49
+ export { symmetricDifference };