a-js-tools 0.0.0 → 0.0.1
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 +13 -13
- package/README.md +30 -25
- package/cjs/index.cjs +11 -9
- package/cjs/src/className.cjs +58 -0
- package/cjs/src/getRandomNumber.cjs +55 -0
- package/cjs/src/performance.cjs +97 -0
- package/mjs/index.mjs +5 -2
- package/mjs/src/className.mjs +55 -0
- package/mjs/src/getRandomNumber.mjs +52 -0
- package/mjs/src/performance.mjs +94 -0
- package/package.json +81 -68
- package/types/index.d.ts +3 -2
- package/types/src/className.d.ts +33 -0
- package/types/src/getRandomNumber.d.ts +28 -0
- package/types/src/getRandomString.d.ts +48 -0
- package/types/src/index.d.ts +3 -0
- package/types/src/performance.d.ts +40 -0
- package/cjs/src/data.cjs +0 -9
- package/cjs/src/pureFunction.cjs +0 -120
- package/mjs/src/data.mjs +0 -6
- package/mjs/src/pureFunction.mjs +0 -114
- package/types/src/data.d.ts +0 -4
- package/types/src/pureFunction.d.ts +0 -95
package/LICENSE
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
Copyright (c) <2024> <
|
|
2
|
-
|
|
3
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
4
|
-
purpose with or without fee is hereby granted, provided that the above
|
|
5
|
-
copyright notice and this permission notice appear in all copies.
|
|
6
|
-
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
8
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
9
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
10
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
11
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
12
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
13
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
1
|
+
Copyright (c) <2024> <earthnut.dev>
|
|
2
|
+
|
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
4
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
5
|
+
copyright notice and this permission notice appear in all copies.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
8
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
9
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
10
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
11
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
12
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
13
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,25 +1,30 @@
|
|
|
1
|
-
# a-js-tools
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
[
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
- `
|
|
20
|
-
- `
|
|
21
|
-
- `
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
1
|
+
# a-js-tools
|
|
2
|
+
|
|
3
|
+
&label=version&labelColor=rgb(73,73,228)&color=rgb(0,0,0)>) &color=rgb(0,0,0)>) &color=rgb(0,0,0)>) &labelColor=rgb(194,112,210)&color=rgb(0,0,0)>) &color=rgb(0,0,0)>)
|
|
4
|
+
|
|
5
|
+
&labelColor=rgb(255,165,0)&color=rgb(0,0,0)>) &color=rgb(0,0,0)>)
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
[>)](https://github.com/earthnutDev/a-js-tools/blob/main/README-zh.md) ?style=social>) [?logo=github>)](https://github.com/earthnutDev/a-js-tools/blob/main/CHANGELOG-en.md) [?logo=github>)](https://github.com/earthnutDev/a-js-tools/issues)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
A purely functional tool that includes
|
|
14
|
+
|
|
15
|
+
## pure function
|
|
16
|
+
|
|
17
|
+
- `typeOf` Verify data type
|
|
18
|
+
- `lmDebounce` Anti shake function
|
|
19
|
+
- `lmThrottle` Throttling function
|
|
20
|
+
- `getRandomInt` get random int number
|
|
21
|
+
- `getRandomFloat` get random float number
|
|
22
|
+
|
|
23
|
+
## class name
|
|
24
|
+
|
|
25
|
+
- `toLowerCamelCase` Convert hyphenation to hump
|
|
26
|
+
- `toSplitCase` Convert small hump to hyphen split
|
|
27
|
+
|
|
28
|
+
## Documentation
|
|
29
|
+
|
|
30
|
+
see [https://earthnut.dev/a-js-tools/](https://earthnut.dev/a-js-tools/)
|
package/cjs/index.cjs
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
3
|
+
var getRandomNumber = require('./src/getRandomNumber.cjs');
|
|
4
|
+
var className = require('./src/className.cjs');
|
|
5
|
+
require('a-type-of-js');
|
|
6
|
+
require('crypto');
|
|
7
|
+
var performance = require('./src/performance.cjs');
|
|
5
8
|
|
|
6
9
|
|
|
7
10
|
|
|
8
|
-
exports.
|
|
9
|
-
exports.
|
|
10
|
-
exports.
|
|
11
|
-
exports.
|
|
12
|
-
exports.
|
|
13
|
-
exports.
|
|
14
|
-
exports.t = data.t;
|
|
11
|
+
exports.getRandomFloat = getRandomNumber.getRandomFloat;
|
|
12
|
+
exports.getRandomInt = getRandomNumber.getRandomInt;
|
|
13
|
+
exports.toLowerCamelCase = className.toLowerCamelCase;
|
|
14
|
+
exports.toSplitCase = className.toSplitCase;
|
|
15
|
+
exports.debounce = performance.debounce;
|
|
16
|
+
exports.throttle = performance.throttle;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/****************************************************************************
|
|
4
|
+
* @Author earthnut
|
|
5
|
+
* @Email earthnut.dev@outlook.com
|
|
6
|
+
* @ProjectName a-js-tools
|
|
7
|
+
* @FileName className.ts
|
|
8
|
+
* @CreateDate 周六 09/14/2024
|
|
9
|
+
* @Description css 类名转换
|
|
10
|
+
****************************************************************************/
|
|
11
|
+
/**************************************
|
|
12
|
+
*
|
|
13
|
+
* 转化为驼峰命名法
|
|
14
|
+
*
|
|
15
|
+
* @param str 待转化的字符串
|
|
16
|
+
* @param [dividingType='-'] 分隔符。缺省为 "-"
|
|
17
|
+
* @param [initial=false] 是否转化首字符。缺省为false(小驼峰式)
|
|
18
|
+
* @returns {string} 转化后的字符串
|
|
19
|
+
*
|
|
20
|
+
**************************************/
|
|
21
|
+
function toLowerCamelCase(
|
|
22
|
+
/** 待转化的字符串 */
|
|
23
|
+
str,
|
|
24
|
+
/** 分隔符。缺省为 "-" */
|
|
25
|
+
dividingType = '-',
|
|
26
|
+
/** 是否转化首字符。缺省为false(小驼峰式) */
|
|
27
|
+
initial = false) {
|
|
28
|
+
let result = str;
|
|
29
|
+
/**************************
|
|
30
|
+
* 匹配规则
|
|
31
|
+
*
|
|
32
|
+
* - 匹配到分隔符,将分隔符后面的字符转化为大写
|
|
33
|
+
**************************/
|
|
34
|
+
const template = /[\\]|[\^]|[?]|[-]|[.]|[(]|[)]|[|]|[[]\[\]]|[{]|[}]|[+]|[*]|[$]/;
|
|
35
|
+
/** 转化首字符 */
|
|
36
|
+
const toTransform = (_str, _dividingType) => _str.replace(new RegExp(`${template.test(_dividingType) ? `\\${_dividingType}` : _dividingType}([a-zA-Z])`, 'g'), (match, p1) => p1.toUpperCase());
|
|
37
|
+
// 多分隔符转化
|
|
38
|
+
dividingType.split('').forEach((item) => {
|
|
39
|
+
result = toTransform(result, item);
|
|
40
|
+
});
|
|
41
|
+
return initial
|
|
42
|
+
? result.replace(/^./, (match) => match.toUpperCase())
|
|
43
|
+
: result;
|
|
44
|
+
}
|
|
45
|
+
/**************************************
|
|
46
|
+
*
|
|
47
|
+
* @param str 转化的字符串
|
|
48
|
+
* @param [dividingType='-'] 转化后单词间的连字符
|
|
49
|
+
*
|
|
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,55 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var aTypeOfJs = require('a-type-of-js');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* 获取一个随机整数
|
|
8
|
+
*
|
|
9
|
+
* 可传入两个参数,获取两参数之间的任意数
|
|
10
|
+
*
|
|
11
|
+
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的整数
|
|
12
|
+
*
|
|
13
|
+
* @export
|
|
14
|
+
* @param {number} max 最大值,不可为 `NaN`
|
|
15
|
+
* @param {number} [min] 最小值,不可取值 `NaN`
|
|
16
|
+
* @return {*} {number}
|
|
17
|
+
*/
|
|
18
|
+
function getRandomInt(max = 1, min = 0) {
|
|
19
|
+
// 判断是否为 NaN 或 不是数字
|
|
20
|
+
if (aTypeOfJs.isNaN(max) || aTypeOfJs.isNaN(min) || !aTypeOfJs.isNumber(max) || !aTypeOfJs.isNumber(min)) {
|
|
21
|
+
throw new TypeError('getRandomInt: max or min is NaN or is not a number');
|
|
22
|
+
}
|
|
23
|
+
/** 获取最小值 */
|
|
24
|
+
let _min = Math.ceil(Number(min)),
|
|
25
|
+
/** 获取最大值 */
|
|
26
|
+
_max = Math.floor(Number(max));
|
|
27
|
+
/** 两值交换 */
|
|
28
|
+
_min > _max && ([_max, _min] = [_min, _max]);
|
|
29
|
+
//** 两值相等时,直接返回最大值 */
|
|
30
|
+
if (_max === _min)
|
|
31
|
+
return _max;
|
|
32
|
+
return Math.round(Math.random() * (_max - _min) + _min);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* 获取一个随机浮点数数
|
|
37
|
+
*
|
|
38
|
+
* 可传入两个参数,获取两参数之间的任意数
|
|
39
|
+
*
|
|
40
|
+
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的浮点数数
|
|
41
|
+
*
|
|
42
|
+
* @export
|
|
43
|
+
* @param {number} max 最大值,缺省 1
|
|
44
|
+
* @param {number} [min] 最小值,缺省 0
|
|
45
|
+
* @return {*} {number}
|
|
46
|
+
*/
|
|
47
|
+
function getRandomFloat(max = 1, min = 0) {
|
|
48
|
+
if (max == min)
|
|
49
|
+
max++;
|
|
50
|
+
min > max && ([max, min] = [min, max]);
|
|
51
|
+
return Math.random() * (max - min) + min;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
exports.getRandomFloat = getRandomFloat;
|
|
55
|
+
exports.getRandomInt = getRandomInt;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**************************************
|
|
4
|
+
*
|
|
5
|
+
* 函数防抖
|
|
6
|
+
*
|
|
7
|
+
* @param {*} callback
|
|
8
|
+
* @param {number} delay 缺省 300 ms
|
|
9
|
+
* @return {*} 返回的是一个函数
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const debounce = (callback: Function, delay = 300) => {
|
|
14
|
+
* let timer: any = null
|
|
15
|
+
* return (...args: any[]) => {
|
|
16
|
+
* clearTimeout(timer)
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
*
|
|
20
|
+
**************************************/
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
+
function debounce(callback, delay = 200) {
|
|
23
|
+
if (typeof callback !== 'function') {
|
|
24
|
+
throw new TypeError('callback must be a function');
|
|
25
|
+
}
|
|
26
|
+
if (!isFinite(delay) || delay < 0)
|
|
27
|
+
// 强制转换非数值
|
|
28
|
+
delay = 200;
|
|
29
|
+
/** 定时器返回的 id */
|
|
30
|
+
let timeoutId;
|
|
31
|
+
const clear = () => {
|
|
32
|
+
if (timeoutId) {
|
|
33
|
+
clearTimeout(timeoutId);
|
|
34
|
+
timeoutId = undefined;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const result = (...args) => {
|
|
38
|
+
clear();
|
|
39
|
+
timeoutId = setTimeout(() => {
|
|
40
|
+
try {
|
|
41
|
+
Reflect.apply(callback, null, args);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.log('Debounce callback throw an error', error);
|
|
45
|
+
}
|
|
46
|
+
}, Math.max(delay, 5));
|
|
47
|
+
};
|
|
48
|
+
result.cancel = () => {
|
|
49
|
+
clear();
|
|
50
|
+
};
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 节流函数
|
|
55
|
+
* @param callback
|
|
56
|
+
* @param delay 延迟时间,单位为毫秒(ms),缺省 200(ms)
|
|
57
|
+
* @returns 返回的是一个函数
|
|
58
|
+
*/
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
+
function throttle(callback, delay = 200) {
|
|
61
|
+
// 强制转换非数值
|
|
62
|
+
if (!isFinite(delay) || (isFinite(delay) && delay < 0))
|
|
63
|
+
delay = 200;
|
|
64
|
+
/** 延迟控制插销 */
|
|
65
|
+
let inThrottle = false;
|
|
66
|
+
/** 延迟控制 */
|
|
67
|
+
let timeoutId = null;
|
|
68
|
+
const throttled = (...args) => {
|
|
69
|
+
if (inThrottle)
|
|
70
|
+
return;
|
|
71
|
+
try {
|
|
72
|
+
Reflect.apply(callback, null, args);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
console.error('Throttle callback throw an error', error);
|
|
76
|
+
}
|
|
77
|
+
inThrottle = true;
|
|
78
|
+
if (timeoutId !== null) {
|
|
79
|
+
clearTimeout(timeoutId);
|
|
80
|
+
}
|
|
81
|
+
timeoutId = setTimeout(() => {
|
|
82
|
+
inThrottle = false;
|
|
83
|
+
timeoutId = null;
|
|
84
|
+
}, delay);
|
|
85
|
+
};
|
|
86
|
+
throttled.cancel = () => {
|
|
87
|
+
if (timeoutId !== null) {
|
|
88
|
+
clearTimeout(timeoutId);
|
|
89
|
+
}
|
|
90
|
+
inThrottle = false;
|
|
91
|
+
timeoutId = null;
|
|
92
|
+
};
|
|
93
|
+
return throttled;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
exports.debounce = debounce;
|
|
97
|
+
exports.throttle = throttle;
|
package/mjs/index.mjs
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { getRandomFloat, getRandomInt } from './src/getRandomNumber.mjs';
|
|
2
|
+
export { toLowerCamelCase, toSplitCase } from './src/className.mjs';
|
|
3
|
+
import 'a-type-of-js';
|
|
4
|
+
import 'crypto';
|
|
5
|
+
export { debounce, throttle } from './src/performance.mjs';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/****************************************************************************
|
|
2
|
+
* @Author earthnut
|
|
3
|
+
* @Email earthnut.dev@outlook.com
|
|
4
|
+
* @ProjectName a-js-tools
|
|
5
|
+
* @FileName className.ts
|
|
6
|
+
* @CreateDate 周六 09/14/2024
|
|
7
|
+
* @Description css 类名转换
|
|
8
|
+
****************************************************************************/
|
|
9
|
+
/**************************************
|
|
10
|
+
*
|
|
11
|
+
* 转化为驼峰命名法
|
|
12
|
+
*
|
|
13
|
+
* @param str 待转化的字符串
|
|
14
|
+
* @param [dividingType='-'] 分隔符。缺省为 "-"
|
|
15
|
+
* @param [initial=false] 是否转化首字符。缺省为false(小驼峰式)
|
|
16
|
+
* @returns {string} 转化后的字符串
|
|
17
|
+
*
|
|
18
|
+
**************************************/
|
|
19
|
+
function toLowerCamelCase(
|
|
20
|
+
/** 待转化的字符串 */
|
|
21
|
+
str,
|
|
22
|
+
/** 分隔符。缺省为 "-" */
|
|
23
|
+
dividingType = '-',
|
|
24
|
+
/** 是否转化首字符。缺省为false(小驼峰式) */
|
|
25
|
+
initial = false) {
|
|
26
|
+
let result = str;
|
|
27
|
+
/**************************
|
|
28
|
+
* 匹配规则
|
|
29
|
+
*
|
|
30
|
+
* - 匹配到分隔符,将分隔符后面的字符转化为大写
|
|
31
|
+
**************************/
|
|
32
|
+
const template = /[\\]|[\^]|[?]|[-]|[.]|[(]|[)]|[|]|[[]\[\]]|[{]|[}]|[+]|[*]|[$]/;
|
|
33
|
+
/** 转化首字符 */
|
|
34
|
+
const toTransform = (_str, _dividingType) => _str.replace(new RegExp(`${template.test(_dividingType) ? `\\${_dividingType}` : _dividingType}([a-zA-Z])`, 'g'), (match, p1) => p1.toUpperCase());
|
|
35
|
+
// 多分隔符转化
|
|
36
|
+
dividingType.split('').forEach((item) => {
|
|
37
|
+
result = toTransform(result, item);
|
|
38
|
+
});
|
|
39
|
+
return initial
|
|
40
|
+
? result.replace(/^./, (match) => match.toUpperCase())
|
|
41
|
+
: result;
|
|
42
|
+
}
|
|
43
|
+
/**************************************
|
|
44
|
+
*
|
|
45
|
+
* @param str 转化的字符串
|
|
46
|
+
* @param [dividingType='-'] 转化后单词间的连字符
|
|
47
|
+
*
|
|
48
|
+
*
|
|
49
|
+
**************************************/
|
|
50
|
+
function toSplitCase(str, dividingType = '-') {
|
|
51
|
+
const result = str.replace(/[A-Z]/g, (match) => dividingType.concat(match.toLowerCase()));
|
|
52
|
+
return result.startsWith(dividingType) ? result.substring(1) : result;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export { toLowerCamelCase, toSplitCase };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { isNaN, isNumber } from 'a-type-of-js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* 获取一个随机整数
|
|
6
|
+
*
|
|
7
|
+
* 可传入两个参数,获取两参数之间的任意数
|
|
8
|
+
*
|
|
9
|
+
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的整数
|
|
10
|
+
*
|
|
11
|
+
* @export
|
|
12
|
+
* @param {number} max 最大值,不可为 `NaN`
|
|
13
|
+
* @param {number} [min] 最小值,不可取值 `NaN`
|
|
14
|
+
* @return {*} {number}
|
|
15
|
+
*/
|
|
16
|
+
function getRandomInt(max = 1, min = 0) {
|
|
17
|
+
// 判断是否为 NaN 或 不是数字
|
|
18
|
+
if (isNaN(max) || isNaN(min) || !isNumber(max) || !isNumber(min)) {
|
|
19
|
+
throw new TypeError('getRandomInt: max or min is NaN or is not a number');
|
|
20
|
+
}
|
|
21
|
+
/** 获取最小值 */
|
|
22
|
+
let _min = Math.ceil(Number(min)),
|
|
23
|
+
/** 获取最大值 */
|
|
24
|
+
_max = Math.floor(Number(max));
|
|
25
|
+
/** 两值交换 */
|
|
26
|
+
_min > _max && ([_max, _min] = [_min, _max]);
|
|
27
|
+
//** 两值相等时,直接返回最大值 */
|
|
28
|
+
if (_max === _min)
|
|
29
|
+
return _max;
|
|
30
|
+
return Math.round(Math.random() * (_max - _min) + _min);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* 获取一个随机浮点数数
|
|
35
|
+
*
|
|
36
|
+
* 可传入两个参数,获取两参数之间的任意数
|
|
37
|
+
*
|
|
38
|
+
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的浮点数数
|
|
39
|
+
*
|
|
40
|
+
* @export
|
|
41
|
+
* @param {number} max 最大值,缺省 1
|
|
42
|
+
* @param {number} [min] 最小值,缺省 0
|
|
43
|
+
* @return {*} {number}
|
|
44
|
+
*/
|
|
45
|
+
function getRandomFloat(max = 1, min = 0) {
|
|
46
|
+
if (max == min)
|
|
47
|
+
max++;
|
|
48
|
+
min > max && ([max, min] = [min, max]);
|
|
49
|
+
return Math.random() * (max - min) + min;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export { getRandomFloat, getRandomInt };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**************************************
|
|
2
|
+
*
|
|
3
|
+
* 函数防抖
|
|
4
|
+
*
|
|
5
|
+
* @param {*} callback
|
|
6
|
+
* @param {number} delay 缺省 300 ms
|
|
7
|
+
* @return {*} 返回的是一个函数
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* const debounce = (callback: Function, delay = 300) => {
|
|
12
|
+
* let timer: any = null
|
|
13
|
+
* return (...args: any[]) => {
|
|
14
|
+
* clearTimeout(timer)
|
|
15
|
+
* }
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
**************************************/
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
function debounce(callback, delay = 200) {
|
|
21
|
+
if (typeof callback !== 'function') {
|
|
22
|
+
throw new TypeError('callback must be a function');
|
|
23
|
+
}
|
|
24
|
+
if (!isFinite(delay) || delay < 0)
|
|
25
|
+
// 强制转换非数值
|
|
26
|
+
delay = 200;
|
|
27
|
+
/** 定时器返回的 id */
|
|
28
|
+
let timeoutId;
|
|
29
|
+
const clear = () => {
|
|
30
|
+
if (timeoutId) {
|
|
31
|
+
clearTimeout(timeoutId);
|
|
32
|
+
timeoutId = undefined;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const result = (...args) => {
|
|
36
|
+
clear();
|
|
37
|
+
timeoutId = setTimeout(() => {
|
|
38
|
+
try {
|
|
39
|
+
Reflect.apply(callback, null, args);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.log('Debounce callback throw an error', error);
|
|
43
|
+
}
|
|
44
|
+
}, Math.max(delay, 5));
|
|
45
|
+
};
|
|
46
|
+
result.cancel = () => {
|
|
47
|
+
clear();
|
|
48
|
+
};
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* 节流函数
|
|
53
|
+
* @param callback
|
|
54
|
+
* @param delay 延迟时间,单位为毫秒(ms),缺省 200(ms)
|
|
55
|
+
* @returns 返回的是一个函数
|
|
56
|
+
*/
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
|
+
function throttle(callback, delay = 200) {
|
|
59
|
+
// 强制转换非数值
|
|
60
|
+
if (!isFinite(delay) || (isFinite(delay) && delay < 0))
|
|
61
|
+
delay = 200;
|
|
62
|
+
/** 延迟控制插销 */
|
|
63
|
+
let inThrottle = false;
|
|
64
|
+
/** 延迟控制 */
|
|
65
|
+
let timeoutId = null;
|
|
66
|
+
const throttled = (...args) => {
|
|
67
|
+
if (inThrottle)
|
|
68
|
+
return;
|
|
69
|
+
try {
|
|
70
|
+
Reflect.apply(callback, null, args);
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
console.error('Throttle callback throw an error', error);
|
|
74
|
+
}
|
|
75
|
+
inThrottle = true;
|
|
76
|
+
if (timeoutId !== null) {
|
|
77
|
+
clearTimeout(timeoutId);
|
|
78
|
+
}
|
|
79
|
+
timeoutId = setTimeout(() => {
|
|
80
|
+
inThrottle = false;
|
|
81
|
+
timeoutId = null;
|
|
82
|
+
}, delay);
|
|
83
|
+
};
|
|
84
|
+
throttled.cancel = () => {
|
|
85
|
+
if (timeoutId !== null) {
|
|
86
|
+
clearTimeout(timeoutId);
|
|
87
|
+
}
|
|
88
|
+
inThrottle = false;
|
|
89
|
+
timeoutId = null;
|
|
90
|
+
};
|
|
91
|
+
return throttled;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export { debounce, throttle };
|
package/package.json
CHANGED
|
@@ -1,68 +1,81 @@
|
|
|
1
|
-
{
|
|
2
|
-
"type": "module",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"name": "a-js-tools",
|
|
5
|
-
"main": "cjs/index.cjs",
|
|
6
|
-
"module": "mjs/index.mjs",
|
|
7
|
-
"typings": "types/index.d.ts",
|
|
8
|
-
"description": "
|
|
9
|
-
"files": [
|
|
10
|
-
"cjs/",
|
|
11
|
-
"mjs/",
|
|
12
|
-
"types/"
|
|
13
|
-
],
|
|
14
|
-
"exports": {
|
|
15
|
-
"import": "./mjs/index.mjs",
|
|
16
|
-
"require": "./cjs/index.cjs",
|
|
17
|
-
"types": "./types/index.d.ts"
|
|
18
|
-
},
|
|
19
|
-
"scripts": {
|
|
20
|
-
"b": "rollup --config rollup.config.js && tsc -p tsconfig.types.json",
|
|
21
|
-
"build": "npx ixxx rm dist run b",
|
|
22
|
-
"clean": "npx ixxx up -d r node_modules package-lock.json run npm install run build",
|
|
23
|
-
"prettier": "npx prettier . --write",
|
|
24
|
-
"test": "npx ixxx
|
|
25
|
-
"versionPatch": "npm version patch --force --no-git-tag-version --allow-same-version=true",
|
|
26
|
-
"
|
|
27
|
-
},
|
|
28
|
-
"repository": {
|
|
29
|
-
"type": "git",
|
|
30
|
-
"url": "git+https://github.com/
|
|
31
|
-
},
|
|
32
|
-
"keywords": [
|
|
33
|
-
"js-tools"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"
|
|
49
|
-
},
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
"@
|
|
56
|
-
"@
|
|
57
|
-
"@
|
|
58
|
-
"eslint": "^
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"rollup
|
|
63
|
-
"rollup
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
|
|
68
|
-
|
|
1
|
+
{
|
|
2
|
+
"type": "module",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"name": "a-js-tools",
|
|
5
|
+
"main": "cjs/index.cjs",
|
|
6
|
+
"module": "mjs/index.mjs",
|
|
7
|
+
"typings": "types/index.d.ts",
|
|
8
|
+
"description": "Some functions that are available in js",
|
|
9
|
+
"files": [
|
|
10
|
+
"cjs/",
|
|
11
|
+
"mjs/",
|
|
12
|
+
"types/"
|
|
13
|
+
],
|
|
14
|
+
"exports": {
|
|
15
|
+
"import": "./mjs/index.mjs",
|
|
16
|
+
"require": "./cjs/index.cjs",
|
|
17
|
+
"types": "./types/index.d.ts"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"b": "rollup --config rollup.config.js && tsc -p tsconfig.types.json",
|
|
21
|
+
"build": "npx ixxx rm dist run b",
|
|
22
|
+
"clean": "npx ixxx up -d r node_modules package-lock.json run npm install run build",
|
|
23
|
+
"prettier": "npx prettier . --write",
|
|
24
|
+
"test": "npx ixxx clear && npx jest",
|
|
25
|
+
"versionPatch": "npm version patch --force --no-git-tag-version --allow-same-version=true",
|
|
26
|
+
"pub": "npm run build && cd dist && npm publish && cd ../"
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/earthnutDev/a-js-tools.git"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"a-js-tools"
|
|
34
|
+
],
|
|
35
|
+
"author": {
|
|
36
|
+
"name": "earthnut",
|
|
37
|
+
"email": "earthnut.dev@outlook.com",
|
|
38
|
+
"url": "https://earthnut.dev/about"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://earthnut.dev/a-js-tools",
|
|
41
|
+
"license": "ISC",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/earthnutDev/a-js-tools/issues",
|
|
44
|
+
"email": "earthnut.dev@outlook.com"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"registry": "https://registry.npmjs.org/"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"a-type-of-js": "^0.0.2",
|
|
52
|
+
"tslib": "^2.6.3"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@babel/core": "^7.26.9",
|
|
56
|
+
"@babel/preset-env": "^7.26.9",
|
|
57
|
+
"@babel/preset-typescript": "^7.26.0",
|
|
58
|
+
"@eslint/js": "^9.6.0",
|
|
59
|
+
"@rollup/plugin-commonjs": "^25.0.8",
|
|
60
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
61
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
62
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
63
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
64
|
+
"@types/expect": "^1.20.4",
|
|
65
|
+
"@types/jest": "^29.5.14",
|
|
66
|
+
"@types/node": "^20.14.9",
|
|
67
|
+
"babel-jest": "^29.7.0",
|
|
68
|
+
"eslint": "^8.57.0",
|
|
69
|
+
"eslint-config-prettier": "^9.1.0",
|
|
70
|
+
"expect": "^29.7.0",
|
|
71
|
+
"globals": "^15.7.0",
|
|
72
|
+
"jest": "^29.7.0",
|
|
73
|
+
"prettier": "^3.3.2",
|
|
74
|
+
"rollup-plugin-cleanup": "^3.2.1",
|
|
75
|
+
"rollup-plugin-copy": "^3.5.0",
|
|
76
|
+
"ts-jest": "^29.2.6",
|
|
77
|
+
"ts-node": "^10.9.2",
|
|
78
|
+
"typescript": "^5.5.3",
|
|
79
|
+
"typescript-eslint": "^7.15.0"
|
|
80
|
+
}
|
|
81
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
1
|
+
export { toLowerCamelCase, toSplitCase, getRandomFloat, getRandomInt, } from './src/index';
|
|
2
|
+
export { throttle, debounce } from './src/performance';
|
|
3
|
+
export type { DebounceAndThrottleReturnType } from './src/performance';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/****************************************************************************
|
|
2
|
+
* @Author earthnut
|
|
3
|
+
* @Email earthnut.dev@outlook.com
|
|
4
|
+
* @ProjectName a-js-tools
|
|
5
|
+
* @FileName className.ts
|
|
6
|
+
* @CreateDate 周六 09/14/2024
|
|
7
|
+
* @Description css 类名转换
|
|
8
|
+
****************************************************************************/
|
|
9
|
+
/**************************************
|
|
10
|
+
*
|
|
11
|
+
* 转化为驼峰命名法
|
|
12
|
+
*
|
|
13
|
+
* @param str 待转化的字符串
|
|
14
|
+
* @param [dividingType='-'] 分隔符。缺省为 "-"
|
|
15
|
+
* @param [initial=false] 是否转化首字符。缺省为false(小驼峰式)
|
|
16
|
+
* @returns {string} 转化后的字符串
|
|
17
|
+
*
|
|
18
|
+
**************************************/
|
|
19
|
+
export declare function toLowerCamelCase(
|
|
20
|
+
/** 待转化的字符串 */
|
|
21
|
+
str: string,
|
|
22
|
+
/** 分隔符。缺省为 "-" */
|
|
23
|
+
dividingType?: string,
|
|
24
|
+
/** 是否转化首字符。缺省为false(小驼峰式) */
|
|
25
|
+
initial?: boolean): string;
|
|
26
|
+
/**************************************
|
|
27
|
+
*
|
|
28
|
+
* @param str 转化的字符串
|
|
29
|
+
* @param [dividingType='-'] 转化后单词间的连字符
|
|
30
|
+
*
|
|
31
|
+
*
|
|
32
|
+
**************************************/
|
|
33
|
+
export declare function toSplitCase(str: string, dividingType?: string): string;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* 获取一个随机整数
|
|
4
|
+
*
|
|
5
|
+
* 可传入两个参数,获取两参数之间的任意数
|
|
6
|
+
*
|
|
7
|
+
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的整数
|
|
8
|
+
*
|
|
9
|
+
* @export
|
|
10
|
+
* @param {number} max 最大值,不可为 `NaN`
|
|
11
|
+
* @param {number} [min] 最小值,不可取值 `NaN`
|
|
12
|
+
* @return {*} {number}
|
|
13
|
+
*/
|
|
14
|
+
export declare function getRandomInt(max?: number, min?: number): number;
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* 获取一个随机浮点数数
|
|
18
|
+
*
|
|
19
|
+
* 可传入两个参数,获取两参数之间的任意数
|
|
20
|
+
*
|
|
21
|
+
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的浮点数数
|
|
22
|
+
*
|
|
23
|
+
* @export
|
|
24
|
+
* @param {number} max 最大值,缺省 1
|
|
25
|
+
* @param {number} [min] 最小值,缺省 0
|
|
26
|
+
* @return {*} {number}
|
|
27
|
+
*/
|
|
28
|
+
export declare function getRandomFloat(max?: number, min?: number): number;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**************************************
|
|
2
|
+
*
|
|
3
|
+
* 随机字符串生成函数
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
*
|
|
7
|
+
**************************************/
|
|
8
|
+
export type RandomStringOptions = {
|
|
9
|
+
/**************************
|
|
10
|
+
* 字符串长度
|
|
11
|
+
* @default 32
|
|
12
|
+
**************************/
|
|
13
|
+
length?: number;
|
|
14
|
+
/**************************
|
|
15
|
+
* 字符串可选字符
|
|
16
|
+
* @default '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
17
|
+
**************************/
|
|
18
|
+
chars?: string;
|
|
19
|
+
/**************************
|
|
20
|
+
* 是否包含数字
|
|
21
|
+
* @default false
|
|
22
|
+
**************************/
|
|
23
|
+
includeNumbers?: boolean;
|
|
24
|
+
/**************************
|
|
25
|
+
* 是否包含大写字母
|
|
26
|
+
* @default false
|
|
27
|
+
**************************/
|
|
28
|
+
includeUppercaseLetters?: boolean;
|
|
29
|
+
/**************************
|
|
30
|
+
* 是否包含特殊字符
|
|
31
|
+
* @default false
|
|
32
|
+
**************************/
|
|
33
|
+
includeSpecial?: boolean;
|
|
34
|
+
/**************************
|
|
35
|
+
* 字符串类型
|
|
36
|
+
* @default 'string''
|
|
37
|
+
**************************/
|
|
38
|
+
type?: 'string' | 'uuid';
|
|
39
|
+
};
|
|
40
|
+
/**************************************
|
|
41
|
+
*
|
|
42
|
+
* 随机字符串生成函数
|
|
43
|
+
* @param {number} length - 字符串长度
|
|
44
|
+
* @returns {string} - 生成的随机字符串
|
|
45
|
+
*
|
|
46
|
+
*
|
|
47
|
+
**************************************/
|
|
48
|
+
export declare function getRandomString(length?: RandomStringOptions | number): string;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
type Callback = (...args: unknown[]) => void;
|
|
2
|
+
/**************************
|
|
3
|
+
* DebounceAndThrottleReturnType
|
|
4
|
+
*
|
|
5
|
+
*
|
|
6
|
+
*
|
|
7
|
+
* @description: 函数防抖和函数节流的返回类型
|
|
8
|
+
*
|
|
9
|
+
**************************/
|
|
10
|
+
export interface DebounceAndThrottleReturnType<F extends Callback> {
|
|
11
|
+
(...args: Parameters<F>): void;
|
|
12
|
+
cancel(): void;
|
|
13
|
+
}
|
|
14
|
+
/**************************************
|
|
15
|
+
*
|
|
16
|
+
* 函数防抖
|
|
17
|
+
*
|
|
18
|
+
* @param {*} callback
|
|
19
|
+
* @param {number} delay 缺省 300 ms
|
|
20
|
+
* @return {*} 返回的是一个函数
|
|
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
|
+
export declare function debounce<F extends (...args: any[]) => void>(callback: F, delay?: number): DebounceAndThrottleReturnType<F>;
|
|
33
|
+
/**
|
|
34
|
+
* 节流函数
|
|
35
|
+
* @param callback
|
|
36
|
+
* @param delay 延迟时间,单位为毫秒(ms),缺省 200(ms)
|
|
37
|
+
* @returns 返回的是一个函数
|
|
38
|
+
*/
|
|
39
|
+
export declare function throttle<F extends (...args: any[]) => void>(callback: F, delay?: number): DebounceAndThrottleReturnType<F>;
|
|
40
|
+
export {};
|
package/cjs/src/data.cjs
DELETED
package/cjs/src/pureFunction.cjs
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 获取数据的类型
|
|
5
|
-
*
|
|
6
|
-
* @param {*} o 任意数据
|
|
7
|
-
* @return {*} 返回是一个字符串 {@link String},包含于 @see {@link TypeOf}
|
|
8
|
-
*/
|
|
9
|
-
function typeOf(o) {
|
|
10
|
-
return Reflect.apply(Object.prototype.toString, o, [])
|
|
11
|
-
.replace(/^.*\s(.*)]$/, '$1')
|
|
12
|
-
.toLowerCase();
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* 防抖
|
|
16
|
-
*
|
|
17
|
-
* 这种设计有一种不好的地方就是倘若最后一次尚未执行,不好清理
|
|
18
|
-
* @param {*} callback
|
|
19
|
-
* @param {number} delay 缺省 300 ms
|
|
20
|
-
* @return {*} 返回的是一个函数
|
|
21
|
-
*/
|
|
22
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
-
function debounce(callback, delay = 200) {
|
|
24
|
-
// 强制转换非数值
|
|
25
|
-
if (typeof delay != 'number' || isNaN(delay))
|
|
26
|
-
delay = 200;
|
|
27
|
-
let timeout;
|
|
28
|
-
return ((...args) => {
|
|
29
|
-
clearTimeout(timeout);
|
|
30
|
-
timeout = setTimeout(() => Reflect.apply(callback, null, args), Math.max(delay, 0));
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* 节流函数
|
|
35
|
-
* @param callback
|
|
36
|
-
* @param delay 延迟时间,单位为毫秒(ms),缺省 200(ms)
|
|
37
|
-
* @returns 返回的是一个函数
|
|
38
|
-
*/
|
|
39
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
40
|
-
function throttle(callback, delay = 200) {
|
|
41
|
-
// 强制转换非数值
|
|
42
|
-
if (typeof delay != 'number' || isNaN(delay))
|
|
43
|
-
delay = 200;
|
|
44
|
-
/** 延迟控制插销 */
|
|
45
|
-
let inThrottle = true;
|
|
46
|
-
return ((...args) => {
|
|
47
|
-
if (!inThrottle)
|
|
48
|
-
return;
|
|
49
|
-
Reflect.apply(callback, null, args);
|
|
50
|
-
inThrottle = false;
|
|
51
|
-
setTimeout(() => (inThrottle = true), Math.max(delay, 0));
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
/** 响应的 */
|
|
55
|
-
// function responsive(target: any) {
|
|
56
|
-
// const handle = {
|
|
57
|
-
// get(target: any, key: any, receiver: any) {
|
|
58
|
-
// console.log(`捕获到 key: ${key}`);
|
|
59
|
-
// return Reflect.get(target, key, receiver);
|
|
60
|
-
// },
|
|
61
|
-
// set(target: any, key: any, value: any, receiver: any) {
|
|
62
|
-
// console.log(`设置值 ${value} 到属性 ${key}`);
|
|
63
|
-
// return Reflect.set(target, key, value, receiver);
|
|
64
|
-
// },
|
|
65
|
-
// };
|
|
66
|
-
// return new Proxy(target, handle);
|
|
67
|
-
// }
|
|
68
|
-
/**
|
|
69
|
-
* 监听
|
|
70
|
-
*/
|
|
71
|
-
// function Listener(vm: any, expOrFn: any, cb: any) {
|
|
72
|
-
// // @ts-ignore
|
|
73
|
-
// (this.vm = vm),
|
|
74
|
-
// (this.expOrFn = expOrFn),
|
|
75
|
-
// (this.cb = cb),
|
|
76
|
-
// Reflect.apply(this.cb, this.vm, [this.evaluate()]);
|
|
77
|
-
// }
|
|
78
|
-
/**
|
|
79
|
-
*
|
|
80
|
-
* 获取一个随机整数
|
|
81
|
-
*
|
|
82
|
-
* 可传入两个参数,获取两参数之间的任意数
|
|
83
|
-
*
|
|
84
|
-
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的整数
|
|
85
|
-
*
|
|
86
|
-
* @export
|
|
87
|
-
* @param {number} max 最大值
|
|
88
|
-
* @param {number} [min] 最小值
|
|
89
|
-
* @return {*} {number}
|
|
90
|
-
*/
|
|
91
|
-
function getRandomInt(max = 1, min = 0) {
|
|
92
|
-
let _min = Math.ceil(Number(min)), _max = Math.floor(Number(max));
|
|
93
|
-
_min > _max && ([_max, _min] = [_min, _max]);
|
|
94
|
-
return Math.floor(Math.random() * (_max - _min + 1) + _min);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
*
|
|
98
|
-
* 获取一个随机浮点数数
|
|
99
|
-
*
|
|
100
|
-
* 可传入两个参数,获取两参数之间的任意数
|
|
101
|
-
*
|
|
102
|
-
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的浮点数数
|
|
103
|
-
*
|
|
104
|
-
* @export
|
|
105
|
-
* @param {number} max 最大值,缺省 1
|
|
106
|
-
* @param {number} [min] 最小值,缺省 0
|
|
107
|
-
* @return {*} {number}
|
|
108
|
-
*/
|
|
109
|
-
function getRandomFloat(max = 1, min = 0) {
|
|
110
|
-
if (max == min)
|
|
111
|
-
max++;
|
|
112
|
-
min > max && ([max, min] = [min, max]);
|
|
113
|
-
return Math.random() * (max - min) + min;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
exports.debounce = debounce;
|
|
117
|
-
exports.getRandomFloat = getRandomFloat;
|
|
118
|
-
exports.getRandomInt = getRandomInt;
|
|
119
|
-
exports.throttle = throttle;
|
|
120
|
-
exports.typeOf = typeOf;
|
package/mjs/src/data.mjs
DELETED
package/mjs/src/pureFunction.mjs
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 获取数据的类型
|
|
3
|
-
*
|
|
4
|
-
* @param {*} o 任意数据
|
|
5
|
-
* @return {*} 返回是一个字符串 {@link String},包含于 @see {@link TypeOf}
|
|
6
|
-
*/
|
|
7
|
-
function typeOf(o) {
|
|
8
|
-
return Reflect.apply(Object.prototype.toString, o, [])
|
|
9
|
-
.replace(/^.*\s(.*)]$/, '$1')
|
|
10
|
-
.toLowerCase();
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* 防抖
|
|
14
|
-
*
|
|
15
|
-
* 这种设计有一种不好的地方就是倘若最后一次尚未执行,不好清理
|
|
16
|
-
* @param {*} callback
|
|
17
|
-
* @param {number} delay 缺省 300 ms
|
|
18
|
-
* @return {*} 返回的是一个函数
|
|
19
|
-
*/
|
|
20
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
21
|
-
function debounce(callback, delay = 200) {
|
|
22
|
-
// 强制转换非数值
|
|
23
|
-
if (typeof delay != 'number' || isNaN(delay))
|
|
24
|
-
delay = 200;
|
|
25
|
-
let timeout;
|
|
26
|
-
return ((...args) => {
|
|
27
|
-
clearTimeout(timeout);
|
|
28
|
-
timeout = setTimeout(() => Reflect.apply(callback, null, args), Math.max(delay, 0));
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* 节流函数
|
|
33
|
-
* @param callback
|
|
34
|
-
* @param delay 延迟时间,单位为毫秒(ms),缺省 200(ms)
|
|
35
|
-
* @returns 返回的是一个函数
|
|
36
|
-
*/
|
|
37
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
-
function throttle(callback, delay = 200) {
|
|
39
|
-
// 强制转换非数值
|
|
40
|
-
if (typeof delay != 'number' || isNaN(delay))
|
|
41
|
-
delay = 200;
|
|
42
|
-
/** 延迟控制插销 */
|
|
43
|
-
let inThrottle = true;
|
|
44
|
-
return ((...args) => {
|
|
45
|
-
if (!inThrottle)
|
|
46
|
-
return;
|
|
47
|
-
Reflect.apply(callback, null, args);
|
|
48
|
-
inThrottle = false;
|
|
49
|
-
setTimeout(() => (inThrottle = true), Math.max(delay, 0));
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
/** 响应的 */
|
|
53
|
-
// function responsive(target: any) {
|
|
54
|
-
// const handle = {
|
|
55
|
-
// get(target: any, key: any, receiver: any) {
|
|
56
|
-
// console.log(`捕获到 key: ${key}`);
|
|
57
|
-
// return Reflect.get(target, key, receiver);
|
|
58
|
-
// },
|
|
59
|
-
// set(target: any, key: any, value: any, receiver: any) {
|
|
60
|
-
// console.log(`设置值 ${value} 到属性 ${key}`);
|
|
61
|
-
// return Reflect.set(target, key, value, receiver);
|
|
62
|
-
// },
|
|
63
|
-
// };
|
|
64
|
-
// return new Proxy(target, handle);
|
|
65
|
-
// }
|
|
66
|
-
/**
|
|
67
|
-
* 监听
|
|
68
|
-
*/
|
|
69
|
-
// function Listener(vm: any, expOrFn: any, cb: any) {
|
|
70
|
-
// // @ts-ignore
|
|
71
|
-
// (this.vm = vm),
|
|
72
|
-
// (this.expOrFn = expOrFn),
|
|
73
|
-
// (this.cb = cb),
|
|
74
|
-
// Reflect.apply(this.cb, this.vm, [this.evaluate()]);
|
|
75
|
-
// }
|
|
76
|
-
/**
|
|
77
|
-
*
|
|
78
|
-
* 获取一个随机整数
|
|
79
|
-
*
|
|
80
|
-
* 可传入两个参数,获取两参数之间的任意数
|
|
81
|
-
*
|
|
82
|
-
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的整数
|
|
83
|
-
*
|
|
84
|
-
* @export
|
|
85
|
-
* @param {number} max 最大值
|
|
86
|
-
* @param {number} [min] 最小值
|
|
87
|
-
* @return {*} {number}
|
|
88
|
-
*/
|
|
89
|
-
function getRandomInt(max = 1, min = 0) {
|
|
90
|
-
let _min = Math.ceil(Number(min)), _max = Math.floor(Number(max));
|
|
91
|
-
_min > _max && ([_max, _min] = [_min, _max]);
|
|
92
|
-
return Math.floor(Math.random() * (_max - _min + 1) + _min);
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
*
|
|
96
|
-
* 获取一个随机浮点数数
|
|
97
|
-
*
|
|
98
|
-
* 可传入两个参数,获取两参数之间的任意数
|
|
99
|
-
*
|
|
100
|
-
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的浮点数数
|
|
101
|
-
*
|
|
102
|
-
* @export
|
|
103
|
-
* @param {number} max 最大值,缺省 1
|
|
104
|
-
* @param {number} [min] 最小值,缺省 0
|
|
105
|
-
* @return {*} {number}
|
|
106
|
-
*/
|
|
107
|
-
function getRandomFloat(max = 1, min = 0) {
|
|
108
|
-
if (max == min)
|
|
109
|
-
max++;
|
|
110
|
-
min > max && ([max, min] = [min, max]);
|
|
111
|
-
return Math.random() * (max - min) + min;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export { debounce, getRandomFloat, getRandomInt, throttle, typeOf };
|
package/types/src/data.d.ts
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
/** 数据类型
|
|
2
|
-
*
|
|
3
|
-
* {@link typeOf} 检测出来的数据类型
|
|
4
|
-
*
|
|
5
|
-
* - number 数组类型 {@link Number}
|
|
6
|
-
* - function 函数(一种特殊的对象) {@link Function}
|
|
7
|
-
* - string 字符串 {@link String}
|
|
8
|
-
* - array 数组(一种有序的对象) {@link Array}
|
|
9
|
-
* - boolean 布尔值 {@link Boolean} ,仅有 true 和 false 两种能值
|
|
10
|
-
* - undefined 值未定义或未赋值 {@link undefined}
|
|
11
|
-
* - object 对象 {@link Object}、{@link Proxy}
|
|
12
|
-
* - null 指向一个特殊的空对象 null
|
|
13
|
-
* - regexp 正则 {@link RegExp}
|
|
14
|
-
* - set 值的集合 {@link Set}
|
|
15
|
-
* - map 保存键值对,且有序的 {@link Map}
|
|
16
|
-
* - date 时间 {@link Date}
|
|
17
|
-
* - bigint 内置对象,它提供了一种方法来表示大于 2^53 - 1 的整数 {@link BigInt}
|
|
18
|
-
* - bigint64array 64 位有符号整数组成的数组 {@link BigInt64Array}
|
|
19
|
-
* - biguint64array 64 位无符号整数组成的数组 {@link BigInt64Array}
|
|
20
|
-
* - symbol
|
|
21
|
-
* - error 错误类型 {@link Error}、 {@link URIError}、{@link TypeError}、{@link SyntaxError}、${@link ReferenceError}、{@link RangeError}、{@link EvalError}、{@link AggregateError}
|
|
22
|
-
* - int8array 二进制补码 8 位有符号整数的数组 {@link Int8Array }
|
|
23
|
-
* - uint8array 8 位无符号整型数组 {@link Uint8Array}
|
|
24
|
-
* - int16array 二进制补码 16 位有符号整数的数组 {@link Int16Array}
|
|
25
|
-
* - uint16array 16 位无符号整数 {@link Uint16Array}
|
|
26
|
-
* - int32array二进制补码 32 位有符号整数的数组 {@link Int32Array}
|
|
27
|
-
* - uint32array 32 位无符号整型数组 {@link Uint32Array}
|
|
28
|
-
* - float32array 32 位的浮点数型数组 {@link Float32Array}
|
|
29
|
-
* - float64array 64 位的浮点数型数组 {@link Float64Array}
|
|
30
|
-
* - uint8clampedarray 8 位无符号整型固定数组 {@link Uint8ClampedArray}
|
|
31
|
-
* - sharedarraybuffer 可以用来在共享内存上创建视图的二进制数据缓冲区,目前,浏览器不支持 {@link SharedArrayBuffer}
|
|
32
|
-
* - promise 异步操作最终的完成(或失败)以及其结果值 {@link Promise}
|
|
33
|
-
* - window 全局对象 {@link Window}、{@link globalThis}
|
|
34
|
-
* - dataview 从二进制 ArrayBuffer 对象中读写多种数值类型的底层接口 {@link DataView}
|
|
35
|
-
* - atomics 命名空间对象包含对 SharedArrayBuffer 和 ArrayBuffer 对象执行原子操作的静态方法 {@link Atomics}
|
|
36
|
-
* - arraybuffer 通用的原始二进制数据缓冲区 {@link ArrayBuffer}
|
|
37
|
-
*
|
|
38
|
-
* _NaN 即便意思非数字的值,但依旧是 {@link Number} 类型_
|
|
39
|
-
*/
|
|
40
|
-
export type TypeOf = 'number' | 'function' | 'string' | 'boolean' | 'object' | 'undefined' | 'null' | 'array' | 'date' | 'set' | 'map' | 'symbol' | 'bigint' | 'bigint64array' | 'biguint64array' | 'regexp' | 'int8array' | 'uint8array' | 'uint16array' | 'float32array' | 'float64array' | 'uint32array' | 'error' | 'uint8clampedarray' | 'sharedarraybuffer' | 'promise' | 'window' | 'dataview' | 'atomics' | 'arraybuffer';
|
|
41
|
-
/**
|
|
42
|
-
* 获取数据的类型
|
|
43
|
-
*
|
|
44
|
-
* @param {*} o 任意数据
|
|
45
|
-
* @return {*} 返回是一个字符串 {@link String},包含于 @see {@link TypeOf}
|
|
46
|
-
*/
|
|
47
|
-
export declare function typeOf(o: unknown): TypeOf;
|
|
48
|
-
/**
|
|
49
|
-
* 防抖
|
|
50
|
-
*
|
|
51
|
-
* 这种设计有一种不好的地方就是倘若最后一次尚未执行,不好清理
|
|
52
|
-
* @param {*} callback
|
|
53
|
-
* @param {number} delay 缺省 300 ms
|
|
54
|
-
* @return {*} 返回的是一个函数
|
|
55
|
-
*/
|
|
56
|
-
export declare function debounce<T extends (...args: any[]) => void>(callback: T, delay?: number): T;
|
|
57
|
-
/**
|
|
58
|
-
* 节流函数
|
|
59
|
-
* @param callback
|
|
60
|
-
* @param delay 延迟时间,单位为毫秒(ms),缺省 200(ms)
|
|
61
|
-
* @returns 返回的是一个函数
|
|
62
|
-
*/
|
|
63
|
-
export declare function throttle<T extends (...args: any[]) => void>(callback: T, delay?: number): T;
|
|
64
|
-
/** 响应的 */
|
|
65
|
-
/**
|
|
66
|
-
* 监听
|
|
67
|
-
*/
|
|
68
|
-
/**
|
|
69
|
-
*
|
|
70
|
-
* 获取一个随机整数
|
|
71
|
-
*
|
|
72
|
-
* 可传入两个参数,获取两参数之间的任意数
|
|
73
|
-
*
|
|
74
|
-
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的整数
|
|
75
|
-
*
|
|
76
|
-
* @export
|
|
77
|
-
* @param {number} max 最大值
|
|
78
|
-
* @param {number} [min] 最小值
|
|
79
|
-
* @return {*} {number}
|
|
80
|
-
*/
|
|
81
|
-
export declare function getRandomInt(max?: number, min?: number): number;
|
|
82
|
-
/**
|
|
83
|
-
*
|
|
84
|
-
* 获取一个随机浮点数数
|
|
85
|
-
*
|
|
86
|
-
* 可传入两个参数,获取两参数之间的任意数
|
|
87
|
-
*
|
|
88
|
-
* 若只传入一个参数,这获取小于(若提供的值为负数,则为大于)该数的浮点数数
|
|
89
|
-
*
|
|
90
|
-
* @export
|
|
91
|
-
* @param {number} max 最大值,缺省 1
|
|
92
|
-
* @param {number} [min] 最小值,缺省 0
|
|
93
|
-
* @return {*} {number}
|
|
94
|
-
*/
|
|
95
|
-
export declare function getRandomFloat(max?: number, min?: number): number;
|