@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.
Files changed (58) hide show
  1. package/CHANGELOG.md +170 -0
  2. package/LICENSE +15 -0
  3. package/README.md +51 -0
  4. package/cjs/array/difference.js +48 -0
  5. package/cjs/array/index.js +133 -0
  6. package/cjs/array/intersection.js +40 -0
  7. package/cjs/array/symmetricDifference.js +45 -0
  8. package/cjs/array/union.js +62 -0
  9. package/cjs/className.js +55 -0
  10. package/cjs/createBezier.js +135 -0
  11. package/cjs/getRandomNumber.js +70 -0
  12. package/cjs/getRandomString.js +143 -0
  13. package/cjs/index.js +40 -0
  14. package/cjs/isNode.js +23 -0
  15. package/cjs/object/createConstructor.js +59 -0
  16. package/cjs/performance.js +138 -0
  17. package/cjs/regexp/autoEscapedRegExp.js +44 -0
  18. package/cjs/regexp/escapeRegExp.js +22 -0
  19. package/cjs/regexp/parse.js +31 -0
  20. package/cjs/sleep.js +37 -0
  21. package/es/array/difference.d.ts +29 -0
  22. package/es/array/difference.js +46 -0
  23. package/es/array/index.d.ts +125 -0
  24. package/es/array/index.js +127 -0
  25. package/es/array/intersection.d.ts +17 -0
  26. package/es/array/intersection.js +38 -0
  27. package/es/array/symmetricDifference.d.ts +27 -0
  28. package/es/array/symmetricDifference.js +43 -0
  29. package/es/array/union.d.ts +39 -0
  30. package/es/array/union.js +60 -0
  31. package/es/className.d.ts +26 -0
  32. package/es/className.js +52 -0
  33. package/es/createBezier.d.ts +64 -0
  34. package/es/createBezier.js +133 -0
  35. package/es/getRandomNumber.d.ts +24 -0
  36. package/es/getRandomNumber.js +67 -0
  37. package/es/getRandomString.d.ts +73 -0
  38. package/es/getRandomString.js +141 -0
  39. package/es/index.d.ts +12 -0
  40. package/es/index.js +15 -0
  41. package/es/isNode.d.ts +8 -0
  42. package/es/isNode.js +20 -0
  43. package/es/object/createConstructor.d.ts +47 -0
  44. package/es/object/createConstructor.js +56 -0
  45. package/es/object/index.d.ts +2 -0
  46. package/es/performance.d.ts +61 -0
  47. package/es/performance.js +135 -0
  48. package/es/regexp/autoEscapedRegExp.d.ts +28 -0
  49. package/es/regexp/autoEscapedRegExp.js +42 -0
  50. package/es/regexp/escapeRegExp.d.ts +16 -0
  51. package/es/regexp/escapeRegExp.js +20 -0
  52. package/es/regexp/index.d.ts +2 -0
  53. package/es/regexp/parse.d.ts +6 -0
  54. package/es/regexp/parse.js +29 -0
  55. package/es/regexp/types.d.ts +10 -0
  56. package/es/sleep.d.ts +25 -0
  57. package/es/sleep.js +35 -0
  58. package/package.json +118 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,170 @@
