lyb-js 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/README.md +507 -9
  2. package/package.json +1 -1
  3. package/dist/Base/LibGetDataType/index.d.ts +0 -10
  4. package/dist/Base/LibGetDataType/index.js +0 -15
  5. package/dist/Base/LibJsGetDataType/index.d.ts +0 -7
  6. package/dist/Base/LibJsGetDataType/index.js +0 -11
  7. package/dist/Base/LibPromiseTimeout/index.d.ts +0 -10
  8. package/dist/Base/LibPromiseTimeout/index.js +0 -40
  9. package/dist/Browser/LibColorConsole/index.d.ts +0 -15
  10. package/dist/Browser/LibColorConsole/index.js +0 -38
  11. package/dist/Browser/LibIsMobile/index.d.ts +0 -6
  12. package/dist/Browser/LibIsMobile/index.js +0 -9
  13. package/dist/Browser/LibIsPad/index.d.ts +0 -6
  14. package/dist/Browser/LibIsPad/index.js +0 -15
  15. package/dist/Browser/LibPathParams/index.d.ts +0 -6
  16. package/dist/Browser/LibPathParams/index.js +0 -16
  17. package/dist/Browser/LibSetTitleIcon/index.d.ts +0 -7
  18. package/dist/Browser/LibSetTitleIcon/index.js +0 -14
  19. package/dist/Browser/LibTagTitleTip/index.d.ts +0 -7
  20. package/dist/Browser/LibTagTitleTip/index.js +0 -24
  21. package/dist/Data/LibChunkArray/index.d.ts +0 -9
  22. package/dist/Data/LibChunkArray/index.js +0 -15
  23. package/dist/Data/LibDeepJSONParse/index.d.ts +0 -6
  24. package/dist/Data/LibDeepJSONParse/index.js +0 -32
  25. package/dist/Data/LibGroupArrayByKey/index.d.ts +0 -10
  26. package/dist/Data/LibGroupArrayByKey/index.js +0 -20
  27. package/dist/Data/LibMatchEmail/index.d.ts +0 -10
  28. package/dist/Data/LibMatchEmail/index.js +0 -16
  29. package/dist/Data/LibSearch/index.d.ts +0 -8
  30. package/dist/Data/LibSearch/index.js +0 -45
  31. package/dist/Data/LibShuffleArray/index.d.ts +0 -7
  32. package/dist/Data/LibShuffleArray/index.js +0 -14
  33. package/dist/Data/LibStepArray/index.d.ts +0 -8
  34. package/dist/Data/LibStepArray/index.js +0 -14
  35. package/dist/File/LibDownloadImageLink/index.d.ts +0 -7
  36. package/dist/File/LibDownloadImageLink/index.js +0 -17
  37. package/dist/File/LibFileSize/index.d.ts +0 -5
  38. package/dist/File/LibFileSize/index.js +0 -11
  39. package/dist/File/LibImageOptimizer/index.d.ts +0 -32
  40. package/dist/File/LibImageOptimizer/index.js +0 -83
  41. package/dist/File/LibImageOptimizerOptions/index.d.ts +0 -18
  42. package/dist/File/LibImageOptimizerOptions/index.js +0 -69
  43. package/dist/File/LibSaveExcel/index.d.ts +0 -11
  44. package/dist/File/LibSaveExcel/index.js +0 -17
  45. package/dist/File/LibSaveJson/index.d.ts +0 -8
  46. package/dist/File/LibSaveJson/index.js +0 -16
  47. package/dist/Formatter/LibFormatterByte/index.d.ts +0 -9
  48. package/dist/Formatter/LibFormatterByte/index.js +0 -17
  49. package/dist/Formatter/LibMaskPhoneNumber/index.d.ts +0 -8
  50. package/dist/Formatter/LibMaskPhoneNumber/index.js +0 -11
  51. package/dist/Formatter/LibNumComma/index.d.ts +0 -9
  52. package/dist/Formatter/LibNumComma/index.js +0 -13
  53. package/dist/Formatter/LibNumberUnit/index.d.ts +0 -12
  54. package/dist/Formatter/LibNumberUnit/index.js +0 -23
  55. package/dist/Math/LibCalculateExpression/index.d.ts +0 -10
  56. package/dist/Math/LibCalculateExpression/index.js +0 -108
  57. package/dist/Math/LibConvertAngle/index.d.ts +0 -12
  58. package/dist/Math/LibConvertAngle/index.js +0 -22
  59. package/dist/Math/LibCoordsAngle/index.d.ts +0 -15
  60. package/dist/Math/LibCoordsAngle/index.js +0 -24
  61. package/dist/Math/LibCoordsDistance/index.d.ts +0 -15
  62. package/dist/Math/LibCoordsDistance/index.js +0 -16
  63. package/dist/Math/LibDecimal/index.d.ts +0 -11
  64. package/dist/Math/LibDecimal/index.js +0 -26
  65. package/dist/Misc/LibRegFormValidate/index.d.ts +0 -32
  66. package/dist/Misc/LibRegFormValidate/index.js +0 -34
  67. package/dist/Misc/LibRetryRequest/index.d.ts +0 -24
  68. package/dist/Misc/LibRetryRequest/index.js +0 -37
  69. package/dist/Random/LibProbabilityResult/index.d.ts +0 -8
  70. package/dist/Random/LibProbabilityResult/index.js +0 -8
  71. package/dist/Random/LibRandom/index.d.ts +0 -10
  72. package/dist/Random/LibRandom/index.js +0 -12
  73. package/dist/Random/LibRandomColor/index.d.ts +0 -7
  74. package/dist/Random/LibRandomColor/index.js +0 -12
  75. package/dist/Random/LibUniqueRandomNumbers/index.d.ts +0 -9
  76. package/dist/Random/LibUniqueRandomNumbers/index.js +0 -16
  77. package/dist/Time/LibSameTimeCheck/index.d.ts +0 -8
  78. package/dist/Time/LibSameTimeCheck/index.js +0 -20
  79. package/dist/Time/LibSecondsFormatterChinese/index.d.ts +0 -11
  80. package/dist/Time/LibSecondsFormatterChinese/index.js +0 -42
  81. package/dist/Time/LibTimeAgo/index.d.ts +0 -9
  82. package/dist/Time/LibTimeAgo/index.js +0 -27
  83. package/dist/Time/LibTimeGreeting/index.d.ts +0 -17
  84. package/dist/Time/LibTimeGreeting/index.js +0 -23
  85. package/dist/index.d.ts +0 -1
  86. package/dist/index.js +0 -1
  87. package/dist/lib.d.ts +0 -395
  88. package/dist/lib.js +0 -405
  89. package/test/index.js +0 -0
