a-js-tools 2.0.0-alpha.0 → 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 +4 -4
- package/{src/array/difference.cjs → cjs/array/difference.js} +4 -2
- package/{src/array/index.cjs → cjs/array/index.js} +4 -4
- package/{src/array/intersection.cjs → cjs/array/intersection.js} +4 -2
- package/{src/array/symmetricDifference.cjs → cjs/array/symmetricDifference.js} +1 -1
- package/{src/className.cjs → cjs/className.js} +3 -3
- package/{src/getRandomNumber.cjs → cjs/getRandomNumber.js} +5 -5
- package/{src/getRandomString.cjs → cjs/getRandomString.js} +32 -24
- package/{index.cjs → cjs/index.js} +17 -16
- package/{src/isNode.cjs → cjs/isNode.js} +5 -1
- package/{src/object/createConstructor.cjs → cjs/object/createConstructor.js} +14 -0
- package/{src/performance.cjs → cjs/performance.js} +34 -32
- package/{src/regexp/autoEscapedRegExp.cjs → cjs/regexp/autoEscapedRegExp.js} +4 -6
- package/{src/regexp/parse.cjs → cjs/regexp/parse.js} +3 -6
- package/{src/sleep.cjs → cjs/sleep.js} +2 -2
- package/{src/array/difference.mjs → es/array/difference.js} +4 -2
- package/{src/array/index.mjs → es/array/index.js} +4 -4
- package/{src/array/intersection.mjs → es/array/intersection.js} +4 -2
- package/{src/array/symmetricDifference.mjs → es/array/symmetricDifference.js} +1 -1
- package/{src/className.mjs → es/className.js} +3 -3
- package/{src/getRandomNumber.mjs → es/getRandomNumber.js} +5 -5
- package/{src/getRandomString.mjs → es/getRandomString.js} +32 -24
- package/es/index.js +14 -0
- package/{src/isNode.mjs → es/isNode.js} +5 -1
- package/{src/object/createConstructor.mjs → es/object/createConstructor.js} +14 -1
- package/{src/performance.mjs → es/performance.js} +35 -33
- package/{src/regexp/autoEscapedRegExp.mjs → es/regexp/autoEscapedRegExp.js} +4 -6
- package/{src/regexp/parse.mjs → es/regexp/parse.js} +3 -6
- package/{src/sleep.mjs → es/sleep.js} +1 -1
- package/{src → es/src}/getRandomNumber.d.ts +1 -1
- package/es/src/index.d.ts +11 -0
- package/{src → es/src}/object/createConstructor.d.ts +2 -0
- package/es/src/object/index.d.ts +2 -0
- package/es/src/performance.d.ts +50 -0
- package/package.json +92 -26
- package/index.d.ts +0 -10
- package/index.mjs +0 -14
- package/src/index.d.ts +0 -3
- package/src/performance.d.ts +0 -52
- /package/{src/array/union.cjs → cjs/array/union.js} +0 -0
- /package/{src/regexp/escapeRegExp.cjs → cjs/regexp/escapeRegExp.js} +0 -0
- /package/{src/array/union.mjs → es/array/union.js} +0 -0
- /package/{src/regexp/escapeRegExp.mjs → es/regexp/escapeRegExp.js} +0 -0
- /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}/getRandomString.d.ts +0 -0
- /package/{src → es/src}/isNode.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) [&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
|
## 安装
|
|
@@ -11,8 +11,8 @@ npm install --save a-js-tools
|
|
|
11
11
|
|
|
12
12
|
## 纯函数
|
|
13
13
|
|
|
14
|
-
- `
|
|
15
|
-
- `
|
|
14
|
+
- `debounce` 防抖函数
|
|
15
|
+
- `throttle` 节流函数
|
|
16
16
|
- `getRandomInt` 获取随机的整数
|
|
17
17
|
- `getRandomFloat` 获取随机的浮点数
|
|
18
18
|
- `getRandomString` 获取随机字符串
|
|
@@ -37,4 +37,4 @@ npm install --save a-js-tools
|
|
|
37
37
|
|
|
38
38
|
## 查看文档
|
|
39
39
|
|
|
40
|
-
查看 [https://earthnut.dev/a-js-tools](https://earthnut.dev/a-js-tools)
|
|
40
|
+
查看 [https://earthnut.dev/npm/a-js-tools](https://earthnut.dev/npm/a-js-tools)
|
|
@@ -34,10 +34,12 @@ var aTypeOfJs = require('a-type-of-js');
|
|
|
34
34
|
*
|
|
35
35
|
*/
|
|
36
36
|
function difference(a, b) {
|
|
37
|
-
if (
|
|
37
|
+
if (!aTypeOfJs.isArray(a) || !aTypeOfJs.isArray(b)) {
|
|
38
38
|
throw new TypeError('参数需为数组');
|
|
39
|
-
|
|
39
|
+
}
|
|
40
|
+
if (aTypeOfJs.isEmptyArray(a) || aTypeOfJs.isEmptyArray(b)) {
|
|
40
41
|
return a;
|
|
42
|
+
}
|
|
41
43
|
/** 获取两个数组中长度较小的 */
|
|
42
44
|
// 参数有顺序要求
|
|
43
45
|
// const [shorter, longer] = a.length > b.length ? [b, a] : [a, b];
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var intersection = require('./intersection.
|
|
4
|
-
var union = require('./union.
|
|
5
|
-
var difference = require('./difference.
|
|
6
|
-
var symmetricDifference = require('./symmetricDifference.
|
|
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
7
|
|
|
8
8
|
/**
|
|
9
9
|
*
|
|
@@ -24,11 +24,13 @@ var aTypeOfJs = require('a-type-of-js');
|
|
|
24
24
|
*
|
|
25
25
|
*/
|
|
26
26
|
function intersection(a, b) {
|
|
27
|
-
if (!aTypeOfJs.isArray(a) || !aTypeOfJs.isArray(b))
|
|
27
|
+
if (!aTypeOfJs.isArray(a) || !aTypeOfJs.isArray(b)) {
|
|
28
28
|
throw new TypeError('参数必须是数组类型数据');
|
|
29
|
+
}
|
|
29
30
|
// 任意数组为空,则返回空数组
|
|
30
|
-
if (
|
|
31
|
+
if (aTypeOfJs.isEmptyArray(a) || aTypeOfJs.isEmptyArray(b)) {
|
|
31
32
|
return [];
|
|
33
|
+
}
|
|
32
34
|
/**
|
|
33
35
|
* 在实际运算中, new Set() 的 开销为 O(n) ,filter 的开销也为 O(n)
|
|
34
36
|
*
|
|
@@ -34,9 +34,9 @@ initial = false) {
|
|
|
34
34
|
/** 转化首字符 */
|
|
35
35
|
const toTransform = (_str, _dividingType) => _str.replace(new RegExp(`${template.test(_dividingType) ? `\\${_dividingType}` : _dividingType}([a-zA-Z])`, 'g'), (match, p1) => p1.toUpperCase());
|
|
36
36
|
// 多分隔符转化
|
|
37
|
-
dividingType
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
dividingType
|
|
38
|
+
.split('')
|
|
39
|
+
.forEach((item) => (result = toTransform(result, item)));
|
|
40
40
|
return initial
|
|
41
41
|
? result.replace(/^./, (match) => match.toUpperCase())
|
|
42
42
|
: result;
|
|
@@ -13,7 +13,7 @@ var aTypeOfJs = require('a-type-of-js');
|
|
|
13
13
|
*
|
|
14
14
|
* 获取一个随机的整数类型
|
|
15
15
|
*
|
|
16
|
-
*
|
|
16
|
+
* 您可以传入两个参数并获取它们之间的任意数字,返回值<span style="color:#ff0;">*会包含端值*</span>
|
|
17
17
|
*
|
|
18
18
|
* 如果只传递一个参数,则如果提供的值为负数,则得到一个小于(或大于)该数字的整数
|
|
19
19
|
*
|
|
@@ -35,12 +35,12 @@ function getRandomInt(max = 1, min = 0) {
|
|
|
35
35
|
let _min = Math.ceil(Number(min)),
|
|
36
36
|
/** 获取最大值 */
|
|
37
37
|
_max = Math.floor(Number(max));
|
|
38
|
-
|
|
39
|
-
if (_min > _max)
|
|
40
|
-
[_max, _min] = [_min, _max];
|
|
41
|
-
//** 两值相等时,直接返回最大值 */
|
|
38
|
+
// 两值相等时,直接返回最大值
|
|
42
39
|
if (_max === _min)
|
|
43
40
|
return _max;
|
|
41
|
+
// 两值交换
|
|
42
|
+
if (_min > _max)
|
|
43
|
+
[_max, _min] = [_min, _max];
|
|
44
44
|
return Math.round(Math.random() * (_max - _min) + _min);
|
|
45
45
|
}
|
|
46
46
|
/**
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var aTypeOfJs = require('a-type-of-js');
|
|
4
|
-
var
|
|
4
|
+
var createConstructor = require('./object/createConstructor.js');
|
|
5
|
+
var getRandomNumber = require('./getRandomNumber.js');
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* 获取随机字符串
|
|
@@ -38,9 +39,8 @@ function getRandomString(options) {
|
|
|
38
39
|
(aTypeOfJs.isPlainObject(options) &&
|
|
39
40
|
(!aTypeOfJs.isNumber(options.length) ||
|
|
40
41
|
options.length < 1 ||
|
|
41
|
-
!Number.isInteger(options.length))))
|
|
42
|
+
!Number.isInteger(options.length))))
|
|
42
43
|
throw new TypeError('参数类型错误 ❌ (getRandomString)');
|
|
43
|
-
}
|
|
44
44
|
const initOptions = {
|
|
45
45
|
length: 32,
|
|
46
46
|
chars: 'abcdefghijklmnopqrstuvwxyz',
|
|
@@ -52,40 +52,43 @@ function getRandomString(options) {
|
|
|
52
52
|
includeSpecial: false,
|
|
53
53
|
};
|
|
54
54
|
/// 生成 UUID
|
|
55
|
-
if (initOptions.type === 'uuid')
|
|
55
|
+
if (initOptions.type === 'uuid')
|
|
56
56
|
return crypto.randomUUID();
|
|
57
|
-
|
|
58
|
-
if (aTypeOfJs.isNumber(options) && Number.isInteger(options) && options > 0)
|
|
59
|
-
|
|
60
|
-
Object.assign(initOptions, { length: options });
|
|
61
|
-
}
|
|
57
|
+
// 验证输入参数
|
|
58
|
+
if (aTypeOfJs.isNumber(options) && Number.isInteger(options) && options > 0)
|
|
59
|
+
createConstructor.ObjectAssign(initOptions, { length: options });
|
|
62
60
|
if (aTypeOfJs.isPlainObject(options)) {
|
|
63
|
-
|
|
61
|
+
createConstructor.ObjectAssign(initOptions, options);
|
|
64
62
|
initOptions.length = initOptions.length < 1 ? 32 : initOptions.length;
|
|
65
63
|
}
|
|
66
64
|
/** 生成随机字符串 */
|
|
67
65
|
const templateCharsArr = initOptions.chars.split('');
|
|
68
66
|
// 添加大写字母
|
|
69
|
-
if (initOptions.includeUppercaseLetters)
|
|
67
|
+
if (initOptions.includeUppercaseLetters)
|
|
70
68
|
interleaveString(templateCharsArr, initOptions.chars.toUpperCase());
|
|
71
|
-
}
|
|
72
69
|
// 添加数字
|
|
73
|
-
if (initOptions.includeNumbers)
|
|
70
|
+
if (initOptions.includeNumbers)
|
|
74
71
|
interleaveString(templateCharsArr, initOptions.chars2);
|
|
75
|
-
}
|
|
76
72
|
// 添加特殊字符
|
|
77
|
-
if (initOptions.includeSpecial)
|
|
73
|
+
if (initOptions.includeSpecial)
|
|
78
74
|
interleaveString(templateCharsArr, initOptions.chars3);
|
|
79
|
-
|
|
80
|
-
// 使用密码学安全的随机数生成器
|
|
81
|
-
const bytes = isNode.isBrowser() && window.crypto
|
|
82
|
-
? window.crypto.getRandomValues(new Uint8Array(initOptions.length))
|
|
83
|
-
: global.crypto.getRandomValues(new Uint8Array(initOptions.length));
|
|
75
|
+
/** 结果字符串 */
|
|
84
76
|
let result = '';
|
|
85
|
-
/**
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
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
|
+
}
|
|
89
92
|
/**
|
|
90
93
|
*
|
|
91
94
|
* 字符串交叉函数
|
|
@@ -112,6 +115,11 @@ function getRandomString(options) {
|
|
|
112
115
|
}
|
|
113
116
|
}
|
|
114
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];
|
|
115
123
|
return result;
|
|
116
124
|
}
|
|
117
125
|
|
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var getRandomString = require('./
|
|
7
|
-
var performance = require('./
|
|
8
|
-
var escapeRegExp = require('./
|
|
9
|
-
var autoEscapedRegExp = require('./
|
|
10
|
-
var isNode = require('./
|
|
11
|
-
var index = require('./
|
|
12
|
-
var sleep = require('./
|
|
13
|
-
var intersection = require('./
|
|
14
|
-
var union = require('./
|
|
15
|
-
var difference = require('./
|
|
16
|
-
var symmetricDifference = require('./
|
|
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
17
|
|
|
18
18
|
|
|
19
19
|
|
|
20
|
+
exports.toLowerCamelCase = className.toLowerCamelCase;
|
|
21
|
+
exports.toSplitCase = className.toSplitCase;
|
|
22
|
+
exports.ObjectAssign = createConstructor.ObjectAssign;
|
|
20
23
|
exports.createConstructor = createConstructor.createConstructor;
|
|
21
24
|
exports.getRandomFloat = getRandomNumber.getRandomFloat;
|
|
22
25
|
exports.getRandomInt = getRandomNumber.getRandomInt;
|
|
23
|
-
exports.toLowerCamelCase = className.toLowerCamelCase;
|
|
24
|
-
exports.toSplitCase = className.toSplitCase;
|
|
25
26
|
exports.getRandomString = getRandomString.getRandomString;
|
|
26
27
|
exports.debounce = performance.debounce;
|
|
27
28
|
exports.throttle = performance.throttle;
|
|
@@ -8,7 +8,11 @@ var aTypeOfJs = require('a-type-of-js');
|
|
|
8
8
|
*
|
|
9
9
|
*/
|
|
10
10
|
function isNode() {
|
|
11
|
-
return !aTypeOfJs.isUndefined(globalThis
|
|
11
|
+
return !aTypeOfJs.isUndefined((globalThis &&
|
|
12
|
+
globalThis.process &&
|
|
13
|
+
globalThis.process.versions &&
|
|
14
|
+
globalThis.process.versions.node) ||
|
|
15
|
+
undefined);
|
|
12
16
|
}
|
|
13
17
|
/**
|
|
14
18
|
*
|
|
@@ -38,7 +38,21 @@
|
|
|
38
38
|
*
|
|
39
39
|
*/
|
|
40
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;
|
|
41
48
|
return constructor;
|
|
42
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
|
+
}
|
|
43
56
|
|
|
57
|
+
exports.ObjectAssign = ObjectAssign;
|
|
44
58
|
exports.createConstructor = createConstructor;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
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');
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* 防抖和节流
|
|
@@ -13,7 +15,8 @@ var aTypeOfJs = require('a-type-of-js');
|
|
|
13
15
|
*
|
|
14
16
|
* 防抖
|
|
15
17
|
*
|
|
16
|
-
* @param
|
|
18
|
+
* @param callback 回调函数
|
|
19
|
+
* @param options 延迟时间(毫秒),默认 200 (ms) 或包含 this 的配置
|
|
17
20
|
* @returns 返回的闭包函数
|
|
18
21
|
* @example
|
|
19
22
|
*
|
|
@@ -26,19 +29,17 @@ var aTypeOfJs = require('a-type-of-js');
|
|
|
26
29
|
* }
|
|
27
30
|
*
|
|
28
31
|
*/
|
|
29
|
-
function debounce(options) {
|
|
30
|
-
if (
|
|
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))
|
|
31
36
|
options = {
|
|
32
|
-
|
|
33
|
-
delay: 200,
|
|
37
|
+
delay: options,
|
|
34
38
|
this: null,
|
|
35
39
|
};
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (aTypeOfJs.isUndefined(options.delay))
|
|
40
|
-
options.delay = 200;
|
|
41
|
-
if (!isFinite(options.delay) || options.delay < 0)
|
|
40
|
+
if (aTypeOfJs.isUndefined(options.delay) ||
|
|
41
|
+
!isFinite(options.delay) ||
|
|
42
|
+
options.delay < 0)
|
|
42
43
|
// 强制转换非数值
|
|
43
44
|
options.delay = 200;
|
|
44
45
|
/** 定时器返回的 id */
|
|
@@ -53,60 +54,61 @@ function debounce(options) {
|
|
|
53
54
|
clear();
|
|
54
55
|
timeoutId = setTimeout(() => {
|
|
55
56
|
try {
|
|
56
|
-
|
|
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);
|
|
57
61
|
}
|
|
58
62
|
catch (error) {
|
|
59
63
|
console.log('Debounce callback throw an error', error);
|
|
60
64
|
}
|
|
61
|
-
}, Math.max(options.delay
|
|
62
|
-
};
|
|
63
|
-
result.cancel = () => {
|
|
64
|
-
clear();
|
|
65
|
+
}, Math.max(options.delay || 5, 5));
|
|
65
66
|
};
|
|
67
|
+
result.cancel = () => clear();
|
|
66
68
|
return result;
|
|
67
69
|
}
|
|
68
70
|
/**
|
|
69
71
|
* 节流
|
|
70
72
|
*
|
|
71
|
-
* @param
|
|
73
|
+
* @param callback 回调函数
|
|
74
|
+
* @param options 延迟时间(毫秒),默认 200 (ms) 或设置 this
|
|
72
75
|
* @returns 返回的闭包函数
|
|
73
76
|
*/
|
|
74
|
-
function throttle(options) {
|
|
75
|
-
if (
|
|
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))
|
|
76
81
|
options = {
|
|
77
|
-
|
|
78
|
-
delay: 200,
|
|
82
|
+
delay: options,
|
|
79
83
|
this: null,
|
|
80
84
|
};
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (aTypeOfJs.isUndefined(options.delay))
|
|
85
|
-
options.delay = 200;
|
|
86
|
-
if (!isFinite(options.delay) || options.delay < 0)
|
|
85
|
+
if (aTypeOfJs.isUndefined(options.delay) ||
|
|
86
|
+
!isFinite(options.delay) ||
|
|
87
|
+
options.delay < 0)
|
|
87
88
|
// 强制转换非数值
|
|
88
89
|
options.delay = 200;
|
|
89
90
|
/** 延迟控制插销 */
|
|
90
91
|
let inThrottle = false;
|
|
91
92
|
/** 延迟控制 */
|
|
92
93
|
let timeoutId = null;
|
|
94
|
+
const delay = options && options.delay ? options.delay : 5;
|
|
93
95
|
const throttled = (...args) => {
|
|
94
96
|
if (inThrottle)
|
|
95
97
|
return;
|
|
96
98
|
try {
|
|
97
|
-
|
|
99
|
+
const _this = options && options.this ? options.this : null;
|
|
100
|
+
callback.apply(_this, args);
|
|
98
101
|
}
|
|
99
102
|
catch (error) {
|
|
100
|
-
console.error('Throttle
|
|
103
|
+
console.error('Throttle 执行回调抛出问题', error);
|
|
101
104
|
}
|
|
102
105
|
inThrottle = true;
|
|
103
|
-
if (!aTypeOfJs.isNull(timeoutId))
|
|
106
|
+
if (!aTypeOfJs.isNull(timeoutId))
|
|
104
107
|
clearTimeout(timeoutId);
|
|
105
|
-
}
|
|
106
108
|
timeoutId = setTimeout(() => {
|
|
107
109
|
inThrottle = false;
|
|
108
110
|
timeoutId = null;
|
|
109
|
-
}, Math.max(
|
|
111
|
+
}, Math.max(delay, 5));
|
|
110
112
|
};
|
|
111
113
|
throttled.cancel = () => {
|
|
112
114
|
if (!aTypeOfJs.isNull(timeoutId)) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var aTypeOfJs = require('a-type-of-js');
|
|
4
|
-
var escapeRegExp = require('./escapeRegExp.
|
|
5
|
-
var parse = require('./parse.
|
|
4
|
+
var escapeRegExp = require('./escapeRegExp.js');
|
|
5
|
+
var parse = require('./parse.js');
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
*
|
|
@@ -35,14 +35,12 @@ var parse = require('./parse.cjs');
|
|
|
35
35
|
*
|
|
36
36
|
*/
|
|
37
37
|
function autoEscapedRegExp(pattern, options) {
|
|
38
|
-
if (!aTypeOfJs.isString(pattern))
|
|
38
|
+
if (!aTypeOfJs.isString(pattern))
|
|
39
39
|
throw new TypeError('pattern must be a 字符串');
|
|
40
|
-
}
|
|
41
40
|
pattern = escapeRegExp.escapeRegExp(pattern);
|
|
42
41
|
/** 简单转化 */
|
|
43
|
-
if (aTypeOfJs.isUndefined(options))
|
|
42
|
+
if (aTypeOfJs.isUndefined(options))
|
|
44
43
|
return new RegExp(pattern);
|
|
45
|
-
}
|
|
46
44
|
options = parse.parse(options);
|
|
47
45
|
return new RegExp(`${options.start ? '^' : ''}${pattern}${options.end ? '$' : ''}`, options.flags);
|
|
48
46
|
}
|
|
@@ -15,19 +15,16 @@ function parse(options) {
|
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
17
|
// 处理 flags
|
|
18
|
-
if (!aTypeOfJs.isString(options.flags))
|
|
18
|
+
if (!aTypeOfJs.isString(options.flags))
|
|
19
19
|
options.flags = '';
|
|
20
|
-
}
|
|
21
20
|
else {
|
|
22
21
|
// 需求是保留字符串中的某一部分,使用
|
|
23
22
|
const regexp = /[migsuy]/g;
|
|
24
23
|
const matchResult = options.flags.match(regexp);
|
|
25
|
-
if (aTypeOfJs.isNull(matchResult))
|
|
24
|
+
if (aTypeOfJs.isNull(matchResult))
|
|
26
25
|
options.flags = '';
|
|
27
|
-
|
|
28
|
-
else {
|
|
26
|
+
else
|
|
29
27
|
options.flags = [...new Set(matchResult)].join('');
|
|
30
|
-
}
|
|
31
28
|
}
|
|
32
29
|
return options;
|
|
33
30
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var isNumber = require('a-type-of-js/isNumber');
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
*
|
|
@@ -30,7 +30,7 @@ var aTypeOfJs = require('a-type-of-js');
|
|
|
30
30
|
async function sleep(delay = 1000) {
|
|
31
31
|
if (!isFinite(delay) || delay < 0)
|
|
32
32
|
throw new TypeError('delay 应该是一个正常的数值');
|
|
33
|
-
if (
|
|
33
|
+
if (isNumber.isZero(delay))
|
|
34
34
|
return Promise.resolve();
|
|
35
35
|
return new Promise(resolve => setTimeout(resolve, delay));
|
|
36
36
|
}
|
|
@@ -32,10 +32,12 @@ import { isArray, isEmptyArray } from 'a-type-of-js';
|
|
|
32
32
|
*
|
|
33
33
|
*/
|
|
34
34
|
function difference(a, b) {
|
|
35
|
-
if (
|
|
35
|
+
if (!isArray(a) || !isArray(b)) {
|
|
36
36
|
throw new TypeError('参数需为数组');
|
|
37
|
-
|
|
37
|
+
}
|
|
38
|
+
if (isEmptyArray(a) || isEmptyArray(b)) {
|
|
38
39
|
return a;
|
|
40
|
+
}
|
|
39
41
|
/** 获取两个数组中长度较小的 */
|
|
40
42
|
// 参数有顺序要求
|
|
41
43
|
// const [shorter, longer] = a.length > b.length ? [b, a] : [a, b];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { intersection } from './intersection.
|
|
2
|
-
import { union } from './union.
|
|
3
|
-
import { difference } from './difference.
|
|
4
|
-
import { symmetricDifference } from './symmetricDifference.
|
|
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
5
|
|
|
6
6
|
/**
|
|
7
7
|
*
|
|
@@ -22,11 +22,13 @@ import { isArray, isEmptyArray } from 'a-type-of-js';
|
|
|
22
22
|
*
|
|
23
23
|
*/
|
|
24
24
|
function intersection(a, b) {
|
|
25
|
-
if (!isArray(a) || !isArray(b))
|
|
25
|
+
if (!isArray(a) || !isArray(b)) {
|
|
26
26
|
throw new TypeError('参数必须是数组类型数据');
|
|
27
|
+
}
|
|
27
28
|
// 任意数组为空,则返回空数组
|
|
28
|
-
if (
|
|
29
|
+
if (isEmptyArray(a) || isEmptyArray(b)) {
|
|
29
30
|
return [];
|
|
31
|
+
}
|
|
30
32
|
/**
|
|
31
33
|
* 在实际运算中, new Set() 的 开销为 O(n) ,filter 的开销也为 O(n)
|
|
32
34
|
*
|
|
@@ -32,9 +32,9 @@ initial = false) {
|
|
|
32
32
|
/** 转化首字符 */
|
|
33
33
|
const toTransform = (_str, _dividingType) => _str.replace(new RegExp(`${template.test(_dividingType) ? `\\${_dividingType}` : _dividingType}([a-zA-Z])`, 'g'), (match, p1) => p1.toUpperCase());
|
|
34
34
|
// 多分隔符转化
|
|
35
|
-
dividingType
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
dividingType
|
|
36
|
+
.split('')
|
|
37
|
+
.forEach((item) => (result = toTransform(result, item)));
|
|
38
38
|
return initial
|
|
39
39
|
? result.replace(/^./, (match) => match.toUpperCase())
|
|
40
40
|
: result;
|
|
@@ -11,7 +11,7 @@ import { isNaN, isNumber } from 'a-type-of-js';
|
|
|
11
11
|
*
|
|
12
12
|
* 获取一个随机的整数类型
|
|
13
13
|
*
|
|
14
|
-
*
|
|
14
|
+
* 您可以传入两个参数并获取它们之间的任意数字,返回值<span style="color:#ff0;">*会包含端值*</span>
|
|
15
15
|
*
|
|
16
16
|
* 如果只传递一个参数,则如果提供的值为负数,则得到一个小于(或大于)该数字的整数
|
|
17
17
|
*
|
|
@@ -33,12 +33,12 @@ function getRandomInt(max = 1, min = 0) {
|
|
|
33
33
|
let _min = Math.ceil(Number(min)),
|
|
34
34
|
/** 获取最大值 */
|
|
35
35
|
_max = Math.floor(Number(max));
|
|
36
|
-
|
|
37
|
-
if (_min > _max)
|
|
38
|
-
[_max, _min] = [_min, _max];
|
|
39
|
-
//** 两值相等时,直接返回最大值 */
|
|
36
|
+
// 两值相等时,直接返回最大值
|
|
40
37
|
if (_max === _min)
|
|
41
38
|
return _max;
|
|
39
|
+
// 两值交换
|
|
40
|
+
if (_min > _max)
|
|
41
|
+
[_max, _min] = [_min, _max];
|
|
42
42
|
return Math.round(Math.random() * (_max - _min) + _min);
|
|
43
43
|
}
|
|
44
44
|
/**
|