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.
- package/LICENSE +2 -2
- package/README.md +1 -1
- package/cjs/array/difference.js +51 -0
- package/cjs/array/index.js +154 -0
- package/cjs/array/intersection.js +45 -0
- package/cjs/array/symmetricDifference.js +51 -0
- package/cjs/array/union.js +65 -0
- package/cjs/className.js +58 -0
- package/cjs/getRandomNumber.js +76 -0
- package/cjs/getRandomString.js +126 -0
- package/cjs/index.js +38 -0
- package/cjs/isNode.js +27 -0
- package/cjs/object/createConstructor.js +58 -0
- package/cjs/performance.js +124 -0
- package/cjs/regexp/autoEscapedRegExp.js +48 -0
- package/cjs/regexp/escapeRegExp.js +24 -0
- package/cjs/regexp/parse.js +32 -0
- package/cjs/sleep.js +38 -0
- package/es/array/difference.js +49 -0
- package/es/array/index.js +148 -0
- package/es/array/intersection.js +43 -0
- package/es/array/symmetricDifference.js +49 -0
- package/es/array/union.js +63 -0
- package/es/className.js +55 -0
- package/es/getRandomNumber.js +73 -0
- package/es/getRandomString.js +124 -0
- package/es/index.js +14 -0
- package/es/isNode.js +24 -0
- package/es/object/createConstructor.js +55 -0
- package/es/performance.js +121 -0
- package/es/regexp/autoEscapedRegExp.js +46 -0
- package/es/regexp/escapeRegExp.js +22 -0
- package/es/regexp/parse.js +30 -0
- package/es/sleep.js +36 -0
- package/es/src/index.d.ts +11 -0
- package/es/src/object/index.d.ts +2 -0
- package/package.json +91 -25
- package/index.cjs.js +0 -898
- package/index.d.ts +0 -9
- package/src/index.d.ts +0 -3
- /package/{src → es/src}/array/difference.d.ts +0 -0
- /package/{src → es/src}/array/index.d.ts +0 -0
- /package/{src → es/src}/array/intersection.d.ts +0 -0
- /package/{src → es/src}/array/symmetricDifference.d.ts +0 -0
- /package/{src → es/src}/array/union.d.ts +0 -0
- /package/{src → es/src}/className.d.ts +0 -0
- /package/{src → es/src}/getRandomNumber.d.ts +0 -0
- /package/{src → es/src}/getRandomString.d.ts +0 -0
- /package/{src → es/src}/isNode.d.ts +0 -0
- /package/{src → es/src}/object/createConstructor.d.ts +0 -0
- /package/{src → es/src}/performance.d.ts +0 -0
- /package/{src → es/src}/regexp/autoEscapedRegExp.d.ts +0 -0
- /package/{src → es/src}/regexp/escapeRegExp.d.ts +0 -0
- /package/{src → es/src}/regexp/index.d.ts +0 -0
- /package/{src → es/src}/regexp/parse.d.ts +0 -0
- /package/{src → es/src}/regexp/types.d.ts +0 -0
- /package/{src → es/src}/sleep.d.ts +0 -0
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) <2024> <
|
|
3
|
+
Copyright (c) <2024> <Mr.MudBean>
|
|
4
4
|
|
|
5
5
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
6
|
purpose with or without fee is hereby granted, provided that the above
|
|
@@ -16,7 +16,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
16
16
|
|
|
17
17
|
# MIT 许可证
|
|
18
18
|
|
|
19
|
-
版权所有 (c) [2024] [
|
|
19
|
+
版权所有 (c) [2024] [泥豆君]
|
|
20
20
|
|
|
21
21
|
特此免费授予任何获得本软件及相关文档文件(以下简称“软件”)副本的人不受限制地处置该软件的权利,包括不受限制地使用、复制、修改、合并、发布、分发、再许可和/或出售该软件副本的权利,并允许向其提供该软件的人这样做,但须遵守以下条件:
|
|
22
22
|
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 一点点 🤏 js 函数
|
|
2
2
|
|
|
3
|
-
[&label=版本号&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/a-js-tools) [?logo=github>)](https://github.com/
|
|
3
|
+
[&label=版本号&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>)](https://www.npmjs.com/package/a-js-tools) [?logo=github>)](https://github.com/MrMudBean/a-js-tools/issues)
|
|
4
4
|
一个纯函数的工具
|
|
5
5
|
|
|
6
6
|
## 安装
|
|
@@ -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 (aTypeOfJs.isEmptyArray(a) || aTypeOfJs.isEmptyArray(b)) {
|
|
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.js');
|
|
4
|
+
var union = require('./union.js');
|
|
5
|
+
var difference = require('./difference.js');
|
|
6
|
+
var symmetricDifference = require('./symmetricDifference.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;
|
|
@@ -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 (aTypeOfJs.isEmptyArray(a) || aTypeOfJs.isEmptyArray(b)) {
|
|
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.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
|
+
}
|
|
42
|
+
if (aTypeOfJs.isEmptyArray(a)) {
|
|
43
|
+
return [...b];
|
|
44
|
+
}
|
|
45
|
+
if (aTypeOfJs.isEmptyArray(b)) {
|
|
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 (aTypeOfJs.isEmptyArray(arrays)) {
|
|
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/cjs/className.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 驼峰命名与连字符命名法的互换
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
* @module @a-js-tools/class-name
|
|
8
|
+
* @license MIT
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* 连字符连接转化为小/大驼峰命名法
|
|
13
|
+
*
|
|
14
|
+
* @param str 待转化文本
|
|
15
|
+
* @param dividingType 连字符,缺省值为 "-"
|
|
16
|
+
* @param initial 是否转换第一个字符。默认值为 false (小驼峰类型)
|
|
17
|
+
* @returns 驼峰命名法字符串(e.g. “helloWorld”)
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
function toLowerCamelCase(
|
|
21
|
+
/** 待转化文本 */
|
|
22
|
+
str,
|
|
23
|
+
/** 连字符,缺省值为 "-" */
|
|
24
|
+
dividingType = '-',
|
|
25
|
+
/** 是否转换第一个字符。默认值为 false (小驼峰类型) */
|
|
26
|
+
initial = false) {
|
|
27
|
+
let result = str;
|
|
28
|
+
/**
|
|
29
|
+
* 匹配规则
|
|
30
|
+
*
|
|
31
|
+
* - 匹配到分隔符,将分隔符后面的字符转化为大写
|
|
32
|
+
*/
|
|
33
|
+
const template = /[\\]|[\^]|[?]|[-]|[.]|[(]|[)]|[|]|[[]\[\]]|[{]|[}]|[+]|[*]|[$]/;
|
|
34
|
+
/** 转化首字符 */
|
|
35
|
+
const toTransform = (_str, _dividingType) => _str.replace(new RegExp(`${template.test(_dividingType) ? `\\${_dividingType}` : _dividingType}([a-zA-Z])`, 'g'), (match, p1) => p1.toUpperCase());
|
|
36
|
+
// 多分隔符转化
|
|
37
|
+
dividingType
|
|
38
|
+
.split('')
|
|
39
|
+
.forEach((item) => (result = toTransform(result, item)));
|
|
40
|
+
return initial
|
|
41
|
+
? result.replace(/^./, (match) => match.toUpperCase())
|
|
42
|
+
: result;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 驼峰命名法转化为连字符连接
|
|
46
|
+
*
|
|
47
|
+
* @param str 待转化文本
|
|
48
|
+
* @param dividingType 分割符
|
|
49
|
+
* @returns 分割符转化的文本 (e.g. 'hello-world')
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
function toSplitCase(str, dividingType = '-') {
|
|
53
|
+
const result = str.replace(/[A-Z]/g, (match) => dividingType.concat(match.toLowerCase()));
|
|
54
|
+
return result.startsWith(dividingType) ? result.substring(1) : result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
exports.toLowerCamelCase = toLowerCamelCase;
|
|
58
|
+
exports.toSplitCase = toSplitCase;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var aTypeOfJs = require('a-type-of-js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 过去随机数
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
* @module @a-js-tools/get-random-number
|
|
10
|
+
* @license MIT
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* 获取一个随机的整数类型
|
|
15
|
+
*
|
|
16
|
+
* 您可以传入两个参数并获取它们之间的任意数字,返回值<span style="color:#ff0;">*会包含端值*</span>
|
|
17
|
+
*
|
|
18
|
+
* 如果只传递一个参数,则如果提供的值为负数,则得到一个小于(或大于)该数字的整数
|
|
19
|
+
*
|
|
20
|
+
* @param max 较大值 ,不允许为`NaN`
|
|
21
|
+
* @param min 较小值,不允许为 `NaN`
|
|
22
|
+
* @returns 任意的整数
|
|
23
|
+
*/
|
|
24
|
+
function getRandomInt(max = 1, min = 0) {
|
|
25
|
+
// 判断是否为 NaN 或 不是数字
|
|
26
|
+
if (!isFinite(max) ||
|
|
27
|
+
!isFinite(min) ||
|
|
28
|
+
aTypeOfJs.isNaN(max) ||
|
|
29
|
+
aTypeOfJs.isNaN(min) ||
|
|
30
|
+
!aTypeOfJs.isNumber(max) ||
|
|
31
|
+
!aTypeOfJs.isNumber(min)) {
|
|
32
|
+
throw new TypeError('getRandomInt: max or min is NaN or is not a number');
|
|
33
|
+
}
|
|
34
|
+
/** 获取最小值 */
|
|
35
|
+
let _min = Math.ceil(Number(min)),
|
|
36
|
+
/** 获取最大值 */
|
|
37
|
+
_max = Math.floor(Number(max));
|
|
38
|
+
// 两值相等时,直接返回最大值
|
|
39
|
+
if (_max === _min)
|
|
40
|
+
return _max;
|
|
41
|
+
// 两值交换
|
|
42
|
+
if (_min > _max)
|
|
43
|
+
[_max, _min] = [_min, _max];
|
|
44
|
+
return Math.round(Math.random() * (_max - _min) + _min);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* 获取任意的浮点数
|
|
49
|
+
*
|
|
50
|
+
* 您可以传入两个参数并获取它们之间的任意数字
|
|
51
|
+
*
|
|
52
|
+
* 如果只传入一个参数,则如果提供的值为负数,则获取小于(或大于)该数字的浮点数
|
|
53
|
+
*
|
|
54
|
+
* @param max 较大数,缺省值为 1
|
|
55
|
+
* @param min 较小值,缺省值为 0
|
|
56
|
+
* @returns 任意的浮点数
|
|
57
|
+
*/
|
|
58
|
+
function getRandomFloat(max = 1, min = 0) {
|
|
59
|
+
// 判断是否为 NaN 或 不是数字
|
|
60
|
+
if (!isFinite(max) ||
|
|
61
|
+
!isFinite(min) ||
|
|
62
|
+
aTypeOfJs.isNaN(max) ||
|
|
63
|
+
aTypeOfJs.isNaN(min) ||
|
|
64
|
+
!aTypeOfJs.isNumber(max) ||
|
|
65
|
+
!aTypeOfJs.isNumber(min)) {
|
|
66
|
+
throw new TypeError('getRandomInt: max or min is NaN or is not a number');
|
|
67
|
+
}
|
|
68
|
+
if (max == min)
|
|
69
|
+
max++;
|
|
70
|
+
if (min > max)
|
|
71
|
+
[max, min] = [min, max];
|
|
72
|
+
return Math.random() * (max - min) + min;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
exports.getRandomFloat = getRandomFloat;
|
|
76
|
+
exports.getRandomInt = getRandomInt;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var aTypeOfJs = require('a-type-of-js');
|
|
4
|
+
var createConstructor = require('./object/createConstructor.js');
|
|
5
|
+
var getRandomNumber = require('./getRandomNumber.js');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 获取随机字符串
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
* @module @a-js-tools/get-random-string
|
|
12
|
+
* @license MIT
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* 获取简单的随机字符串
|
|
17
|
+
*
|
|
18
|
+
* @param options - 字符串长度
|
|
19
|
+
* @returns - 随机字符串
|
|
20
|
+
*
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
function getRandomString(options) {
|
|
24
|
+
// 验证输入参数
|
|
25
|
+
if (
|
|
26
|
+
// 参数类型错误
|
|
27
|
+
(!aTypeOfJs.isPlainObject(options) && !aTypeOfJs.isNumber(options)) ||
|
|
28
|
+
// 参数为 NaN
|
|
29
|
+
(aTypeOfJs.isNumber(options) && aTypeOfJs.isNaN(options)) ||
|
|
30
|
+
// 参数为数字时为无穷大
|
|
31
|
+
(aTypeOfJs.isNumber(options) && !isFinite(options)) ||
|
|
32
|
+
// 参数为数字时为非整数
|
|
33
|
+
(aTypeOfJs.isNumber(options) && !Number.isInteger(options)) ||
|
|
34
|
+
// 参数为数字时为负数
|
|
35
|
+
(aTypeOfJs.isNumber(options) && Number.isInteger(options) && options < 1) ||
|
|
36
|
+
// 参数为数值然而却小于 1
|
|
37
|
+
(aTypeOfJs.isNumber(options) && options < 1) ||
|
|
38
|
+
// 参数为对象但是 length 属性非数值
|
|
39
|
+
(aTypeOfJs.isPlainObject(options) &&
|
|
40
|
+
(!aTypeOfJs.isNumber(options.length) ||
|
|
41
|
+
options.length < 1 ||
|
|
42
|
+
!Number.isInteger(options.length))))
|
|
43
|
+
throw new TypeError('参数类型错误 ❌ (getRandomString)');
|
|
44
|
+
const initOptions = {
|
|
45
|
+
length: 32,
|
|
46
|
+
chars: 'abcdefghijklmnopqrstuvwxyz',
|
|
47
|
+
chars2: '0123456789',
|
|
48
|
+
chars3: '!@#$%^&*()_+~`|}{[]:;?><,./-=',
|
|
49
|
+
type: 'string',
|
|
50
|
+
includeUppercaseLetters: false,
|
|
51
|
+
includeNumbers: false,
|
|
52
|
+
includeSpecial: false,
|
|
53
|
+
};
|
|
54
|
+
/// 生成 UUID
|
|
55
|
+
if (initOptions.type === 'uuid')
|
|
56
|
+
return crypto.randomUUID();
|
|
57
|
+
// 验证输入参数
|
|
58
|
+
if (aTypeOfJs.isNumber(options) && Number.isInteger(options) && options > 0)
|
|
59
|
+
createConstructor.ObjectAssign(initOptions, { length: options });
|
|
60
|
+
if (aTypeOfJs.isPlainObject(options)) {
|
|
61
|
+
createConstructor.ObjectAssign(initOptions, options);
|
|
62
|
+
initOptions.length = initOptions.length < 1 ? 32 : initOptions.length;
|
|
63
|
+
}
|
|
64
|
+
/** 生成随机字符串 */
|
|
65
|
+
const templateCharsArr = initOptions.chars.split('');
|
|
66
|
+
// 添加大写字母
|
|
67
|
+
if (initOptions.includeUppercaseLetters)
|
|
68
|
+
interleaveString(templateCharsArr, initOptions.chars.toUpperCase());
|
|
69
|
+
// 添加数字
|
|
70
|
+
if (initOptions.includeNumbers)
|
|
71
|
+
interleaveString(templateCharsArr, initOptions.chars2);
|
|
72
|
+
// 添加特殊字符
|
|
73
|
+
if (initOptions.includeSpecial)
|
|
74
|
+
interleaveString(templateCharsArr, initOptions.chars3);
|
|
75
|
+
/** 结果字符串 */
|
|
76
|
+
let result = '';
|
|
77
|
+
/** 混淆后的字符串 */
|
|
78
|
+
const str = templateCharsArr.join('');
|
|
79
|
+
/** 混淆后字符长度 */
|
|
80
|
+
const strLen = str.length;
|
|
81
|
+
if (globalThis && globalThis.crypto && globalThis.crypto.getRandomValues) {
|
|
82
|
+
// 使用密码学安全的随机数生成器
|
|
83
|
+
const bytes = globalThis.crypto.getRandomValues(new Uint8Array(initOptions.length));
|
|
84
|
+
/** 获取最后的 chars 数据 */
|
|
85
|
+
// 循环遍历
|
|
86
|
+
bytes.forEach(byte => (result += str[byte % strLen]));
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
for (let i = 0; i < initOptions.length; i++)
|
|
90
|
+
result += str[getRandomNumber.getRandomInt(strLen - 1)];
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
*
|
|
94
|
+
* 字符串交叉函数
|
|
95
|
+
*
|
|
96
|
+
* 非线形串交叉,对相交叉
|
|
97
|
+
*
|
|
98
|
+
* @param str1 - 字符串1
|
|
99
|
+
* @param str2 - 字符串2
|
|
100
|
+
* @returns - 交叉后的字符串
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* interleaveString('abc', '123') // 'a1b2c3'
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
function interleaveString(str1, str2) {
|
|
107
|
+
const str1Length = str1.length, str2Length = str2.length;
|
|
108
|
+
const maxLength = Math.max(str1Length, str2Length);
|
|
109
|
+
for (let i = 0; i < maxLength; i++) {
|
|
110
|
+
if (i < str1Length && !aTypeOfJs.isUndefined(str2[i])) {
|
|
111
|
+
str1[i] += str2[i];
|
|
112
|
+
}
|
|
113
|
+
else if (i < str2Length) {
|
|
114
|
+
str1[i] = str2[i];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/// 结果字符串不包含字符
|
|
119
|
+
if (!/[a-zA-Z]/.test(result))
|
|
120
|
+
return String.fromCharCode(getRandomNumber.getRandomInt(97, 122)).concat(result.slice(1));
|
|
121
|
+
while (!/^[a-zA-Z]$/.test(result[0]))
|
|
122
|
+
result = result.slice(1) + result[0];
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
exports.getRandomString = getRandomString;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var className = require('./className.js');
|
|
4
|
+
var createConstructor = require('./object/createConstructor.js');
|
|
5
|
+
var getRandomNumber = require('./getRandomNumber.js');
|
|
6
|
+
var getRandomString = require('./getRandomString.js');
|
|
7
|
+
var performance = require('./performance.js');
|
|
8
|
+
var escapeRegExp = require('./regexp/escapeRegExp.js');
|
|
9
|
+
var autoEscapedRegExp = require('./regexp/autoEscapedRegExp.js');
|
|
10
|
+
var isNode = require('./isNode.js');
|
|
11
|
+
var index = require('./array/index.js');
|
|
12
|
+
var sleep = require('./sleep.js');
|
|
13
|
+
var intersection = require('./array/intersection.js');
|
|
14
|
+
var union = require('./array/union.js');
|
|
15
|
+
var difference = require('./array/difference.js');
|
|
16
|
+
var symmetricDifference = require('./array/symmetricDifference.js');
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
exports.toLowerCamelCase = className.toLowerCamelCase;
|
|
21
|
+
exports.toSplitCase = className.toSplitCase;
|
|
22
|
+
exports.ObjectAssign = createConstructor.ObjectAssign;
|
|
23
|
+
exports.createConstructor = createConstructor.createConstructor;
|
|
24
|
+
exports.getRandomFloat = getRandomNumber.getRandomFloat;
|
|
25
|
+
exports.getRandomInt = getRandomNumber.getRandomInt;
|
|
26
|
+
exports.getRandomString = getRandomString.getRandomString;
|
|
27
|
+
exports.debounce = performance.debounce;
|
|
28
|
+
exports.throttle = performance.throttle;
|
|
29
|
+
exports.escapeRegExp = escapeRegExp.escapeRegExp;
|
|
30
|
+
exports.autoEscapedRegExp = autoEscapedRegExp.autoEscapedRegExp;
|
|
31
|
+
exports.isBrowser = isNode.isBrowser;
|
|
32
|
+
exports.isNode = isNode.isNode;
|
|
33
|
+
exports.enArr = index.enArr;
|
|
34
|
+
exports.sleep = sleep.sleep;
|
|
35
|
+
exports.intersection = intersection.intersection;
|
|
36
|
+
exports.union = union.union;
|
|
37
|
+
exports.difference = difference.difference;
|
|
38
|
+
exports.symmetricDifference = symmetricDifference.symmetricDifference;
|