@@ -1,16 +0,0 @@
1
- /**
2
- * @description 保存文件到本地
3
- * @param data 要保存的数据
4
- * @param name 文件名
5
- * @example
6
- * libSaveJson(JSON.stringify({ key: "value" }), "example.json");
7
- */
8
- export const libSaveJson = (data, name) => {
9
- const urlObject = window.URL || window.webkitURL;
10
- const exportBlob = new Blob([data]);
11
- const saveLink = document.createElement("a");
12
- saveLink.href = urlObject.createObjectURL(exportBlob);
13
- saveLink.download = name;
14
- saveLink.click();
15
- urlObject.revokeObjectURL(saveLink.href);
16
- };
@@ -1,9 +0,0 @@
1
- /**
2
- * @description 格式化字节大小
3
- * @param bytes 字节数
4
- * @returns ['大小', '单位', '大小及单位']
5
- * @example
6
- * const [size, unit, formatted] = libFormatterByte(2048);
7
- * console.log(size, unit, formatted); // 2.00 KB 2.00 KB
8
- */
9
- export declare const libFormatterByte: (bytes: number) => (string | number)[];
@@ -1,17 +0,0 @@
1
- /**
2
- * @description 格式化字节大小
3
- * @param bytes 字节数
4
- * @returns ['大小', '单位', '大小及单位']
5
- * @example
6
- * const [size, unit, formatted] = libFormatterByte(2048);
7
- * console.log(size, unit, formatted); // 2.00 KB 2.00 KB
8
- */
9
- export const libFormatterByte = (bytes) => {
10
- if (bytes <= 0)
11
- return [0, "B", "0 B"];
12
- const k = 1024;
13
- const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
14
- const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1);
15
- const size = (bytes / k ** i).toFixed(2);
16
- return [size, sizes[i], `${size} ${sizes[i]}`];
17
- };
@@ -1,8 +0,0 @@
1
- /**
2
- * @description 隐藏手机号码中间的四位数字。
3
- * @param mobile 需要处理的手机号码。
4
- * @example
5
- * const masked = libMaskPhoneNumber("13812345678");
6
- * console.log(masked); // 138****5678
7
- */
8
- export declare const libMaskPhoneNumber: (mobile: number | string) => string;
@@ -1,11 +0,0 @@
1
- /**
2
- * @description 隐藏手机号码中间的四位数字。
3
- * @param mobile 需要处理的手机号码。
4
- * @example
5
- * const masked = libMaskPhoneNumber("13812345678");
6
- * console.log(masked); // 138****5678
7
- */
8
- export const libMaskPhoneNumber = (mobile) => {
9
- const m = mobile.toString();
10
- return m.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
11
- };
@@ -1,9 +0,0 @@
1
- /**
2
- * @description 数字每三位添加逗号
3
- * @param num 需要格式化的数字
4
- * @param reserve 保留小数位数
5
- * @example
6
- * const formatted = libNumComma(1234567.89);
7
- * console.log(formatted); // 1,234,567.89
8
- */
9
- export declare const libNumComma: (num: number, reserve?: number) => string;
@@ -1,13 +0,0 @@
1
- /**
2
- * @description 数字每三位添加逗号
3
- * @param num 需要格式化的数字
4
- * @param reserve 保留小数位数
5
- * @example
6
- * const formatted = libNumComma(1234567.89);
7
- * console.log(formatted); // 1,234,567.89
8
- */
9
- export const libNumComma = (num, reserve = 2) => {
10
- const str = num.toFixed(reserve).toString();
11
- const reg = str.indexOf(".") > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g;
12
- return str.replace(reg, "$1,");
13
- };
@@ -1,12 +0,0 @@
1
- export interface LibNumberUnitParams {
2
- [unit: string]: number;
3
- }
4
- /** @description 将大于1000的数字使用k为单位
5
- * @param num 数字
6
- * @param units 单位组,key为单位,value为格式化阈值
7
- * @returns [数字, 单位]
8
- * @example
9
- * const [value, unit] = libNumberUnit(1500, { K: 1000, M: 1000000 });
10
- * console.log(value, unit); // 1.50 K
11
- */
12
- export declare const libNumberUnit: (num: number, units: LibNumberUnitParams) => string[];
@@ -1,23 +0,0 @@
1
- import Decimal from "decimal.js";
2
- /** @description 将大于1000的数字使用k为单位
3
- * @param num 数字
4
- * @param units 单位组,key为单位,value为格式化阈值
5
- * @returns [数字, 单位]
6
- * @example
7
- * const [value, unit] = libNumberUnit(1500, { K: 1000, M: 1000000 });
8
- * console.log(value, unit); // 1.50 K
9
- */
10
- export const libNumberUnit = (num, units) => {
11
- const decimalValue = new Decimal(num);
12
- const sortedUnits = Object.entries(units).sort(([, a], [, b]) => b - a);
13
- for (const [unit, threshold] of sortedUnits) {
14
- const decimalThreshold = new Decimal(threshold);
15
- if (decimalValue.greaterThanOrEqualTo(decimalThreshold)) {
16
- const formattedValue = decimalValue
17
- .dividedBy(decimalThreshold)
18
- .toFixed(2);
19
- return [formattedValue, unit];
20
- }
21
- }
22
- return [decimalValue.toFixed(2), ""];
23
- };
@@ -1,10 +0,0 @@
1
- /** @description 计算表达式字符串
2
- * @param expression 表达式字符串
3
- * @param point 小数点精度
4
- * @returns 计算结果
5
- *
6
- * @example
7
- * const result = libCalculateExpression("(1+2)-(3*4)/5");
8
- * console.log(result); // 0.6
9
- */
10
- export declare const libCalculateExpression: (expression: string, point?: number) => number;
@@ -1,108 +0,0 @@
1
- import Decimal from 'decimal.js';
2
- /** @description 计算表达式字符串
3
- * @param expression 表达式字符串
4
- * @param point 小数点精度
5
- * @returns 计算结果
6
- *
7
- * @example
8
- * const result = libCalculateExpression("(1+2)-(3*4)/5");
9
- * console.log(result); // 0.6
10
- */
11
- export const libCalculateExpression = (expression, point = 2) => {
12
- // 清除所有空格
13
- expression = expression.replace(/\s+/g, '');
14
- // 支持的运算符和优先级
15
- const operators = {
16
- "+": 1,
17
- "-": 1,
18
- "*": 2,
19
- "/": 2,
20
- };
21
- // 支持的小数点精度
22
- const toDecimal = (value) => new Decimal(value);
23
- // 判断字符是否是运算符
24
- const isOperator = (char) => ['+', '-', '*', '/'].includes(char);
25
- // 判断字符是否是数字(包括小数点)
26
- const isNumber = (char) => /[0-9.]/.test(char);
27
- // 解析表达式并计算
28
- const evaluate = (expression) => {
29
- const outputQueue = []; // 输出队列
30
- const operatorStack = []; // 操作符栈
31
- let i = 0;
32
- while (i < expression.length) {
33
- const char = expression[i];
34
- if (isNumber(char)) {
35
- let numStr = '';
36
- // 处理多位数字(支持小数)
37
- while (i < expression.length && isNumber(expression[i])) {
38
- numStr += expression[i];
39
- i++;
40
- }
41
- outputQueue.push(toDecimal(numStr));
42
- }
43
- else if (char === '(') {
44
- operatorStack.push(char);
45
- i++;
46
- }
47
- else if (char === ')') {
48
- // 处理右括号,直到遇到左括号
49
- while (operatorStack.length > 0 && operatorStack[operatorStack.length - 1] !== '(') {
50
- outputQueue.push(operatorStack.pop());
51
- }
52
- operatorStack.pop(); // 弹出左括号
53
- i++;
54
- }
55
- else if (isOperator(char)) {
56
- // 运算符
57
- while (operatorStack.length > 0 && operators[operatorStack[operatorStack.length - 1]] >= operators[char]) {
58
- outputQueue.push(operatorStack.pop());
59
- }
60
- operatorStack.push(char);
61
- i++;
62
- }
63
- else {
64
- throw new Error(`无效字符: ${char}`);
65
- }
66
- }
67
- // 把所有剩余的操作符添加到输出队列
68
- while (operatorStack.length > 0) {
69
- outputQueue.push(operatorStack.pop());
70
- }
71
- // 执行运算
72
- const calcStack = [];
73
- for (let token of outputQueue) {
74
- if (typeof token === 'string') {
75
- const b = calcStack.pop();
76
- const a = calcStack.pop();
77
- switch (token) {
78
- case '+':
79
- calcStack.push(a.add(b));
80
- break;
81
- case '-':
82
- calcStack.push(a.sub(b));
83
- break;
84
- case '*':
85
- calcStack.push(a.mul(b));
86
- break;
87
- case '/':
88
- if (b.eq(0))
89
- throw new Error("除数不能为零");
90
- calcStack.push(a.div(b));
91
- break;
92
- }
93
- }
94
- else {
95
- calcStack.push(token);
96
- }
97
- }
98
- return calcStack.pop();
99
- };
100
- try {
101
- // 调用计算器并返回结果
102
- const result = evaluate(expression);
103
- return Number(result.toFixed(point)); // 保留指定的小数位数
104
- }
105
- catch (error) {
106
- throw new Error("表达式计算失败:" + error.message);
107
- }
108
- };
@@ -1,12 +0,0 @@
1
- /**
2
- * @description 角度和弧度互相转换
3
- * @param value 角度值或弧度值
4
- * @param type 角度类型或弧度类型
5
- * @example
6
- * // 角度转弧度
7
- * libConvertAngle(90, "rad"); // 返回 1.5708... (π/2)
8
- *
9
- * // 弧度转角度
10
- * libConvertAngle(Math.PI, "deg"); // 返回 180
11
- */
12
- export declare const libConvertAngle: (value: number, type: "rad" | "deg") => number;
@@ -1,22 +0,0 @@
1
- /**
2
- * @description 角度和弧度互相转换
3
- * @param value 角度值或弧度值
4
- * @param type 角度类型或弧度类型
5
- * @example
6
- * // 角度转弧度
7
- * libConvertAngle(90, "rad"); // 返回 1.5708... (π/2)
8
- *
9
- * // 弧度转角度
10
- * libConvertAngle(Math.PI, "deg"); // 返回 180
11
- */
12
- export const libConvertAngle = (value, type) => {
13
- if (type === "rad") {
14
- return value * (Math.PI / 180);
15
- }
16
- else if (type === "deg") {
17
- return value * (180 / Math.PI);
18
- }
19
- else {
20
- throw new Error("请使用正确类型");
21
- }
22
- };
@@ -1,15 +0,0 @@
1
- /** @description 计算两点角度
2
- * @param coord1 起点坐标
3
- * @param coord2 终点坐标
4
- * @example
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
- */
9
- export declare const libCoordsAngle: (coord1: {
10
- x: number;
11
- y: number;
12
- }, coord2: {
13
- x: number;
14
- y: number;
15
- }) => number;
@@ -1,24 +0,0 @@
1
- /** @description 计算两点角度
2
- * @param coord1 起点坐标
3
- * @param coord2 终点坐标
4
- * @example
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
- */
9
- export const libCoordsAngle = (coord1, coord2) => {
10
- // 计算相对于第一个坐标的水平和垂直距离
11
- const deltaX = coord2.x - coord1.x;
12
- const deltaY = coord2.y - coord1.y;
13
- // 使用反三角函数计算角度(以弧度为单位)
14
- const angleRad = Math.atan2(deltaY, deltaX);
15
- // 将弧度转换为角度
16
- let angleDeg = angleRad * (180 / Math.PI);
17
- // 将角度转换为顺时针方向为正方向的角度
18
- angleDeg = -angleDeg + 90;
19
- // 调整角度使得右边成为 360 度的位置变为 0 度
20
- if (angleDeg < 0) {
21
- angleDeg += 360;
22
- }
23
- return angleDeg;
24
- };
@@ -1,15 +0,0 @@
1
- /** @description 计算两点距离
2
- * @param coord1 起点坐标
3
- * @param coord2 终点坐标
4
- * @example
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
- */
9
- export declare const libCoordsDistance: (coord1: {
10
- x: number;
11
- y: number;
12
- }, coord2: {
13
- x: number;
14
- y: number;
15
- }) => number;
@@ -1,16 +0,0 @@
1
- /** @description 计算两点距离
2
- * @param coord1 起点坐标
3
- * @param coord2 终点坐标
4
- * @example
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
- */
9
- export const libCoordsDistance = (coord1, coord2) => {
10
- // 计算两个坐标之间的水平和垂直距离
11
- const deltaX = coord2.x - coord1.x;
12
- const deltaY = coord2.y - coord1.y;
13
- // 使用勾股定理计算两点之间的距离
14
- const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
15
- return distance;
16
- };
@@ -1,11 +0,0 @@
1
- /** @description 计算两个数的运算结果,并保留指定位数的小数
2
- * @param num1 第一个数
3
- * @param num2 第二个数
4
- * @param operator 运算符,支持加减乘除
5
- * @example
6
- * // 示例用法
7
- * libDecimal(10, 3, "+"); // 13
8
- * libDecimal(10, 3, "-"); // 7
9
- * libDecimal(10, 3, "/", 2); // 3.33
10
- */
11
- export declare const libDecimal: (num1: number, num2: number, operator: "+" | "-" | "*" | "/", point?: number) => number;
@@ -1,26 +0,0 @@
1
- import { Decimal } from "decimal.js";
2
- /** @description 计算两个数的运算结果,并保留指定位数的小数
3
- * @param num1 第一个数
4
- * @param num2 第二个数
5
- * @param operator 运算符,支持加减乘除
6
- * @example
7
- * // 示例用法
8
- * libDecimal(10, 3, "+"); // 13
9
- * libDecimal(10, 3, "-"); // 7
10
- * libDecimal(10, 3, "/", 2); // 3.33
11
- */
12
- export const libDecimal = (num1, num2, operator, point = 2) => {
13
- const calc = {
14
- "+": (a, b) => a.add(b),
15
- "-": (a, b) => a.sub(b),
16
- "*": (a, b) => a.mul(b),
17
- "/": (a, b) => {
18
- if (b.eq(0)) {
19
- throw new Error("除数不能为0");
20
- }
21
- return a.div(b);
22
- },
23
- };
24
- const result = calc[operator](new Decimal(num1), new Decimal(num2));
25
- return Number(result.toFixed(point));
26
- };
@@ -1,32 +0,0 @@
1
- export type ValidationResult = {
2
- key: string;
3
- msg: string;
4
- name: string;
5
- };
6
- /**
7
- * @description 表单验证函数。
8
- * @param form 表单数据对象。
9
- * @param rules 验证规则数组。
10
- * @returns 验证结果数组,包含未通过验证的项。
11
- * @example
12
- * const form = { username: "john", email: "john@example.com" };
13
- * const rules = [
14
- * { key: "username", verify: /^[a-zA-Z0-9]{3,}$/, msg: "用户名不合法", name: "用户名" },
15
- * { key: "email", verify: /^\S+@\S+\.\S+$/, msg: "邮箱格式不正确", name: "邮箱" },
16
- * ];
17
- * libRegFormValidate(form, rules);
18
- * // 返回结果: []
19
- *
20
- * const invalidForm = { username: "jo", email: "invalid-email" };
21
- * libRegFormValidate(invalidForm, rules);
22
- * // 返回结果: [
23
- * // { key: "username", msg: "用户名不合法", name: "用户名" },
24
- * // { key: "email", msg: "邮箱格式不正确", name: "邮箱" }
25
- * // ]
26
- */
27
- export declare const libRegFormValidate: (form: Record<string, any>, rules: Array<{
28
- key: string;
29
- verify: RegExp | ((value: any) => boolean);
30
- msg: string;
31
- name: string;
32
- }>) => ValidationResult[];
@@ -1,34 +0,0 @@
1
- /**
2
- * @description 表单验证函数。
3
- * @param form 表单数据对象。
4
- * @param rules 验证规则数组。
5
- * @returns 验证结果数组,包含未通过验证的项。
6
- * @example
7
- * const form = { username: "john", email: "john@example.com" };
8
- * const rules = [
9
- * { key: "username", verify: /^[a-zA-Z0-9]{3,}$/, msg: "用户名不合法", name: "用户名" },
10
- * { key: "email", verify: /^\S+@\S+\.\S+$/, msg: "邮箱格式不正确", name: "邮箱" },
11
- * ];
12
- * libRegFormValidate(form, rules);
13
- * // 返回结果: []
14
- *
15
- * const invalidForm = { username: "jo", email: "invalid-email" };
16
- * libRegFormValidate(invalidForm, rules);
17
- * // 返回结果: [
18
- * // { key: "username", msg: "用户名不合法", name: "用户名" },
19
- * // { key: "email", msg: "邮箱格式不正确", name: "邮箱" }
20
- * // ]
21
- */
22
- export const libRegFormValidate = (form, rules) => {
23
- return rules.reduce((result, rule) => {
24
- const { key, verify, msg, name } = rule;
25
- const value = form[key];
26
- if (value === "" || value === undefined || value === null) {
27
- result.push({ key, msg: "必填项", name });
28
- }
29
- else if (typeof verify === "function" ? !verify(value) : !verify.test(value)) {
30
- result.push({ key, msg, name });
31
- }
32
- return result;
33
- }, []);
34
- };
@@ -1,24 +0,0 @@
1
- /** @description 请求失败重连
2
- * @param promiseFn 请求函数
3
- * @param maxRetries 最大重试次数
4
- * @param retryDelay 重试间隔时间
5
- * @param params 请求参数
6
- * @example
7
- * const requestFn = (params: { url: string }) => fetch(params.url).then(res => res.json());
8
- * const params = { url: "https://api.example.com/data" };
9
- * libRetryRequest({
10
- * promiseFn: requestFn,
11
- * params,
12
- * maxRetries: 5,
13
- * retryDelay: 1000
14
- * })
15
- * .then(data => console.log(data))
16
- * .catch(err => console.error(err));
17
- */
18
- export declare const libRetryRequest: <T>({ promiseFn, maxRetries, retryDelay, params, }: {
19
- promiseFn: (params?: any) => Promise<T>;
20
- params?: any;
21
- maxRetries?: number;
22
- retryDelay?: number;
23
- onRetry?: () => void;
24
- }) => Promise<T>;
@@ -1,37 +0,0 @@
1
- /** @description 请求失败重连
2
- * @param promiseFn 请求函数
3
- * @param maxRetries 最大重试次数
4
- * @param retryDelay 重试间隔时间
5
- * @param params 请求参数
6
- * @example
7
- * const requestFn = (params: { url: string }) => fetch(params.url).then(res => res.json());
8
- * const params = { url: "https://api.example.com/data" };
9
- * libRetryRequest({
10
- * promiseFn: requestFn,
11
- * params,
12
- * maxRetries: 5,
13
- * retryDelay: 1000
14
- * })
15
- * .then(data => console.log(data))
16
- * .catch(err => console.error(err));
17
- */
18
- export const libRetryRequest = ({ promiseFn, maxRetries = 3, retryDelay = 2000, params = undefined, }) => {
19
- return new Promise((resolve, reject) => {
20
- let count = 0;
21
- const makeRequest = () => {
22
- promiseFn(params)
23
- .then((res) => {
24
- resolve(res);
25
- })
26
- .catch((err) => {
27
- count++;
28
- if (count >= maxRetries) {
29
- reject(err);
30
- return;
31
- }
32
- setTimeout(makeRequest, retryDelay);
33
- });
34
- };
35
- makeRequest();
36
- });
37
- };
@@ -1,8 +0,0 @@
1
- /** @description 百分比概率结果
2
- * @param probability 触发概率,百分比,0-100
3
- * @example
4
- * libProbabilityResult(50); // 50% 概率为 true
5
- * libProbabilityResult(80); // 80% 概率为 true
6
- * libProbabilityResult(100); // 100% 概率为 true
7
- */
8
- export declare const libProbabilityResult: (probability: number) => boolean;
@@ -1,8 +0,0 @@
1
- /** @description 百分比概率结果
2
- * @param probability 触发概率,百分比,0-100
3
- * @example
4
- * libProbabilityResult(50); // 50% 概率为 true
5
- * libProbabilityResult(80); // 80% 概率为 true
6
- * libProbabilityResult(100); // 100% 概率为 true
7
- */
8
- export const libProbabilityResult = (probability) => Math.random() * 100 < probability;
@@ -1,10 +0,0 @@
1
- /** @description 随机数
2
- * @param min 最小值
3
- * @param max 最大值
4
- * @param num 保留小数位数
5
- * @example
6
- * libRandom(1, 10); // 1 到 10 之间的随机整数
7
- * libRandom(1, 10, 2); // 1 到 10 之间保留两位小数的随机数
8
- * libRandom(5, 5, 3); // 返回 5.000
9
- */
10
- export declare const libRandom: (min: number, max: number, num?: number) => number;
@@ -1,12 +0,0 @@
1
- /** @description 随机数
2
- * @param min 最小值
3
- * @param max 最大值
4
- * @param num 保留小数位数
5
- * @example
6
- * libRandom(1, 10); // 1 到 10 之间的随机整数
7
- * libRandom(1, 10, 2); // 1 到 10 之间保留两位小数的随机数
8
- * libRandom(5, 5, 3); // 返回 5.000
9
- */
10
- export const libRandom = (min, max, num = 0) => {
11
- return parseFloat((Math.random() * (max - min) + min).toFixed(num));
12
- };
@@ -1,7 +0,0 @@
1
- /** @description 随机 RGBA 颜色
2
- * @param alpha 透明度
3
- * @example
4
- * libRandomColor(); // 生成随机的 RGBA 颜色,默认透明度 1
5
- * libRandomColor(0.5); // 生成随机的 RGBA 颜色,透明度为 0.5
6
- */
7
- export declare const libRandomColor: (alpha?: number) => string;
@@ -1,12 +0,0 @@
1
- /** @description 随机 RGBA 颜色
2
- * @param alpha 透明度
3
- * @example
4
- * libRandomColor(); // 生成随机的 RGBA 颜色,默认透明度 1
5
- * libRandomColor(0.5); // 生成随机的 RGBA 颜色,透明度为 0.5
6
- */
7
- export const libRandomColor = (alpha = 1) => {
8
- const r = Math.floor(Math.random() * 256);
9
- const g = Math.floor(Math.random() * 256);
10
- const b = Math.floor(Math.random() * 256);
11
- return `rgba(${r}, ${g}, ${b}, ${alpha})`;
12
- };
@@ -1,9 +0,0 @@
1
- /** @description 随机生成n个指定范围的随机数数组
2
- * @param min 最小值
3
- * @param max 最大值
4
- * @param count 数组长度
5
- * @example
6
- * libUniqueRandomNumbers(1, 10, 5); // 从 1 到 10 中随机生成 5 个唯一数字
7
- * libUniqueRandomNumbers(1, 100, 10); // 从 1 到 100 中随机生成 10 个唯一数字
8
- */
9
- export declare const libUniqueRandomNumbers: (min: number, max: number, count: number) => number[];
@@ -1,16 +0,0 @@
1
- /** @description 随机生成n个指定范围的随机数数组
2
- * @param min 最小值
3
- * @param max 最大值
4
- * @param count 数组长度
5
- * @example
6
- * libUniqueRandomNumbers(1, 10, 5); // 从 1 到 10 中随机生成 5 个唯一数字
7
- * libUniqueRandomNumbers(1, 100, 10); // 从 1 到 100 中随机生成 10 个唯一数字
8
- */
9
- export const libUniqueRandomNumbers = (min, max, count) => {
10
- const numbers = Array.from({ length: max - min + 1 }, (_, i) => i + min);
11
- for (let i = numbers.length - 1; i > 0; i--) {
12
- const j = Math.floor(Math.random() * (i + 1));
13
- [numbers[i], numbers[j]] = [numbers[j], numbers[i]];
14
- }
15
- return numbers.slice(0, count);
16
- };
@@ -1,8 +0,0 @@
1
- import dayjs from "dayjs";
2
- /**
3
- * @description 传入时间戳与当前时间判断是否为同一天或同一周
4
- * @param timestamp 毫秒时间戳
5
- * @param unit 判断单位
6
- * @returns 0-同一单位时间 1-新单位时间 -1时间戳大于当前时间
7
- */
8
- export declare const libSameTimeCheck: (timestamp: number, unit: dayjs.OpUnitType) => 0 | 1 | -1;