lyb-js 1.0.6 → 1.0.8

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 (152) hide show
  1. package/README.md +143 -144
  2. package/dist/Base/LibGetDataType/index.d.ts +10 -0
  3. package/dist/Base/LibGetDataType/index.js +15 -0
  4. package/dist/Base/LibJsGetDataType/index.d.ts +10 -0
  5. package/dist/Base/LibJsGetDataType/index.js +15 -0
  6. package/dist/Base/LibJsPromiseTimeout/index.d.ts +10 -0
  7. package/dist/Base/LibJsPromiseTimeout/index.js +40 -0
  8. package/dist/Base/LibPromiseTimeout/index.d.ts +10 -0
  9. package/dist/Base/LibPromiseTimeout/index.js +40 -0
  10. package/dist/Browser/LibColorConsole/index.d.ts +15 -0
  11. package/dist/Browser/LibColorConsole/index.js +38 -0
  12. package/dist/Browser/LibIsMobile/index.d.ts +6 -0
  13. package/dist/Browser/LibIsMobile/index.js +9 -0
  14. package/dist/Browser/LibIsPad/index.d.ts +6 -0
  15. package/dist/Browser/LibIsPad/index.js +15 -0
  16. package/dist/Browser/LibJsColorConsole/index.d.ts +15 -0
  17. package/dist/Browser/LibJsColorConsole/index.js +38 -0
  18. package/dist/Browser/LibJsIsMobile/index.d.ts +6 -0
  19. package/dist/Browser/LibJsIsMobile/index.js +9 -0
  20. package/dist/Browser/LibJsIsPad/index.d.ts +6 -0
  21. package/dist/Browser/LibJsIsPad/index.js +15 -0
  22. package/dist/Browser/LibJsPathParams/index.d.ts +6 -0
  23. package/dist/Browser/LibJsPathParams/index.js +16 -0
  24. package/dist/Browser/LibJsSetTitleIcon/index.d.ts +7 -0
  25. package/dist/Browser/LibJsSetTitleIcon/index.js +14 -0
  26. package/dist/Browser/LibJsTagTitleTip/index.d.ts +7 -0
  27. package/dist/Browser/LibJsTagTitleTip/index.js +24 -0
  28. package/dist/Browser/LibPathParams/index.d.ts +6 -0
  29. package/dist/Browser/LibPathParams/index.js +16 -0
  30. package/dist/Browser/LibSetTitleIcon/index.d.ts +7 -0
  31. package/dist/Browser/LibSetTitleIcon/index.js +14 -0
  32. package/dist/Browser/LibTagTitleTip/index.d.ts +7 -0
  33. package/dist/Browser/LibTagTitleTip/index.js +24 -0
  34. package/dist/Data/LibChunkArray/index.d.ts +9 -0
  35. package/dist/Data/LibChunkArray/index.js +15 -0
  36. package/dist/Data/LibDeepJSONParse/index.d.ts +6 -0
  37. package/dist/Data/LibDeepJSONParse/index.js +32 -0
  38. package/dist/Data/LibGroupArrayByKey/index.d.ts +10 -0
  39. package/dist/Data/LibGroupArrayByKey/index.js +20 -0
  40. package/dist/Data/LibJsChunkArray/index.d.ts +9 -0
  41. package/dist/Data/LibJsChunkArray/index.js +15 -0
  42. package/dist/Data/LibJsDeepJSONParse/index.d.ts +6 -0
  43. package/dist/Data/LibJsDeepJSONParse/index.js +32 -0
  44. package/dist/Data/LibJsGroupArrayByKey/index.d.ts +10 -0
  45. package/dist/Data/LibJsGroupArrayByKey/index.js +20 -0
  46. package/dist/Data/LibJsMatchEmail/index.d.ts +10 -0
  47. package/dist/Data/LibJsMatchEmail/index.js +16 -0
  48. package/dist/Data/LibJsShuffleArray/index.d.ts +7 -0
  49. package/dist/Data/LibJsShuffleArray/index.js +14 -0
  50. package/dist/Data/LibJsStepArray/index.d.ts +8 -0
  51. package/dist/Data/LibJsStepArray/index.js +14 -0
  52. package/dist/Data/LibMatchEmail/index.d.ts +10 -0
  53. package/dist/Data/LibMatchEmail/index.js +16 -0
  54. package/dist/Data/LibShuffleArray/index.d.ts +7 -0
  55. package/dist/Data/LibShuffleArray/index.js +14 -0
  56. package/dist/Data/LibStepArray/index.d.ts +8 -0
  57. package/dist/Data/LibStepArray/index.js +14 -0
  58. package/dist/File/LibDownloadImageLink/index.d.ts +7 -0
  59. package/dist/File/LibDownloadImageLink/index.js +17 -0
  60. package/dist/File/LibImageOptimizer/index.d.ts +32 -0
  61. package/dist/File/LibImageOptimizer/index.js +83 -0
  62. package/dist/File/LibJsDownloadImageLink/index.d.ts +7 -0
  63. package/dist/File/LibJsDownloadImageLink/index.js +17 -0
  64. package/dist/File/LibJsImageOptimizer/index.d.ts +31 -0
  65. package/dist/File/LibJsImageOptimizer/index.js +82 -0
  66. package/dist/File/LibJsSaveJson/index.d.ts +9 -0
  67. package/dist/File/LibJsSaveJson/index.js +17 -0
  68. package/dist/File/LibSaveJson/index.d.ts +9 -0
  69. package/dist/File/LibSaveJson/index.js +17 -0
  70. package/dist/Formatter/LibFormatterByte/index.d.ts +9 -0
  71. package/dist/Formatter/LibFormatterByte/index.js +17 -0
  72. package/dist/Formatter/LibJsFormatterByte/index.d.ts +9 -0
  73. package/dist/Formatter/LibJsFormatterByte/index.js +17 -0
  74. package/dist/Formatter/LibJsMaskPhoneNumber/index.d.ts +8 -0
  75. package/dist/Formatter/LibJsMaskPhoneNumber/index.js +11 -0
  76. package/dist/Formatter/LibJsNumComma/index.d.ts +9 -0
  77. package/dist/Formatter/LibJsNumComma/index.js +13 -0
  78. package/dist/Formatter/LibJsNumberUnit/index.d.ts +12 -0
  79. package/dist/Formatter/LibJsNumberUnit/index.js +23 -0
  80. package/dist/Formatter/LibJsSecondsFormatterChinese/index.d.ts +11 -0
  81. package/dist/Formatter/LibJsSecondsFormatterChinese/index.js +42 -0
  82. package/dist/Formatter/LibMaskPhoneNumber/index.d.ts +8 -0
  83. package/dist/Formatter/LibMaskPhoneNumber/index.js +11 -0
  84. package/dist/Formatter/LibNumComma/index.d.ts +9 -0
  85. package/dist/Formatter/LibNumComma/index.js +13 -0
  86. package/dist/Formatter/LibNumberUnit/index.d.ts +12 -0
  87. package/dist/Formatter/LibNumberUnit/index.js +23 -0
  88. package/dist/Formatter/LibSecondsFormatterChinese/index.d.ts +11 -0
  89. package/dist/Formatter/LibSecondsFormatterChinese/index.js +42 -0
  90. package/dist/Math/LibCalculateExpression/index.d.ts +9 -0
  91. package/dist/Math/LibCalculateExpression/index.js +107 -0
  92. package/dist/Math/LibConvertAngle/index.d.ts +12 -0
  93. package/dist/Math/LibConvertAngle/index.js +22 -0
  94. package/dist/Math/LibCoordsAngle/index.d.ts +15 -0
  95. package/dist/Math/LibCoordsAngle/index.js +24 -0
  96. package/dist/Math/LibCoordsDistance/index.d.ts +15 -0
  97. package/dist/Math/LibCoordsDistance/index.js +16 -0
  98. package/dist/Math/LibDecimal/index.d.ts +10 -0
  99. package/dist/Math/LibDecimal/index.js +25 -0
  100. package/dist/Math/LibJsCalculateExpression/index.d.ts +9 -0
  101. package/dist/Math/LibJsCalculateExpression/index.js +107 -0
  102. package/dist/Math/LibJsConvertAngle/index.d.ts +12 -0
  103. package/dist/Math/LibJsConvertAngle/index.js +22 -0
  104. package/dist/Math/LibJsCoordsAngle/index.d.ts +15 -0
  105. package/dist/Math/LibJsCoordsAngle/index.js +24 -0
  106. package/dist/Math/LibJsCoordsDistance/index.d.ts +15 -0
  107. package/dist/Math/LibJsCoordsDistance/index.js +16 -0
  108. package/dist/Math/LibJsDecimal/index.d.ts +10 -0
  109. package/dist/Math/LibJsDecimal/index.js +25 -0
  110. package/dist/Misc/LibJsRegFormValidate/index.d.ts +32 -0
  111. package/dist/Misc/LibJsRegFormValidate/index.js +34 -0
  112. package/dist/Misc/LibJsRetryRequest/index.d.ts +24 -0
  113. package/dist/Misc/LibJsRetryRequest/index.js +37 -0
  114. package/dist/Misc/LibRegFormValidate/index.d.ts +32 -0
  115. package/dist/Misc/LibRegFormValidate/index.js +34 -0
  116. package/dist/Misc/LibRetryRequest/index.d.ts +24 -0
  117. package/dist/Misc/LibRetryRequest/index.js +37 -0
  118. package/dist/Random/LibJsProbabilityResult/index.d.ts +8 -0
  119. package/dist/Random/LibJsProbabilityResult/index.js +8 -0
  120. package/dist/Random/LibJsRandom/index.d.ts +9 -0
  121. package/dist/Random/LibJsRandom/index.js +11 -0
  122. package/dist/Random/LibJsRandomColor/index.d.ts +7 -0
  123. package/dist/Random/LibJsRandomColor/index.js +12 -0
  124. package/dist/Random/LibJsUniqueRandomNumbers/index.d.ts +9 -0
  125. package/dist/Random/LibJsUniqueRandomNumbers/index.js +16 -0
  126. package/dist/Random/LibProbabilityResult/index.d.ts +8 -0
  127. package/dist/Random/LibProbabilityResult/index.js +8 -0
  128. package/dist/Random/LibRandom/index.d.ts +9 -0
  129. package/dist/Random/LibRandom/index.js +11 -0
  130. package/dist/Random/LibRandomColor/index.d.ts +7 -0
  131. package/dist/Random/LibRandomColor/index.js +12 -0
  132. package/dist/Random/LibUniqueRandomNumbers/index.d.ts +9 -0
  133. package/dist/Random/LibUniqueRandomNumbers/index.js +16 -0
  134. package/dist/Time/LibJsSameTimeCheck/index.d.ts +12 -0
  135. package/dist/Time/LibJsSameTimeCheck/index.js +24 -0
  136. package/dist/Time/LibJsTimeAgo/index.d.ts +9 -0
  137. package/dist/Time/LibJsTimeAgo/index.js +27 -0
  138. package/dist/Time/LibJsTimeGreeting/index.d.ts +17 -0
  139. package/dist/Time/LibJsTimeGreeting/index.js +23 -0
  140. package/dist/Time/LibSameTimeCheck/index.d.ts +12 -0
  141. package/dist/Time/LibSameTimeCheck/index.js +24 -0
  142. package/dist/Time/LibTimeAgo/index.d.ts +9 -0
  143. package/dist/Time/LibTimeAgo/index.js +27 -0
  144. package/dist/Time/LibTimeGreeting/index.d.ts +17 -0
  145. package/dist/Time/LibTimeGreeting/index.js +23 -0
  146. package/dist/index.d.ts +1 -0
  147. package/dist/index.js +1 -0
  148. package/dist/lib.d.ts +394 -0
  149. package/dist/lib.js +404 -0
  150. package/dist/libJs.d.ts +394 -0
  151. package/dist/libJs.js +404 -0
  152. package/package.json +1 -1
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @description 角度和弧度互相转换
3
+ * @param value 角度值或弧度值
4
+ * @param type 角度类型或弧度类型
5
+ * @example
6
+ * //角度转弧度
7
+ * libJsConvertAngle(90, "rad"); //返回 1.5708... (π/2)
8
+ *
9
+ * //弧度转角度
10
+ * libJsConvertAngle(Math.PI, "deg"); //返回 180
11
+ */
12
+ export const libJsConvertAngle = (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
+ };
@@ -0,0 +1,15 @@
1
+ /** @description 计算两点角度
2
+ * @param coord1 起点坐标
3
+ * @param coord2 终点坐标
4
+ * @example
5
+ * libJsCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 0 }); //0
6
+ * libJsCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 1 }); //45
7
+ * libJsCoordsAngle({ x: 0, y: 0 }, { x: 0, y: 1 }); //90
8
+ */
9
+ export declare const libJsCoordsAngle: (coord1: {
10
+ x: number;
11
+ y: number;
12
+ }, coord2: {
13
+ x: number;
14
+ y: number;
15
+ }) => number;
@@ -0,0 +1,24 @@
1
+ /** @description 计算两点角度
2
+ * @param coord1 起点坐标
3
+ * @param coord2 终点坐标
4
+ * @example
5
+ * libJsCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 0 }); //0
6
+ * libJsCoordsAngle({ x: 0, y: 0 }, { x: 1, y: 1 }); //45
7
+ * libJsCoordsAngle({ x: 0, y: 0 }, { x: 0, y: 1 }); //90
8
+ */
9
+ export const libJsCoordsAngle = (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
+ };
@@ -0,0 +1,15 @@
1
+ /** @description 计算两点距离
2
+ * @param coord1 起点坐标
3
+ * @param coord2 终点坐标
4
+ * @example
5
+ * libJsCoordsDistance({ x: 0, y: 0 }, { x: 3, y: 4 }); //5
6
+ * libJsCoordsDistance({ x: 1, y: 1 }, { x: 4, y: 5 }); //5
7
+ * libJsCoordsDistance({ x: 0, y: 0 }, { x: 0, y: 0 }); //0
8
+ */
9
+ export declare const libJsCoordsDistance: (coord1: {
10
+ x: number;
11
+ y: number;
12
+ }, coord2: {
13
+ x: number;
14
+ y: number;
15
+ }) => number;
@@ -0,0 +1,16 @@
1
+ /** @description 计算两点距离
2
+ * @param coord1 起点坐标
3
+ * @param coord2 终点坐标
4
+ * @example
5
+ * libJsCoordsDistance({ x: 0, y: 0 }, { x: 3, y: 4 }); //5
6
+ * libJsCoordsDistance({ x: 1, y: 1 }, { x: 4, y: 5 }); //5
7
+ * libJsCoordsDistance({ x: 0, y: 0 }, { x: 0, y: 0 }); //0
8
+ */
9
+ export const libJsCoordsDistance = (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
+ };
@@ -0,0 +1,10 @@
1
+ /** @description 计算两个数的运算结果,并保留指定位数的小数
2
+ * @param num1 第一个数
3
+ * @param num2 第二个数
4
+ * @param operator 运算符,支持加减乘除
5
+ * @example
6
+ * libJsDecimal(10, 3, "+"); //13
7
+ * libJsDecimal(10, 3, "-"); //7
8
+ * libJsDecimal(10, 3, "/", 2); //3.33
9
+ */
10
+ export declare const libJsDecimal: (num1: number, num2: number, operator: "+" | "-" | "*" | "/", point?: number) => number;
@@ -0,0 +1,25 @@
1
+ import { Decimal } from "decimal.js";
2
+ /** @description 计算两个数的运算结果,并保留指定位数的小数
3
+ * @param num1 第一个数
4
+ * @param num2 第二个数
5
+ * @param operator 运算符,支持加减乘除
6
+ * @example
7
+ * libJsDecimal(10, 3, "+"); //13
8
+ * libJsDecimal(10, 3, "-"); //7
9
+ * libJsDecimal(10, 3, "/", 2); //3.33
10
+ */
11
+ export const libJsDecimal = (num1, num2, operator, point = 2) => {
12
+ const calc = {
13
+ "+": (a, b) => a.add(b),
14
+ "-": (a, b) => a.sub(b),
15
+ "*": (a, b) => a.mul(b),
16
+ "/": (a, b) => {
17
+ if (b.eq(0)) {
18
+ throw new Error("除数不能为0");
19
+ }
20
+ return a.div(b);
21
+ },
22
+ };
23
+ const result = calc[operator](new Decimal(num1), new Decimal(num2));
24
+ return Number(result.toFixed(point));
25
+ };
@@ -0,0 +1,32 @@
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
+ * libJsRegFormValidate(form, rules);
18
+ * //返回结果: []
19
+ *
20
+ * const invalidForm = { username: "jo", email: "invalid-email" };
21
+ * libJsRegFormValidate(invalidForm, rules);
22
+ * //返回结果: [
23
+ * // { key: "username", msg: "用户名不合法", name: "用户名" },
24
+ * // { key: "email", msg: "邮箱格式不正确", name: "邮箱" }
25
+ * //]
26
+ */
27
+ export declare const libJsRegFormValidate: (form: Record<string, any>, rules: Array<{
28
+ key: string;
29
+ verify: RegExp | ((value: any) => boolean);
30
+ msg: string;
31
+ name: string;
32
+ }>) => ValidationResult[];
@@ -0,0 +1,34 @@
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
+ * libJsRegFormValidate(form, rules);
13
+ * //返回结果: []
14
+ *
15
+ * const invalidForm = { username: "jo", email: "invalid-email" };
16
+ * libJsRegFormValidate(invalidForm, rules);
17
+ * //返回结果: [
18
+ * // { key: "username", msg: "用户名不合法", name: "用户名" },
19
+ * // { key: "email", msg: "邮箱格式不正确", name: "邮箱" }
20
+ * //]
21
+ */
22
+ export const libJsRegFormValidate = (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
+ };
@@ -0,0 +1,24 @@
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
+ * libJsRetryRequest({
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 libJsRetryRequest: <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>;
@@ -0,0 +1,37 @@
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
+ * libJsRetryRequest({
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 libJsRetryRequest = ({ 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
+ };
@@ -0,0 +1,32 @@
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[];
@@ -0,0 +1,34 @@
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
+ };
@@ -0,0 +1,24 @@
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>;
@@ -0,0 +1,37 @@
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
+ };
@@ -0,0 +1,8 @@
1
+ /** @description 百分比概率结果
2
+ * @param probability 触发概率,百分比,0-100
3
+ * @example
4
+ * libJsProbabilityResult(50); //50% 概率为 true
5
+ * libJsProbabilityResult(80); //80% 概率为 true
6
+ * libJsProbabilityResult(100); //100% 概率为 true
7
+ */
8
+ export declare const libJsProbabilityResult: (probability: number) => boolean;
@@ -0,0 +1,8 @@
1
+ /** @description 百分比概率结果
2
+ * @param probability 触发概率,百分比,0-100
3
+ * @example
4
+ * libJsProbabilityResult(50); //50% 概率为 true
5
+ * libJsProbabilityResult(80); //80% 概率为 true
6
+ * libJsProbabilityResult(100); //100% 概率为 true
7
+ */
8
+ export const libJsProbabilityResult = (probability) => Math.random() * 100 < probability;
@@ -0,0 +1,9 @@
1
+ /** @description 随机获取两个数之间的值,包含两数自身
2
+ * @param min 最小值
3
+ * @param max 最大值
4
+ * @param num 保留小数位数
5
+ * @example
6
+ * libJsRandom(1, 10); //1 到 10 之间的随机整数
7
+ * libJsRandom(1, 10, 2); //1 到 10 之间保留两位小数的随机数
8
+ */
9
+ export declare const libJsRandom: (min: number, max: number, num?: number) => number;
@@ -0,0 +1,11 @@
1
+ /** @description 随机获取两个数之间的值,包含两数自身
2
+ * @param min 最小值
3
+ * @param max 最大值
4
+ * @param num 保留小数位数
5
+ * @example
6
+ * libJsRandom(1, 10); //1 到 10 之间的随机整数
7
+ * libJsRandom(1, 10, 2); //1 到 10 之间保留两位小数的随机数
8
+ */
9
+ export const libJsRandom = (min, max, num = 0) => {
10
+ return parseFloat((Math.random() * (max - min) + min).toFixed(num));
11
+ };
@@ -0,0 +1,7 @@
1
+ /** @description 随机 RGBA 颜色
2
+ * @param alpha 透明度
3
+ * @example
4
+ * libJsRandomColor(); //生成随机的 RGBA 颜色,默认透明度 1
5
+ * libJsRandomColor(0.5); //生成随机的 RGBA 颜色,透明度为 0.5
6
+ */
7
+ export declare const libJsRandomColor: (alpha?: number) => string;
@@ -0,0 +1,12 @@
1
+ /** @description 随机 RGBA 颜色
2
+ * @param alpha 透明度
3
+ * @example
4
+ * libJsRandomColor(); //生成随机的 RGBA 颜色,默认透明度 1
5
+ * libJsRandomColor(0.5); //生成随机的 RGBA 颜色,透明度为 0.5
6
+ */
7
+ export const libJsRandomColor = (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
+ };
@@ -0,0 +1,9 @@
1
+ /** @description 随机生成指定个数、指定范围不重复的随机数数组
2
+ * @param min 最小值
3
+ * @param max 最大值
4
+ * @param count 数组长度
5
+ * @example
6
+ * libJsUniqueRandomNumbers(1, 10, 5); //从 1 到 10 中随机生成 5 个唯一数字
7
+ * libJsUniqueRandomNumbers(1, 100, 10); //从 1 到 100 中随机生成 10 个唯一数字
8
+ */
9
+ export declare const libJsUniqueRandomNumbers: (min: number, max: number, count: number) => number[];
@@ -0,0 +1,16 @@
1
+ /** @description 随机生成指定个数、指定范围不重复的随机数数组
2
+ * @param min 最小值
3
+ * @param max 最大值
4
+ * @param count 数组长度
5
+ * @example
6
+ * libJsUniqueRandomNumbers(1, 10, 5); //从 1 到 10 中随机生成 5 个唯一数字
7
+ * libJsUniqueRandomNumbers(1, 100, 10); //从 1 到 100 中随机生成 10 个唯一数字
8
+ */
9
+ export const libJsUniqueRandomNumbers = (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
+ };
@@ -0,0 +1,8 @@
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;
@@ -0,0 +1,8 @@
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;
@@ -0,0 +1,9 @@
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
+ */
9
+ export declare const libRandom: (min: number, max: number, num?: number) => number;
@@ -0,0 +1,11 @@
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
+ */
9
+ export const libRandom = (min, max, num = 0) => {
10
+ return parseFloat((Math.random() * (max - min) + min).toFixed(num));
11
+ };
@@ -0,0 +1,7 @@
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;
@@ -0,0 +1,12 @@
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
+ };
@@ -0,0 +1,9 @@
1
+ /** @description 随机生成指定个数、指定范围不重复的随机数数组
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[];
@@ -0,0 +1,16 @@
1
+ /** @description 随机生成指定个数、指定范围不重复的随机数数组
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
+ };
@@ -0,0 +1,12 @@
1
+ import dayjs from "dayjs";
2
+ /**
3
+ * @description 传入时间戳与当前时间判断是否为同一分、同一时、同一天、同一周、同一月、同一年
4
+ * @param timestamp 毫秒时间戳
5
+ * @param unit 判断单位
6
+ * @returns 0-同一单位时间 1-新单位时间 -1时间戳大于当前时间
7
+ * @example
8
+ * const timestamp = 1679872800000; //时间戳
9
+ * const result = libSameTimeCheck(timestamp, 'day'); //判断是否为同一天
10
+ * console.log(result); //0: 同一天, 1: 新的一天, -1: 时间戳大于当前时间
11
+ */
12
+ export declare const libJsSameTimeCheck: (timestamp: number, unit: dayjs.OpUnitType) => 0 | 1 | -1;
@@ -0,0 +1,24 @@
1
+ import dayjs from "dayjs";
2
+ /**
3
+ * @description 传入时间戳与当前时间判断是否为同一分、同一时、同一天、同一周、同一月、同一年
4
+ * @param timestamp 毫秒时间戳
5
+ * @param unit 判断单位
6
+ * @returns 0-同一单位时间 1-新单位时间 -1时间戳大于当前时间
7
+ * @example
8
+ * const timestamp = 1679872800000; //时间戳
9
+ * const result = libSameTimeCheck(timestamp, 'day'); //判断是否为同一天
10
+ * console.log(result); //0: 同一天, 1: 新的一天, -1: 时间戳大于当前时间
11
+ */
12
+ export const libJsSameTimeCheck = (timestamp, unit) => {
13
+ const inputTime = dayjs(timestamp).startOf(unit);
14
+ const currentTime = dayjs().startOf(unit);
15
+ if (inputTime.isSame(currentTime)) {
16
+ return 0;
17
+ }
18
+ else if (inputTime.isBefore(currentTime)) {
19
+ return 1;
20
+ }
21
+ else {
22
+ return -1;
23
+ }
24
+ };
@@ -0,0 +1,9 @@
1
+ /** @description 时间差计算
2
+ * @param timestamp 毫秒时间戳
3
+ * @example
4
+ * libJsTimeAgotamp(Date.now() - 3600000); //"1 小时前"
5
+ * libJsTimeAgotamp(Date.now() - 86400000); //"1 天前"
6
+ * libJsTimeAgotamp(Date.now() - 31536000000); //"1 年前"
7
+ * libJsTimeAgotamp(Date.now() - 10000); //"刚刚"
8
+ */
9
+ export declare const libJsTimeAgo: (timestamp: number) => string;