lyb-js 1.0.2 → 1.0.3
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/README.md +406 -9
- package/dist/Base/LibGetDataType/index.d.ts +3 -3
- package/dist/Base/LibGetDataType/index.js +3 -3
- package/dist/Browser/LibColorConsole/index.d.ts +2 -2
- package/dist/Browser/LibColorConsole/index.js +3 -3
- package/dist/Browser/LibIsMobile/index.d.ts +1 -1
- package/dist/Browser/LibIsMobile/index.js +1 -1
- package/dist/Browser/LibIsPad/index.d.ts +1 -1
- package/dist/Browser/LibIsPad/index.js +1 -1
- package/dist/Browser/LibPathParams/index.d.ts +1 -1
- package/dist/Browser/LibPathParams/index.js +1 -1
- package/dist/Data/LibChunkArray/index.d.ts +1 -1
- package/dist/Data/LibChunkArray/index.js +1 -1
- package/dist/Data/LibDeepJSONParse/index.d.ts +1 -1
- package/dist/Data/LibDeepJSONParse/index.js +7 -7
- package/dist/Data/LibGroupArrayByKey/index.d.ts +4 -4
- package/dist/Data/LibGroupArrayByKey/index.js +4 -4
- package/dist/Data/LibMatchEmail/index.d.ts +1 -1
- package/dist/Data/LibMatchEmail/index.js +1 -1
- package/dist/Data/LibShuffleArray/index.d.ts +2 -2
- package/dist/Data/LibShuffleArray/index.js +2 -2
- package/dist/Data/LibStepArray/index.d.ts +1 -1
- package/dist/Data/LibStepArray/index.js +1 -1
- package/dist/File/LibDownloadImageLink/index.d.ts +2 -2
- package/dist/File/LibDownloadImageLink/index.js +2 -2
- package/dist/File/LibImageOptimizer/index.d.ts +5 -5
- package/dist/File/LibImageOptimizer/index.js +4 -4
- package/dist/File/LibSaveJson/index.d.ts +1 -0
- package/dist/File/LibSaveJson/index.js +1 -0
- package/dist/Formatter/LibFormatterByte/index.d.ts +1 -1
- package/dist/Formatter/LibFormatterByte/index.js +1 -1
- package/dist/Formatter/LibMaskPhoneNumber/index.d.ts +3 -3
- package/dist/Formatter/LibMaskPhoneNumber/index.js +3 -3
- package/dist/Formatter/LibNumComma/index.d.ts +1 -1
- package/dist/Formatter/LibNumComma/index.js +1 -1
- package/dist/Formatter/LibNumberUnit/index.d.ts +1 -1
- package/dist/Formatter/LibNumberUnit/index.js +1 -1
- package/dist/Formatter/LibSecondsFormatterChinese/index.d.ts +11 -0
- package/dist/Formatter/LibSecondsFormatterChinese/index.js +42 -0
- package/dist/Math/LibCalculateExpression/index.d.ts +1 -2
- package/dist/Math/LibCalculateExpression/index.js +17 -18
- package/dist/Math/LibConvertAngle/index.d.ts +4 -4
- package/dist/Math/LibConvertAngle/index.js +4 -4
- package/dist/Math/LibCoordsAngle/index.d.ts +3 -3
- package/dist/Math/LibCoordsAngle/index.js +8 -8
- package/dist/Math/LibCoordsDistance/index.d.ts +3 -3
- package/dist/Math/LibCoordsDistance/index.js +5 -5
- package/dist/Math/LibDecimal/index.d.ts +3 -4
- package/dist/Math/LibDecimal/index.js +3 -4
- package/dist/Misc/LibRegFormValidate/index.d.ts +9 -9
- package/dist/Misc/LibRegFormValidate/index.js +9 -9
- package/dist/Random/LibProbabilityResult/index.d.ts +3 -3
- package/dist/Random/LibProbabilityResult/index.js +3 -3
- package/dist/Random/LibRandom/index.d.ts +3 -4
- package/dist/Random/LibRandom/index.js +3 -4
- package/dist/Random/LibRandomColor/index.d.ts +2 -2
- package/dist/Random/LibRandomColor/index.js +2 -2
- package/dist/Random/LibUniqueRandomNumbers/index.d.ts +3 -3
- package/dist/Random/LibUniqueRandomNumbers/index.js +3 -3
- package/dist/Time/LibSameTimeCheck/index.d.ts +5 -1
- package/dist/Time/LibSameTimeCheck/index.js +5 -1
- package/dist/Time/LibTimeAgo/index.d.ts +4 -4
- package/dist/Time/LibTimeAgo/index.js +4 -4
- package/dist/Time/LibTimeGreeting/index.d.ts +5 -5
- package/dist/Time/LibTimeGreeting/index.js +5 -5
- package/dist/lib.d.ts +77 -78
- package/dist/lib.js +78 -79
- package/package.json +1 -1
- package/test/index.js +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/** @description
|
|
1
|
+
/** @description 将数组打乱顺序
|
|
2
2
|
* @param arr 需要乱序的数组
|
|
3
3
|
* @example
|
|
4
4
|
* const shuffled = libShuffleArray([1, 2, 3, 4, 5]);
|
|
5
|
-
* console.log(shuffled); //
|
|
5
|
+
* console.log(shuffled); //[3, 5, 2, 1, 4] (结果每次不同)
|
|
6
6
|
*/
|
|
7
7
|
export declare const libShuffleArray: <T>(arr: T[]) => T[];
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/** @description
|
|
1
|
+
/** @description 将数组打乱顺序
|
|
2
2
|
* @param arr 需要乱序的数组
|
|
3
3
|
* @example
|
|
4
4
|
* const shuffled = libShuffleArray([1, 2, 3, 4, 5]);
|
|
5
|
-
* console.log(shuffled); //
|
|
5
|
+
* console.log(shuffled); //[3, 5, 2, 1, 4] (结果每次不同)
|
|
6
6
|
*/
|
|
7
7
|
export const libShuffleArray = (arr) => {
|
|
8
8
|
const newArr = [...arr];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/** @description
|
|
1
|
+
/** @description 将链接图片下载到本地
|
|
2
2
|
* @param link 图片链接
|
|
3
3
|
* @param name 图片名称
|
|
4
4
|
* @example
|
|
5
|
-
* libDownloadImageLink("https://example.com/image.jpg", "
|
|
5
|
+
* libDownloadImageLink("https://example.com/image.jpg", "图片.jpg");
|
|
6
6
|
*/
|
|
7
7
|
export declare const libDownloadImageLink: (link: string, name: string) => void;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/** @description
|
|
1
|
+
/** @description 将链接图片下载到本地
|
|
2
2
|
* @param link 图片链接
|
|
3
3
|
* @param name 图片名称
|
|
4
4
|
* @example
|
|
5
|
-
* libDownloadImageLink("https://example.com/image.jpg", "
|
|
5
|
+
* libDownloadImageLink("https://example.com/image.jpg", "图片.jpg");
|
|
6
6
|
*/
|
|
7
7
|
export const libDownloadImageLink = (link, name) => {
|
|
8
8
|
fetch(link)
|
|
@@ -8,21 +8,21 @@ export type LibImageOptimizerOptionsParams = {
|
|
|
8
8
|
/** 超过多大进行压缩 */
|
|
9
9
|
maxSize?: number;
|
|
10
10
|
/** 成功回调 */
|
|
11
|
-
success: (
|
|
11
|
+
success: (formData: FormData, file: File, url: string) => void;
|
|
12
12
|
/** 失败回调 */
|
|
13
13
|
fail?: (error: ErrorEvent) => void;
|
|
14
14
|
};
|
|
15
|
-
/** @description
|
|
15
|
+
/** @description 图片压缩,支持png压缩,保留透明背景
|
|
16
16
|
* @param obj 压缩参数
|
|
17
17
|
* @example
|
|
18
|
-
*
|
|
18
|
+
* //图片压缩使用示例
|
|
19
19
|
* libImageOptimizerOptionsParams({
|
|
20
20
|
* file: myFile,
|
|
21
21
|
* ratio: 0.8,
|
|
22
22
|
* width: 800,
|
|
23
23
|
* maxSize: 1024,
|
|
24
|
-
* success: (
|
|
25
|
-
* console.log('压缩成功',
|
|
24
|
+
* success: (formData, file, url) => {
|
|
25
|
+
* console.log('压缩成功', formData, file, url);
|
|
26
26
|
* },
|
|
27
27
|
* fail: (error) => {
|
|
28
28
|
* console.error('压缩失败', error);
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
/** @description
|
|
1
|
+
/** @description 图片压缩,支持png压缩,保留透明背景
|
|
2
2
|
* @param obj 压缩参数
|
|
3
3
|
* @example
|
|
4
|
-
*
|
|
4
|
+
* //图片压缩使用示例
|
|
5
5
|
* libImageOptimizerOptionsParams({
|
|
6
6
|
* file: myFile,
|
|
7
7
|
* ratio: 0.8,
|
|
8
8
|
* width: 800,
|
|
9
9
|
* maxSize: 1024,
|
|
10
|
-
* success: (
|
|
11
|
-
* console.log('压缩成功',
|
|
10
|
+
* success: (formData, file, url) => {
|
|
11
|
+
* console.log('压缩成功', formData, file, url);
|
|
12
12
|
* },
|
|
13
13
|
* fail: (error) => {
|
|
14
14
|
* console.error('压缩失败', error);
|
|
@@ -4,6 +4,6 @@
|
|
|
4
4
|
* @returns ['大小', '单位', '大小及单位']
|
|
5
5
|
* @example
|
|
6
6
|
* const [size, unit, formatted] = libFormatterByte(2048);
|
|
7
|
-
* console.log(size, unit, formatted); //
|
|
7
|
+
* console.log(size, unit, formatted); //2.00 KB 2.00 KB
|
|
8
8
|
*/
|
|
9
9
|
export declare const libFormatterByte: (bytes: number) => (string | number)[];
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @returns ['大小', '单位', '大小及单位']
|
|
5
5
|
* @example
|
|
6
6
|
* const [size, unit, formatted] = libFormatterByte(2048);
|
|
7
|
-
* console.log(size, unit, formatted); //
|
|
7
|
+
* console.log(size, unit, formatted); //2.00 KB 2.00 KB
|
|
8
8
|
*/
|
|
9
9
|
export const libFormatterByte = (bytes) => {
|
|
10
10
|
if (bytes <= 0)
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @description
|
|
3
|
-
* @param mobile
|
|
2
|
+
* @description 隐藏手机号码中间的四位数字
|
|
3
|
+
* @param mobile 需要处理的手机号码
|
|
4
4
|
* @example
|
|
5
5
|
* const masked = libMaskPhoneNumber("13812345678");
|
|
6
|
-
* console.log(masked); //
|
|
6
|
+
* console.log(masked); //138****5678
|
|
7
7
|
*/
|
|
8
8
|
export declare const libMaskPhoneNumber: (mobile: number | string) => string;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @description
|
|
3
|
-
* @param mobile
|
|
2
|
+
* @description 隐藏手机号码中间的四位数字
|
|
3
|
+
* @param mobile 需要处理的手机号码
|
|
4
4
|
* @example
|
|
5
5
|
* const masked = libMaskPhoneNumber("13812345678");
|
|
6
|
-
* console.log(masked); //
|
|
6
|
+
* console.log(masked); //138****5678
|
|
7
7
|
*/
|
|
8
8
|
export const libMaskPhoneNumber = (mobile) => {
|
|
9
9
|
const m = mobile.toString();
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* @param reserve 保留小数位数
|
|
5
5
|
* @example
|
|
6
6
|
* const formatted = libNumComma(1234567.89);
|
|
7
|
-
* console.log(formatted); //
|
|
7
|
+
* console.log(formatted); //1,234,567.89
|
|
8
8
|
*/
|
|
9
9
|
export const libNumComma = (num, reserve = 2) => {
|
|
10
10
|
const str = num.toFixed(reserve).toString();
|
|
@@ -7,6 +7,6 @@ export interface LibNumberUnitParams {
|
|
|
7
7
|
* @returns [数字, 单位]
|
|
8
8
|
* @example
|
|
9
9
|
* const [value, unit] = libNumberUnit(1500, { K: 1000, M: 1000000 });
|
|
10
|
-
* console.log(value, unit); //
|
|
10
|
+
* console.log(value, unit); //1.50 K
|
|
11
11
|
*/
|
|
12
12
|
export declare const libNumberUnit: (num: number, units: LibNumberUnitParams) => string[];
|
|
@@ -5,7 +5,7 @@ import Decimal from "decimal.js";
|
|
|
5
5
|
* @returns [数字, 单位]
|
|
6
6
|
* @example
|
|
7
7
|
* const [value, unit] = libNumberUnit(1500, { K: 1000, M: 1000000 });
|
|
8
|
-
* console.log(value, unit); //
|
|
8
|
+
* console.log(value, unit); //1.50 K
|
|
9
9
|
*/
|
|
10
10
|
export const libNumberUnit = (num, units) => {
|
|
11
11
|
const decimalValue = new Decimal(num);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 将秒数格式化为中文时间描述,支持扩展到年
|
|
3
|
+
* @param seconds 秒数
|
|
4
|
+
* @returns 格式化后的中文时间
|
|
5
|
+
* @example
|
|
6
|
+
* libSecondsFormatterChinese(100000); //"1天3小时46分40秒"
|
|
7
|
+
* libSecondsFormatterChinese(31536000); //"1年"
|
|
8
|
+
* libSecondsFormatterChinese(3600); //"1小时"
|
|
9
|
+
* libSecondsFormatterChinese(90); //"1分30秒"
|
|
10
|
+
*/
|
|
11
|
+
export declare const libSecondsFormatterChinese: (seconds: number) => string;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import dayjs from "dayjs";
|
|
2
|
+
import duration from "dayjs/plugin/duration";
|
|
3
|
+
dayjs.extend(duration);
|
|
4
|
+
/**
|
|
5
|
+
* @description 将秒数格式化为中文时间描述,支持扩展到年
|
|
6
|
+
* @param seconds 秒数
|
|
7
|
+
* @returns 格式化后的中文时间
|
|
8
|
+
* @example
|
|
9
|
+
* libSecondsFormatterChinese(100000); //"1天3小时46分40秒"
|
|
10
|
+
* libSecondsFormatterChinese(31536000); //"1年"
|
|
11
|
+
* libSecondsFormatterChinese(3600); //"1小时"
|
|
12
|
+
* libSecondsFormatterChinese(90); //"1分30秒"
|
|
13
|
+
*/
|
|
14
|
+
export const libSecondsFormatterChinese = (seconds) => {
|
|
15
|
+
const duration = dayjs.duration(seconds, "seconds");
|
|
16
|
+
const years = Math.floor(duration.asYears());
|
|
17
|
+
const months = Math.floor(duration.asMonths() % 12);
|
|
18
|
+
const days = Math.floor(duration.asDays() % 30);
|
|
19
|
+
const hours = duration.hours();
|
|
20
|
+
const minutes = duration.minutes();
|
|
21
|
+
const remainingSeconds = duration.seconds();
|
|
22
|
+
const timeParts = [];
|
|
23
|
+
if (years > 0) {
|
|
24
|
+
timeParts.push(`${years}年`);
|
|
25
|
+
}
|
|
26
|
+
if (months > 0) {
|
|
27
|
+
timeParts.push(`${months}月`);
|
|
28
|
+
}
|
|
29
|
+
if (days > 0) {
|
|
30
|
+
timeParts.push(`${days}天`);
|
|
31
|
+
}
|
|
32
|
+
if (hours > 0) {
|
|
33
|
+
timeParts.push(`${hours}小时`);
|
|
34
|
+
}
|
|
35
|
+
if (minutes > 0) {
|
|
36
|
+
timeParts.push(`${minutes}分`);
|
|
37
|
+
}
|
|
38
|
+
if (timeParts.length === 0 || remainingSeconds > 0) {
|
|
39
|
+
timeParts.push(`${remainingSeconds}秒`);
|
|
40
|
+
}
|
|
41
|
+
return timeParts.join("");
|
|
42
|
+
};
|
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
* @param expression 表达式字符串
|
|
3
3
|
* @param point 小数点精度
|
|
4
4
|
* @returns 计算结果
|
|
5
|
-
*
|
|
6
5
|
* @example
|
|
7
6
|
* const result = libCalculateExpression("(1+2)-(3*4)/5");
|
|
8
|
-
* console.log(result); //
|
|
7
|
+
* console.log(result); //0.6
|
|
9
8
|
*/
|
|
10
9
|
export declare const libCalculateExpression: (expression: string, point?: number) => number;
|
|
@@ -3,37 +3,36 @@ import Decimal from 'decimal.js';
|
|
|
3
3
|
* @param expression 表达式字符串
|
|
4
4
|
* @param point 小数点精度
|
|
5
5
|
* @returns 计算结果
|
|
6
|
-
*
|
|
7
6
|
* @example
|
|
8
7
|
* const result = libCalculateExpression("(1+2)-(3*4)/5");
|
|
9
|
-
* console.log(result); //
|
|
8
|
+
* console.log(result); //0.6
|
|
10
9
|
*/
|
|
11
10
|
export const libCalculateExpression = (expression, point = 2) => {
|
|
12
|
-
|
|
11
|
+
//清除所有空格
|
|
13
12
|
expression = expression.replace(/\s+/g, '');
|
|
14
|
-
|
|
13
|
+
//支持的运算符和优先级
|
|
15
14
|
const operators = {
|
|
16
15
|
"+": 1,
|
|
17
16
|
"-": 1,
|
|
18
17
|
"*": 2,
|
|
19
18
|
"/": 2,
|
|
20
19
|
};
|
|
21
|
-
|
|
20
|
+
//支持的小数点精度
|
|
22
21
|
const toDecimal = (value) => new Decimal(value);
|
|
23
|
-
|
|
22
|
+
//判断字符是否是运算符
|
|
24
23
|
const isOperator = (char) => ['+', '-', '*', '/'].includes(char);
|
|
25
|
-
|
|
24
|
+
//判断字符是否是数字(包括小数点)
|
|
26
25
|
const isNumber = (char) => /[0-9.]/.test(char);
|
|
27
|
-
|
|
26
|
+
//解析表达式并计算
|
|
28
27
|
const evaluate = (expression) => {
|
|
29
|
-
const outputQueue = [];
|
|
30
|
-
const operatorStack = [];
|
|
28
|
+
const outputQueue = []; //输出队列
|
|
29
|
+
const operatorStack = []; //操作符栈
|
|
31
30
|
let i = 0;
|
|
32
31
|
while (i < expression.length) {
|
|
33
32
|
const char = expression[i];
|
|
34
33
|
if (isNumber(char)) {
|
|
35
34
|
let numStr = '';
|
|
36
|
-
|
|
35
|
+
//处理多位数字(支持小数)
|
|
37
36
|
while (i < expression.length && isNumber(expression[i])) {
|
|
38
37
|
numStr += expression[i];
|
|
39
38
|
i++;
|
|
@@ -45,15 +44,15 @@ export const libCalculateExpression = (expression, point = 2) => {
|
|
|
45
44
|
i++;
|
|
46
45
|
}
|
|
47
46
|
else if (char === ')') {
|
|
48
|
-
|
|
47
|
+
//处理右括号,直到遇到左括号
|
|
49
48
|
while (operatorStack.length > 0 && operatorStack[operatorStack.length - 1] !== '(') {
|
|
50
49
|
outputQueue.push(operatorStack.pop());
|
|
51
50
|
}
|
|
52
|
-
operatorStack.pop();
|
|
51
|
+
operatorStack.pop(); //弹出左括号
|
|
53
52
|
i++;
|
|
54
53
|
}
|
|
55
54
|
else if (isOperator(char)) {
|
|
56
|
-
|
|
55
|
+
//运算符
|
|
57
56
|
while (operatorStack.length > 0 && operators[operatorStack[operatorStack.length - 1]] >= operators[char]) {
|
|
58
57
|
outputQueue.push(operatorStack.pop());
|
|
59
58
|
}
|
|
@@ -64,11 +63,11 @@ export const libCalculateExpression = (expression, point = 2) => {
|
|
|
64
63
|
throw new Error(`无效字符: ${char}`);
|
|
65
64
|
}
|
|
66
65
|
}
|
|
67
|
-
|
|
66
|
+
//把所有剩余的操作符添加到输出队列
|
|
68
67
|
while (operatorStack.length > 0) {
|
|
69
68
|
outputQueue.push(operatorStack.pop());
|
|
70
69
|
}
|
|
71
|
-
|
|
70
|
+
//执行运算
|
|
72
71
|
const calcStack = [];
|
|
73
72
|
for (let token of outputQueue) {
|
|
74
73
|
if (typeof token === 'string') {
|
|
@@ -98,9 +97,9 @@ export const libCalculateExpression = (expression, point = 2) => {
|
|
|
98
97
|
return calcStack.pop();
|
|
99
98
|
};
|
|
100
99
|
try {
|
|
101
|
-
|
|
100
|
+
//调用计算器并返回结果
|
|
102
101
|
const result = evaluate(expression);
|
|
103
|
-
return Number(result.toFixed(point));
|
|
102
|
+
return Number(result.toFixed(point)); //保留指定的小数位数
|
|
104
103
|
}
|
|
105
104
|
catch (error) {
|
|
106
105
|
throw new Error("表达式计算失败:" + error.message);
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
* @param value 角度值或弧度值
|
|
4
4
|
* @param type 角度类型或弧度类型
|
|
5
5
|
* @example
|
|
6
|
-
*
|
|
7
|
-
* libConvertAngle(90, "rad");
|
|
6
|
+
* //角度转弧度
|
|
7
|
+
* libConvertAngle(90, "rad"); //返回 1.5708... (π/2)
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
* libConvertAngle(Math.PI, "deg");
|
|
9
|
+
* //弧度转角度
|
|
10
|
+
* libConvertAngle(Math.PI, "deg"); //返回 180
|
|
11
11
|
*/
|
|
12
12
|
export declare const libConvertAngle: (value: number, type: "rad" | "deg") => number;
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
* @param value 角度值或弧度值
|
|
4
4
|
* @param type 角度类型或弧度类型
|
|
5
5
|
* @example
|
|
6
|
-
*
|
|
7
|
-
* libConvertAngle(90, "rad");
|
|
6
|
+
* //角度转弧度
|
|
7
|
+
* libConvertAngle(90, "rad"); //返回 1.5708... (π/2)
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
* libConvertAngle(Math.PI, "deg");
|
|
9
|
+
* //弧度转角度
|
|
10
|
+
* libConvertAngle(Math.PI, "deg"); //返回 180
|
|
11
11
|
*/
|
|
12
12
|
export const libConvertAngle = (value, type) => {
|
|
13
13
|
if (type === "rad") {
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* @param coord1 起点坐标
|
|
3
3
|
* @param coord2 终点坐标
|
|
4
4
|
* @example
|
|
5
|
-
* libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 0 }); //
|
|
6
|
-
* libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 1 }); //
|
|
7
|
-
* libCoordsAngle({ x: 0, y: 0 }, { x: 0, y: 1 }); //
|
|
5
|
+
* libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 0 }); //0
|
|
6
|
+
* libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 1 }); //45
|
|
7
|
+
* libCoordsAngle({ x: 0, y: 0 }, { x: 0, y: 1 }); //90
|
|
8
8
|
*/
|
|
9
9
|
export declare const libCoordsAngle: (coord1: {
|
|
10
10
|
x: number;
|
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
* @param coord1 起点坐标
|
|
3
3
|
* @param coord2 终点坐标
|
|
4
4
|
* @example
|
|
5
|
-
* libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 0 }); //
|
|
6
|
-
* libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 1 }); //
|
|
7
|
-
* libCoordsAngle({ x: 0, y: 0 }, { x: 0, y: 1 }); //
|
|
5
|
+
* libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 0 }); //0
|
|
6
|
+
* libCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 1 }); //45
|
|
7
|
+
* libCoordsAngle({ x: 0, y: 0 }, { x: 0, y: 1 }); //90
|
|
8
8
|
*/
|
|
9
9
|
export const libCoordsAngle = (coord1, coord2) => {
|
|
10
|
-
|
|
10
|
+
//计算相对于第一个坐标的水平和垂直距离
|
|
11
11
|
const deltaX = coord2.x - coord1.x;
|
|
12
12
|
const deltaY = coord2.y - coord1.y;
|
|
13
|
-
|
|
13
|
+
//使用反三角函数计算角度(以弧度为单位)
|
|
14
14
|
const angleRad = Math.atan2(deltaY, deltaX);
|
|
15
|
-
|
|
15
|
+
//将弧度转换为角度
|
|
16
16
|
let angleDeg = angleRad * (180 / Math.PI);
|
|
17
|
-
|
|
17
|
+
//将角度转换为顺时针方向为正方向的角度
|
|
18
18
|
angleDeg = -angleDeg + 90;
|
|
19
|
-
|
|
19
|
+
//调整角度使得右边成为 360 度的位置变为 0 度
|
|
20
20
|
if (angleDeg < 0) {
|
|
21
21
|
angleDeg += 360;
|
|
22
22
|
}
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* @param coord1 起点坐标
|
|
3
3
|
* @param coord2 终点坐标
|
|
4
4
|
* @example
|
|
5
|
-
* libCoordsDistance({ x: 0, y: 0 }, { x: 3, y: 4 }); //
|
|
6
|
-
* libCoordsDistance({ x: 1, y: 1 }, { x: 4, y: 5 }); //
|
|
7
|
-
* libCoordsDistance({ x: 0, y: 0 }, { x: 0, y: 0 }); //
|
|
5
|
+
* libCoordsDistance({ x: 0, y: 0 }, { x: 3, y: 4 }); //5
|
|
6
|
+
* libCoordsDistance({ x: 1, y: 1 }, { x: 4, y: 5 }); //5
|
|
7
|
+
* libCoordsDistance({ x: 0, y: 0 }, { x: 0, y: 0 }); //0
|
|
8
8
|
*/
|
|
9
9
|
export declare const libCoordsDistance: (coord1: {
|
|
10
10
|
x: number;
|
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
* @param coord1 起点坐标
|
|
3
3
|
* @param coord2 终点坐标
|
|
4
4
|
* @example
|
|
5
|
-
* libCoordsDistance({ x: 0, y: 0 }, { x: 3, y: 4 }); //
|
|
6
|
-
* libCoordsDistance({ x: 1, y: 1 }, { x: 4, y: 5 }); //
|
|
7
|
-
* libCoordsDistance({ x: 0, y: 0 }, { x: 0, y: 0 }); //
|
|
5
|
+
* libCoordsDistance({ x: 0, y: 0 }, { x: 3, y: 4 }); //5
|
|
6
|
+
* libCoordsDistance({ x: 1, y: 1 }, { x: 4, y: 5 }); //5
|
|
7
|
+
* libCoordsDistance({ x: 0, y: 0 }, { x: 0, y: 0 }); //0
|
|
8
8
|
*/
|
|
9
9
|
export const libCoordsDistance = (coord1, coord2) => {
|
|
10
|
-
|
|
10
|
+
//计算两个坐标之间的水平和垂直距离
|
|
11
11
|
const deltaX = coord2.x - coord1.x;
|
|
12
12
|
const deltaY = coord2.y - coord1.y;
|
|
13
|
-
|
|
13
|
+
//使用勾股定理计算两点之间的距离
|
|
14
14
|
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
|
|
15
15
|
return distance;
|
|
16
16
|
};
|
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
* @param num2 第二个数
|
|
4
4
|
* @param operator 运算符,支持加减乘除
|
|
5
5
|
* @example
|
|
6
|
-
* //
|
|
7
|
-
* libDecimal(10, 3, "
|
|
8
|
-
* libDecimal(10, 3, "
|
|
9
|
-
* libDecimal(10, 3, "/", 2); // 3.33
|
|
6
|
+
* libDecimal(10, 3, "+"); //13
|
|
7
|
+
* libDecimal(10, 3, "-"); //7
|
|
8
|
+
* libDecimal(10, 3, "/", 2); //3.33
|
|
10
9
|
*/
|
|
11
10
|
export declare const libDecimal: (num1: number, num2: number, operator: "+" | "-" | "*" | "/", point?: number) => number;
|
|
@@ -4,10 +4,9 @@ import { Decimal } from "decimal.js";
|
|
|
4
4
|
* @param num2 第二个数
|
|
5
5
|
* @param operator 运算符,支持加减乘除
|
|
6
6
|
* @example
|
|
7
|
-
* //
|
|
8
|
-
* libDecimal(10, 3, "
|
|
9
|
-
* libDecimal(10, 3, "
|
|
10
|
-
* libDecimal(10, 3, "/", 2); // 3.33
|
|
7
|
+
* libDecimal(10, 3, "+"); //13
|
|
8
|
+
* libDecimal(10, 3, "-"); //7
|
|
9
|
+
* libDecimal(10, 3, "/", 2); //3.33
|
|
11
10
|
*/
|
|
12
11
|
export const libDecimal = (num1, num2, operator, point = 2) => {
|
|
13
12
|
const calc = {
|
|
@@ -4,10 +4,10 @@ export type ValidationResult = {
|
|
|
4
4
|
name: string;
|
|
5
5
|
};
|
|
6
6
|
/**
|
|
7
|
-
* @description
|
|
8
|
-
* @param form
|
|
9
|
-
* @param rules
|
|
10
|
-
* @returns
|
|
7
|
+
* @description 通过传递对象数字的方式进行正则或自定义函数进行验证
|
|
8
|
+
* @param form 表单数据对象
|
|
9
|
+
* @param rules 验证规则数组
|
|
10
|
+
* @returns 验证结果数组,包含未通过验证的项
|
|
11
11
|
* @example
|
|
12
12
|
* const form = { username: "john", email: "john@example.com" };
|
|
13
13
|
* const rules = [
|
|
@@ -15,14 +15,14 @@ export type ValidationResult = {
|
|
|
15
15
|
* { key: "email", verify: /^\S+@\S+\.\S+$/, msg: "邮箱格式不正确", name: "邮箱" },
|
|
16
16
|
* ];
|
|
17
17
|
* libRegFormValidate(form, rules);
|
|
18
|
-
*
|
|
18
|
+
* //返回结果: []
|
|
19
19
|
*
|
|
20
20
|
* const invalidForm = { username: "jo", email: "invalid-email" };
|
|
21
21
|
* libRegFormValidate(invalidForm, rules);
|
|
22
|
-
*
|
|
23
|
-
* //
|
|
24
|
-
* //
|
|
25
|
-
* //
|
|
22
|
+
* //返回结果: [
|
|
23
|
+
* // { key: "username", msg: "用户名不合法", name: "用户名" },
|
|
24
|
+
* // { key: "email", msg: "邮箱格式不正确", name: "邮箱" }
|
|
25
|
+
* //]
|
|
26
26
|
*/
|
|
27
27
|
export declare const libRegFormValidate: (form: Record<string, any>, rules: Array<{
|
|
28
28
|
key: string;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @description
|
|
3
|
-
* @param form
|
|
4
|
-
* @param rules
|
|
5
|
-
* @returns
|
|
2
|
+
* @description 通过传递对象数字的方式进行正则或自定义函数进行验证
|
|
3
|
+
* @param form 表单数据对象
|
|
4
|
+
* @param rules 验证规则数组
|
|
5
|
+
* @returns 验证结果数组,包含未通过验证的项
|
|
6
6
|
* @example
|
|
7
7
|
* const form = { username: "john", email: "john@example.com" };
|
|
8
8
|
* const rules = [
|
|
@@ -10,14 +10,14 @@
|
|
|
10
10
|
* { key: "email", verify: /^\S+@\S+\.\S+$/, msg: "邮箱格式不正确", name: "邮箱" },
|
|
11
11
|
* ];
|
|
12
12
|
* libRegFormValidate(form, rules);
|
|
13
|
-
*
|
|
13
|
+
* //返回结果: []
|
|
14
14
|
*
|
|
15
15
|
* const invalidForm = { username: "jo", email: "invalid-email" };
|
|
16
16
|
* libRegFormValidate(invalidForm, rules);
|
|
17
|
-
*
|
|
18
|
-
* //
|
|
19
|
-
* //
|
|
20
|
-
* //
|
|
17
|
+
* //返回结果: [
|
|
18
|
+
* // { key: "username", msg: "用户名不合法", name: "用户名" },
|
|
19
|
+
* // { key: "email", msg: "邮箱格式不正确", name: "邮箱" }
|
|
20
|
+
* //]
|
|
21
21
|
*/
|
|
22
22
|
export const libRegFormValidate = (form, rules) => {
|
|
23
23
|
return rules.reduce((result, rule) => {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/** @description 百分比概率结果
|
|
2
2
|
* @param probability 触发概率,百分比,0-100
|
|
3
3
|
* @example
|
|
4
|
-
* libProbabilityResult(50); //
|
|
5
|
-
* libProbabilityResult(80); //
|
|
6
|
-
* libProbabilityResult(100); //
|
|
4
|
+
* libProbabilityResult(50); //50% 概率为 true
|
|
5
|
+
* libProbabilityResult(80); //80% 概率为 true
|
|
6
|
+
* libProbabilityResult(100); //100% 概率为 true
|
|
7
7
|
*/
|
|
8
8
|
export declare const libProbabilityResult: (probability: number) => boolean;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/** @description 百分比概率结果
|
|
2
2
|
* @param probability 触发概率,百分比,0-100
|
|
3
3
|
* @example
|
|
4
|
-
* libProbabilityResult(50); //
|
|
5
|
-
* libProbabilityResult(80); //
|
|
6
|
-
* libProbabilityResult(100); //
|
|
4
|
+
* libProbabilityResult(50); //50% 概率为 true
|
|
5
|
+
* libProbabilityResult(80); //80% 概率为 true
|
|
6
|
+
* libProbabilityResult(100); //100% 概率为 true
|
|
7
7
|
*/
|
|
8
8
|
export const libProbabilityResult = (probability) => Math.random() * 100 < probability;
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
/** @description
|
|
1
|
+
/** @description 随机获取两个数之间的值,包含两数自身
|
|
2
2
|
* @param min 最小值
|
|
3
3
|
* @param max 最大值
|
|
4
4
|
* @param num 保留小数位数
|
|
5
5
|
* @example
|
|
6
|
-
* libRandom(1, 10); //
|
|
7
|
-
* libRandom(1, 10, 2); //
|
|
8
|
-
* libRandom(5, 5, 3); // 返回 5.000
|
|
6
|
+
* libRandom(1, 10); //1 到 10 之间的随机整数
|
|
7
|
+
* libRandom(1, 10, 2); //1 到 10 之间保留两位小数的随机数
|
|
9
8
|
*/
|
|
10
9
|
export declare const libRandom: (min: number, max: number, num?: number) => number;
|