1
+ # 更新日志
2
+
3
+ ## 2.0.5 (2026-6-11)
4
+
5
+ - 维护注释信息
6
+
7
+ ## 2.0.4 (2026-6-10)
8
+
9
+ ## 2.0.3 (2026-3-26)
10
+
11
+ ## 2.0.2 (2026-3-24)
12
+
13
+ - 维护注释信息
14
+
15
+ ## v2.0.1 (2025-12-26)
16
+
17
+ - 修复类型导出错误
18
+
19
+ ## v2.0.0 (2025-12-24)
20
+
21
+ ## v2.0.0-alpha.0 (2025-6-24)
22
+
23
+ - 修改了 `debounce` 和 `throttle` 方法的参数类型,该更改可能导致不兼容
24
+ - 修改 `isNode` 校验方式,防止在基于 'chromium' 浏览器插件的后台应用中校验错误
25
+
26
+ ## v1.0.13-beta.5 (2025-9-12)
27
+
28
+ 修复在无法使用 `globalThis.crypto.getRandomValues` 环境下获取随机字符串的长度错误
29
+
30
+ ## v1.0.13-beta.4 (2025-9-12)
31
+
32
+ for weChatMiniProgram
33
+
34
+ ## v1.0.13-beta.3 (2025-9-12)
35
+
36
+ for weChatMiniProgram
37
+
38
+ ## v1.0.13-beta.2 (2025-9-11)
39
+
40
+ for weChatMiniProgram
41
+
42
+ ## v1.0.13-beta.1 (2025-9-11)
43
+
44
+ for weChatMiniProgram
45
+
46
+ ## v1.0.13-beta.0 (2025-9-7)
47
+
48
+ for weChatMiniProgram
49
+
50
+ ## v1.0.12 (2025-8-4)
51
+
52
+ - 移除 `arguments` 在原型上的使用,因为在大多数时候,会编译成严格模式,导致报错
53
+
54
+ ```bash
55
+ constructor.prototype.arguments = Function.arguments;
56
+ ^
57
+
58
+ TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
59
+ ```
60
+
61
+ ## v1.0.11 (2025-8-4)
62
+
63
+ - 在使用 `createConstructor` 在 webpack 的环境下,发现非标准的函数对象的原型被写了,导致出现了使用函数原型异常的现象。但是为了统一处理,在 `createConstructor` 强制为原型设置原型方法。
64
+
65
+ ## v1.0.10 (2025-7-26)
66
+
67
+ - 整理文档
68
+
69
+ ## v1.0.10-test.0 (2025-7-24)
70
+
71
+ - 么事,测试 pnpm 发布
72
+
73
+ ## v1.0.9 (2025-7-19)
74
+
75
+ - 修复已知问题
76
+
77
+ ## v1.0.8 (2025-7-18)
78
+
79
+ - 优化了使用 `createConstructor` 的类型声明
80
+
81
+ ## v1.0.7 (2025-7-11)
82
+
83
+ - 使用 `getRandomString` 时判断是否是浏览器环境,环境判定有误,导致使用 `window.crypto` 使用有误。仙子啊直接使用 `globalThis.crypto` 而非环境判定。
84
+
85
+ ## v1.0.6 (2025-7-9)
86
+
87
+ - 更新了 `debounce` 和 `throttle` 的回调类型
88
+
89
+ ## v1.0.5 (2025-6-24)
90
+
91
+ 修改 `debounce` 和 `throttle` 的第二参数
92
+
93
+ ## v1.0.13-beta.5 (2025-9-12)
94
+
95
+ 修复在无法使用 `globalThis.crypto.getRandomValues` 环境下获取随机字符串的长度错误
96
+
97
+ ## v1.0.13-beta.4 (2025-9-12)
98
+
99
+ for weChatMiniProgram
100
+
101
+ ## v1.0.13-beta.3 (2025-9-12)
102
+
103
+ for weChatMiniProgram
104
+
105
+ ## v1.0.13-beta.2 (2025-9-11)
106
+
107
+ for weChatMiniProgram
108
+
109
+ ## v1.0.13-beta.1 (2025-9-11)
110
+
111
+ for weChatMiniProgram
112
+
113
+ ## v1.0.13-beta.0 (2025-9-7)
114
+
115
+ for weChatMiniProgram
116
+
117
+ ## v1.0.12 (2025-8-4)
118
+
119
+ - 移除 `arguments` 在原型上的使用,因为在大多数时候,会编译成严格模式,导致报错
120
+
121
+ ```bash
122
+ constructor.prototype.arguments = Function.arguments;
123
+ ^
124
+
125
+ TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
126
+ ```
127
+
128
+ ## v1.0.11 (2025-8-4)
129
+
130
+ - 在使用 `createConstructor` 在 webpack 的环境下,发现非标准的函数对象的原型被写了,导致出现了使用函数原型异常的现象。但是为了统一处理,在 `createConstructor` 强制为原型设置原型方法。
131
+
132
+ ## v1.0.10 (2025-7-26)
133
+
134
+ - 整理文档
135
+
136
+ ## v1.0.10-test.0 (2025-7-24)
137
+
138
+ - 么事,测试 pnpm 发布
139
+
140
+ ## v1.0.9 (2025-7-19)
141
+
142
+ - 修复已知问题
143
+
144
+ ## v1.0.8 (2025-7-18)
145
+
146
+ - 优化了使用 `createConstructor` 的类型声明
147
+
148
+ ## v1.0.7 (2025-7-11)
149
+
150
+ - 使用 `getRandomString` 时判断是否是浏览器环境,环境判定有误,导致使用 `window.crypto` 使用有误。仙子啊直接使用 `globalThis.crypto` 而非环境判定。
151
+
152
+ ## v1.0.6 (2025-7-9)
153
+
154
+ - 更新了 `debounce` 和 `throttle` 的回调类型
155
+
156
+ ## v1.0.5 (2025-6-24)
157
+
158
+ 修改 `debounce` 和 `throttle` 的第二参数
159
+
160
+ ## v1.0.2 (2025-6-19)
161
+
162
+ - 移除了 `createConstructor` 这个鸡肋
163
+
164
+ ## v1.0.1 (2025-6-15)
165
+
166
+ - 文档维护
167
+
168
+ ## v1.0.0 (5 🈷️ 24 日 2025 年)
169
+
170
+ - 没有说明,莫名其妙的更新
package/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ # MIT License
2
+
3
+ Copyright (c) <2024> <Mr.MudBean>
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # @mudbean/utils
2
+
3
+ [![version](<https://img.shields.io/npm/v/@mudbean/utils.svg?logo=npm&logoColor=rgb(0,0,0)&label=版本号&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/@mudbean/utils) [![issues 提交](<https://img.shields.io/badge/issues-提交-rgb(255,0,63)?logo=github>)](https://github.com/MrMudBean/utils/issues)
4
+ 一个纯函数的工具
5
+
6
+ mudbean
7
+
8
+ ## 安装
9
+
10
+ ```sh
11
+ npm install --save @mudbean/utils
12
+
13
+ # pnpm
14
+ pnpm add --save @mudbean/utils
15
+
16
+ # yarn
17
+ yarn add @mudbean/utils
18
+ ```
19
+
20
+ ## 纯函数
21
+
22
+ - `autoEscapedRegExp` 生成简单的正则表达式
23
+ - `createBezier` 构建简单的贝尔赛曲线
24
+ - `debounce` 防抖函数
25
+ - `escapeRegExp` 转义字符串为简单的正则表达式
26
+ - `getRandomFloat` 获取随机的浮点数
27
+ - `getRandomInt` 获取随机的整数
28
+ - `getRandomString` 获取随机字符串
29
+ - `isBrowser` 是否为浏览器环境
30
+ - `isNode` 是否为 Node 环境
31
+ - `sleep` 你的线程太累了,让它丫的睡一会吧
32
+ - `throttle` 节流函数
33
+
34
+ ## class 名称转化
35
+
36
+ - `toLowerCamelCase` 转化为小驼峰
37
+ - `toSplitCase` 转化为连接符分隔
38
+
39
+ ## 数组相关
40
+
41
+ - `intersection` 方法,计算两个数组的交集(两个数组共有的元素)
42
+ - `union` 方法,计算两个数组的并集(两个数组合并在一起并去重)
43
+ - `difference` 方法,计算两个数组的差集(以第一个数组为基准)
44
+ - `symmetricDifference` 方法,计算两个数组的对称差集(在两个数组都不共有的元素)
45
+ - `enArr` 对象,包含上面的方法
46
+
47
+ ## 状态
48
+
49
+ 此软件包是 `@mudbean` 生态系统的一部分。
50
+ 它使用严格的 TypeScript 编写,并通过 Rollup 构建进行验证。
51
+ 虽然单元测试较少,但 API 稳定,并在生产环境中大量使用。
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ var is = require('@mudbean/is');
4
+
5
+ /**
6
+ * ## 求给出的两个数组的差值(A - B)
7
+ *
8
+ * @param a - 第一个数组
9
+ * @param b - 第二个数组
10
+ * @throws {TypeError} 当两个参数有一个不是
11
+ * @description 当两个参数有一个不是数组时将抛出
12
+ * @returns 返回第一个参数相对第二个参数的差值
13
+ * @example
14
+ * ```ts
15
+ * import { difference} from '@mudbean/utils';
16
+ *
17
+ * const log = console.log;
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
+ function difference(a, b) {
34
+ if (!is.isArray(a) || !is.isArray(b)) {
35
+ throw new TypeError('参数需为数组');
36
+ }
37
+ if (is.isEmptyArray(a) || is.isEmptyArray(b)) {
38
+ return a;
39
+ }
40
+ /** 获取两个数组中长度较小的 */
41
+ // 参数有顺序要求
42
+ // const [shorter, longer] = a.length > b.length ? [b, a] : [a, b];
43
+ // const shorterSet = new Set(shorter);
44
+ const bSet = new Set(b);
45
+ return a.filter(i => !bSet.has(i));
46
+ }
47
+
48
+ exports.difference = difference;
@@ -0,0 +1,133 @@
1
+ 'use strict';
2
+
3
+ var intersection = require('./intersection.js');
4
+ var union = require('./union.js');
5
+ var difference = require('./difference.js');
6
+ var symmetricDifference = require('./symmetricDifference.js');
7
+
8
+ /**
9
+ * # 数组的一些方法
10
+ *
11
+ * - `union` 两个数组的并集(排除共有项)
12
+ * - `intersection` 两个数组的交集(共有项)
13
+ * - `difference` 两个数组的差集 (A - B)
14
+ * - `symmetricDifference` 对称差集 ( A △ B)
15
+ */
16
+ const enArr = {
17
+ /**
18
+ * ## 数组的并集
19
+ * <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
20
+ * @param arrays - 多个数组
21
+ * @returns 联合后的数组
22
+ * @example
23
+ *
24
+ * ```ts
25
+ * import { union } from '@mudbean/utils';
26
+ *
27
+ * const log = console.log;
28
+ *
29
+ * // []
30
+ * log(union());
31
+ *
32
+ * // [1, 2, 3]
33
+ * log(union([1, 2, 3]));
34
+ *
35
+ * // TypeError
36
+ * log(union([1, 2, 3], 'i'));
37
+ * log(union([1, 2, 3], undefined));
38
+ * log(union([1, 2, 3], null));
39
+ * log(union([1, 2, 3], 4));
40
+ * log(union([1, 2, 3], {}));
41
+ * log(union([1, 2, 3], false));
42
+ *
43
+ * // [1, 2, 3, 4, 6]
44
+ * log(union([1, 2, 3], [2, 4, 6]));
45
+ *
46
+ * // [1, 2, 3, 4, 6]
47
+ * log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
48
+ *
49
+ * // [1, 2, 3, 4, 6]
50
+ * log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
51
+ * ```
52
+ */
53
+ union: union.union,
54
+ /**
55
+ * ## 两个数组的交集
56
+ *
57
+ * @param a 数组 1️⃣
58
+ * @param b 数组 2️⃣
59
+ * @returns 返回两个数组的交集
60
+ * @example
61
+ * ```ts
62
+ * import { intersection } from '@mudbean/utils';
63
+ *
64
+ * const log = console.log;
65
+ *
66
+ * log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
67
+ * log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
68
+ * ```
69
+ */
70
+ intersection: intersection.intersection,
71
+ /**
72
+ * ## 求给出的两个数组的差值(A - B)
73
+ *
74
+ * @param a - 第一个数组
75
+ * @param b - 第二个数组
76
+ * @throws {TypeError} 当两个参数有一个不是
77
+ * @description 当两个参数有一个不是数组时将抛出
78
+ * @returns 返回第一个参数相对第二个参数的差值
79
+ * @example
80
+ * ```ts
81
+ * import { difference} from '@mudbean/utils';
82
+ *
83
+ * const log = console.log;
84
+ *
85
+ * log(difference([], [1, 2, 3])); // []
86
+ *
87
+ * log([1, 2, 3], []); //[1, 2, 3]
88
+ *
89
+ * log([1, 2, 3, 4, 5, 6, 7], [1, 2, 3, 4, 5]); //[6, 7]
90
+ *
91
+ * // 将抛出 TypeError
92
+ * log(difference([1, 2, 3], 'a'));
93
+ * log(difference([1, 2, 3], 1));
94
+ * log(difference([1, 2, 3], undefined));
95
+ * log(difference([1, 2, 3], null));
96
+ * log(difference([1, 2, 3], true));
97
+ * ```
98
+ */
99
+ difference: difference.difference,
100
+ /**
101
+ * ## 对称差集 ( A △ B)
102
+ *
103
+ * @param a - 数组 a
104
+ * @param b - 数组 b
105
+ * @returns - 返回一个全新的数组
106
+ * @example
107
+ * ```ts
108
+ * import { symmetricDifference } from '@mudbean/utils';
109
+ *
110
+ * const log = console.log;
111
+ *
112
+ * log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
113
+ *
114
+ * log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
115
+ *
116
+ * log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
117
+ *
118
+ * /// TypeError
119
+ * log(symmetricDifference(1, []));
120
+ * log(symmetricDifference(undefined, []));
121
+ * log(symmetricDifference(null, []));
122
+ * log(symmetricDifference('a', []));
123
+ * log(symmetricDifference(true, []));
124
+ * ```
125
+ */
126
+ symmetricDifference: symmetricDifference.symmetricDifference,
127
+ };
128
+
129
+ exports.intersection = intersection.intersection;
130
+ exports.union = union.union;
131
+ exports.difference = difference.difference;
132
+ exports.symmetricDifference = symmetricDifference.symmetricDifference;
133
+ exports.enArr = enArr;
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ var is = require('@mudbean/is');
4
+
5
+ /**
6
+ * # 两个数组的交集
7
+ *
8
+ * @param a 数组 1️⃣
9
+ * @param b 数组 2️⃣
10
+ * @returns 返回两个数组的交集
11
+ * @example
12
+ * ```ts
13
+ * import { intersection } from '@mudbean/utils';
14
+ *
15
+ * const log = console.log;
16
+ *
17
+ * log(intersection([1, 2, 3, 4], [2, 3])); // [2, 3]
18
+ * log(intersection([1, 3, 5, 7], [2, 3, 4])); // [3]
19
+ * ```
20
+ */
21
+ function intersection(a, b) {
22
+ if (!is.isArray(a) || !is.isArray(b)) {
23
+ throw new TypeError('参数必须是数组类型数据');
24
+ }
25
+ // 任意数组为空,则返回空数组
26
+ if (is.isEmptyArray(a) || is.isEmptyArray(b)) {
27
+ return [];
28
+ }
29
+ /**
30
+ * 在实际运算中, new Set() 的 开销为 O(n) ,filter 的开销也为 O(n)
31
+ *
32
+ * 但是以较短的数组创建 Set ,可以节省内存开销
33
+ */
34
+ const [shorter, longer] = a.length <= b.length ? [a, b] : [b, a];
35
+ // 提前创建工具 Set
36
+ const shortSet = new Set(shorter);
37
+ return longer.filter(item => shortSet.has(item));
38
+ }
39
+
40
+ exports.intersection = intersection;
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ var is = require('@mudbean/is');
4
+ var difference = require('./difference.js');
5
+
6
+ /**
7
+ * # 对称差集 ( A △ B)
8
+ *
9
+ * @param a - 数组 a
10
+ * @param b - 数组 b
11
+ * @returns - 返回一个全新的数组
12
+ * @example
13
+ * ```ts
14
+ * import { symmetricDifference } from '@mudbean/utils';
15
+ *
16
+ * const log = console.log;
17
+ *
18
+ * log(symmetricDifference([1, 2], [2, 3])); // [1, 3]
19
+ *
20
+ * log(symmetricDifference([1, 2, 3], [1, 2, 4])); // [3, 4]
21
+ *
22
+ * log(symmetricDifference([1, 2, 3], [1, 2, 3])); // []
23
+ *
24
+ * /// TypeError
25
+ * log(symmetricDifference(1, []));
26
+ * log(symmetricDifference(undefined, []));
27
+ * log(symmetricDifference(null, []));
28
+ * log(symmetricDifference('a', []));
29
+ * log(symmetricDifference(true, []));
30
+ * ```
31
+ */
32
+ function symmetricDifference(a, b) {
33
+ if (!is.isArray(a) || !is.isArray(b)) {
34
+ throw new TypeError('参数必须是数组');
35
+ }
36
+ if (is.isEmptyArray(a)) {
37
+ return [...b];
38
+ }
39
+ if (is.isEmptyArray(b)) {
40
+ return [...a];
41
+ }
42
+ return [...difference.difference(a, b), ...difference.difference(b, a)];
43
+ }
44
+
45
+ exports.symmetricDifference = symmetricDifference;
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ var is = require('@mudbean/is');
4
+
5
+ /**
6
+ * # 数组的并集
7
+ *
8
+ * <span style="color:#f36;">请注意,参数有不为数组时直接抛出 TypeError</span>
9
+ * @param arrays - 多个数组
10
+ * @returns 联合后的数组
11
+ * @example
12
+ *
13
+ * ```ts
14
+ * import { union } from '@mudbean/is';
15
+ *
16
+ * const log = console.log;
17
+ *
18
+ * // []
19
+ * log(union());
20
+ *
21
+ * // [1, 2, 3]
22
+ * log(union([1, 2, 3]));
23
+ *
24
+ * // TypeError
25
+ * log(union([1, 2, 3], 'i'));
26
+ * log(union([1, 2, 3], undefined));
27
+ * log(union([1, 2, 3], null));
28
+ * log(union([1, 2, 3], 4));
29
+ * log(union([1, 2, 3], {}));
30
+ * log(union([1, 2, 3], false));
31
+ *
32
+ * // [1, 2, 3, 4, 6]
33
+ * log(union([1, 2, 3], [2, 4, 6]));
34
+ *
35
+ * // [1, 2, 3, 4, 6]
36
+ * log(union([1, 2, 3], [2, 4, 6], [1, 2, 3]));
37
+ *
38
+ * // [1, 2, 3, 4, 6]
39
+ * log(union([1, 2, 3], [2, 4, 6], [1, 2, 3], [1, 2, 3]));
40
+ * ```
41
+ *
42
+ */
43
+ function union(...arrays) {
44
+ if (is.isEmptyArray(arrays)) {
45
+ return [];
46
+ }
47
+ if (arrays.some(i => !is.isArray(i))) {
48
+ throw new TypeError('参数必须都是数组形式的元素');
49
+ }
50
+ if (arrays.length === 1) {
51
+ return [...arrays[0]];
52
+ }
53
+ const resultSet = new Set();
54
+ for (const array of arrays) {
55
+ for (const item of array) {
56
+ resultSet.add(item);
57
+ }
58
+ }
59
+ return Array.from(resultSet);
60
+ }
61
+
62
+ exports.union = union;
@@ -0,0 +1,55 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 驼峰命名与连字符命名法的互换
5
+ */
6
+ /**
7
+ * # 连字符连接转化为小/大驼峰命名法
8
+ *
9
+ * @param str 待转化文本
10
+ * @param dividingType 连字符,缺省值为 "-"
11
+ * @param initial 是否转换第一个字符。默认值为 false (小驼峰类型)
12
+ * @returns 驼峰命名法字符串(e.g. “helloWorld”)
13
+ */
14
+ function toLowerCamelCase(
15
+ /** 待转化文本 */
16
+ str,
17
+ /** 连字符,缺省值为 "-" */
18
+ dividingType = '-',
19
+ /** 是否转换第一个字符。默认值为 false (小驼峰类型) */
20
+ initial = false) {
21
+ let result = str;
22
+ /**
23
+ * 匹配规则
24
+ *
25
+ * - 匹配到分隔符,将分隔符后面的字符转化为大写
26
+ */
27
+ const template = /[\\]|[\^]|[?]|[-]|[.]|[(]|[)]|[|]|[[]\[\]]|[{]|[}]|[+]|[*]|[$]/;
28
+ /**
29
+ * 转化首字符
30
+ * @param _str
31
+ * @param _dividingType
32
+ */
33
+ const toTransform = (_str, _dividingType) => _str.replace(new RegExp(`${template.test(_dividingType) ? `\\${_dividingType}` : _dividingType}([a-zA-Z])`, 'g'), (match, p1) => p1.toUpperCase());
34
+ // 多分隔符转化
35
+ dividingType
36
+ .split('')
37
+ .forEach((item) => (result = toTransform(result, item)));
38
+ return initial
39
+ ? result.replace(/^./, (match) => match.toUpperCase())
40
+ : result;
41
+ }
42
+ /**
43
+ * # 驼峰命名法转化为连字符连接
44
+ *
45
+ * @param str 待转化文本
46
+ * @param dividingType 分割符
47
+ * @returns 分割符转化的文本 (e.g. 'hello-world')
48
+ */
49
+ function toSplitCase(str, dividingType = '-') {
50
+ const result = str.replace(/[A-Z]/g, (match) => dividingType.concat(match.toLowerCase()));
51
+ return result.startsWith(dividingType) ? result.substring(1) : result;
52
+ }
53
+
54
+ exports.toLowerCamelCase = toLowerCamelCase;
55
+ exports.toSplitCase = toSplitCase;