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,13 @@
1
+ /**
2
+ * @description 数字每三位添加逗号
3
+ * @param num 需要格式化的数字
4
+ * @param reserve 保留小数位数
5
+ * @example
6
+ * const formatted = libJsNumComma(1234567.89);
7
+ * console.log(formatted); //1,234,567.89
8
+ */
9
+ export const libJsNumComma = (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
+ };
@@ -0,0 +1,12 @@
1
+ export interface LibJsNumberUnitParams {
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] = libJsNumberUnit(1500, { K: 1000, M: 1000000 });
10
+ * console.log(value, unit); //1.50 K
11
+ */
12
+ export declare const libJsNumberUnit: (num: number, units: LibJsNumberUnitParams) => string[];
@@ -0,0 +1,23 @@
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] = libJsNumberUnit(1500, { K: 1000, M: 1000000 });
8
+ * console.log(value, unit); //1.50 K
9
+ */
10
+ export const libJsNumberUnit = (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
+ };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * @description 将秒数格式化为中文时间描述,支持扩展到年
3
+ * @param seconds 秒数
4
+ * @returns 格式化后的中文时间
5
+ * @example
6
+ * libJsSecondsFormatterChinese(100000); //"1天3小时46分40秒"
7
+ * libJsSecondsFormatterChinese(31536000); //"1年"
8
+ * libJsSecondsFormatterChinese(3600); //"1小时"
9
+ * libJsSecondsFormatterChinese(90); //"1分30秒"
10
+ */
11
+ export declare const libJsSecondsFormatterChinese: (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
+ * libJsSecondsFormatterChinese(100000); //"1天3小时46分40秒"
10
+ * libJsSecondsFormatterChinese(31536000); //"1年"
11
+ * libJsSecondsFormatterChinese(3600); //"1小时"
12
+ * libJsSecondsFormatterChinese(90); //"1分30秒"
13
+ */
14
+ export const libJsSecondsFormatterChinese = (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
+ };
@@ -0,0 +1,8 @@
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;
@@ -0,0 +1,11 @@
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
+ };
@@ -0,0 +1,9 @@
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;
@@ -0,0 +1,13 @@
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
+ };
@@ -0,0 +1,12 @@
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[];
@@ -0,0 +1,23 @@
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
+ };
@@ -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
+ };
@@ -0,0 +1,9 @@
1
+ /** @description 计算表达式字符串
2
+ * @param expression 表达式字符串
3
+ * @param point 小数点精度
4
+ * @returns 计算结果
5
+ * @example
6
+ * const result = libCalculateExpression("(1+2)-(3*4)/5");
7
+ * console.log(result); //0.6
8
+ */
9
+ export declare const libCalculateExpression: (expression: string, point?: number) => number;
@@ -0,0 +1,107 @@
1
+ import Decimal from 'decimal.js';
2
+ /** @description 计算表达式字符串
3
+ * @param expression 表达式字符串
4
+ * @param point 小数点精度
5
+ * @returns 计算结果
6
+ * @example
7
+ * const result = libCalculateExpression("(1+2)-(3*4)/5");
8
+ * console.log(result); //0.6
9
+ */
10
+ export const libCalculateExpression = (expression, point = 2) => {
11
+ //清除所有空格
12
+ expression = expression.replace(/\s+/g, '');
13
+ //支持的运算符和优先级
14
+ const operators = {
15
+ "+": 1,
16
+ "-": 1,
17
+ "*": 2,
18
+ "/": 2,
19
+ };
20
+ //支持的小数点精度
21
+ const toDecimal = (value) => new Decimal(value);
22
+ //判断字符是否是运算符
23
+ const isOperator = (char) => ['+', '-', '*', '/'].includes(char);
24
+ //判断字符是否是数字(包括小数点)
25
+ const isNumber = (char) => /[0-9.]/.test(char);
26
+ //解析表达式并计算
27
+ const evaluate = (expression) => {
28
+ const outputQueue = []; //输出队列
29
+ const operatorStack = []; //操作符栈
30
+ let i = 0;
31
+ while (i < expression.length) {
32
+ const char = expression[i];
33
+ if (isNumber(char)) {
34
+ let numStr = '';
35
+ //处理多位数字(支持小数)
36
+ while (i < expression.length && isNumber(expression[i])) {
37
+ numStr += expression[i];
38
+ i++;
39
+ }
40
+ outputQueue.push(toDecimal(numStr));
41
+ }
42
+ else if (char === '(') {
43
+ operatorStack.push(char);
44
+ i++;
45
+ }
46
+ else if (char === ')') {
47
+ //处理右括号,直到遇到左括号
48
+ while (operatorStack.length > 0 && operatorStack[operatorStack.length - 1] !== '(') {
49
+ outputQueue.push(operatorStack.pop());
50
+ }
51
+ operatorStack.pop(); //弹出左括号
52
+ i++;
53
+ }
54
+ else if (isOperator(char)) {
55
+ //运算符
56
+ while (operatorStack.length > 0 && operators[operatorStack[operatorStack.length - 1]] >= operators[char]) {
57
+ outputQueue.push(operatorStack.pop());
58
+ }
59
+ operatorStack.push(char);
60
+ i++;
61
+ }
62
+ else {
63
+ throw new Error(`无效字符: ${char}`);
64
+ }
65
+ }
66
+ //把所有剩余的操作符添加到输出队列
67
+ while (operatorStack.length > 0) {
68
+ outputQueue.push(operatorStack.pop());
69
+ }
70
+ //执行运算
71
+ const calcStack = [];
72
+ for (let token of outputQueue) {
73
+ if (typeof token === 'string') {
74
+ const b = calcStack.pop();
75
+ const a = calcStack.pop();
76
+ switch (token) {
77
+ case '+':
78
+ calcStack.push(a.add(b));
79
+ break;
80
+ case '-':
81
+ calcStack.push(a.sub(b));
82
+ break;
83
+ case '*':
84
+ calcStack.push(a.mul(b));
85
+ break;
86
+ case '/':
87
+ if (b.eq(0))
88
+ throw new Error("除数不能为零");
89
+ calcStack.push(a.div(b));
90
+ break;
91
+ }
92
+ }
93
+ else {
94
+ calcStack.push(token);
95
+ }
96
+ }
97
+ return calcStack.pop();
98
+ };
99
+ try {
100
+ //调用计算器并返回结果
101
+ const result = evaluate(expression);
102
+ return Number(result.toFixed(point)); //保留指定的小数位数
103
+ }
104
+ catch (error) {
105
+ throw new Error("表达式计算失败:" + error.message);
106
+ }
107
+ };
@@ -0,0 +1,12 @@
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;
@@ -0,0 +1,22 @@
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
+ };
@@ -0,0 +1,15 @@
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;
@@ -0,0 +1,24 @@
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
+ };
@@ -0,0 +1,15 @@
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;
@@ -0,0 +1,16 @@
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
+ };
@@ -0,0 +1,10 @@
1
+ /** @description 计算两个数的运算结果,并保留指定位数的小数
2
+ * @param num1 第一个数
3
+ * @param num2 第二个数
4
+ * @param operator 运算符,支持加减乘除
5
+ * @example
6
+ * libDecimal(10, 3, "+"); //13
7
+ * libDecimal(10, 3, "-"); //7
8
+ * libDecimal(10, 3, "/", 2); //3.33
9
+ */
10
+ export declare const libDecimal: (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
+ * libDecimal(10, 3, "+"); //13
8
+ * libDecimal(10, 3, "-"); //7
9
+ * libDecimal(10, 3, "/", 2); //3.33
10
+ */
11
+ export const libDecimal = (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,9 @@
1
+ /** @description 计算表达式字符串
2
+ * @param expression 表达式字符串
3
+ * @param point 小数点精度
4
+ * @returns 计算结果
5
+ * @example
6
+ * const result = libJsCalculateExpression("(1+2)-(3*4)/5");
7
+ * console.log(result); //0.6
8
+ */
9
+ export declare const libJsCalculateExpression: (expression: string, point?: number) => number;
@@ -0,0 +1,107 @@
1
+ import Decimal from 'decimal.js';
2
+ /** @description 计算表达式字符串
3
+ * @param expression 表达式字符串
4
+ * @param point 小数点精度
5
+ * @returns 计算结果
6
+ * @example
7
+ * const result = libJsCalculateExpression("(1+2)-(3*4)/5");
8
+ * console.log(result); //0.6
9
+ */
10
+ export const libJsCalculateExpression = (expression, point = 2) => {
11
+ //清除所有空格
12
+ expression = expression.replace(/\s+/g, '');
13
+ //支持的运算符和优先级
14
+ const operators = {
15
+ "+": 1,
16
+ "-": 1,
17
+ "*": 2,
18
+ "/": 2,
19
+ };
20
+ //支持的小数点精度
21
+ const toDecimal = (value) => new Decimal(value);
22
+ //判断字符是否是运算符
23
+ const isOperator = (char) => ['+', '-', '*', '/'].includes(char);
24
+ //判断字符是否是数字(包括小数点)
25
+ const isNumber = (char) => /[0-9.]/.test(char);
26
+ //解析表达式并计算
27
+ const evaluate = (expression) => {
28
+ const outputQueue = []; //输出队列
29
+ const operatorStack = []; //操作符栈
30
+ let i = 0;
31
+ while (i < expression.length) {
32
+ const char = expression[i];
33
+ if (isNumber(char)) {
34
+ let numStr = '';
35
+ //处理多位数字(支持小数)
36
+ while (i < expression.length && isNumber(expression[i])) {
37
+ numStr += expression[i];
38
+ i++;
39
+ }
40
+ outputQueue.push(toDecimal(numStr));
41
+ }
42
+ else if (char === '(') {
43
+ operatorStack.push(char);
44
+ i++;
45
+ }
46
+ else if (char === ')') {
47
+ //处理右括号,直到遇到左括号
48
+ while (operatorStack.length > 0 && operatorStack[operatorStack.length - 1] !== '(') {
49
+ outputQueue.push(operatorStack.pop());
50
+ }
51
+ operatorStack.pop(); //弹出左括号
52
+ i++;
53
+ }
54
+ else if (isOperator(char)) {
55
+ //运算符
56
+ while (operatorStack.length > 0 && operators[operatorStack[operatorStack.length - 1]] >= operators[char]) {
57
+ outputQueue.push(operatorStack.pop());
58
+ }
59
+ operatorStack.push(char);
60
+ i++;
61
+ }
62
+ else {
63
+ throw new Error(`无效字符: ${char}`);
64
+ }
65
+ }
66
+ //把所有剩余的操作符添加到输出队列
67
+ while (operatorStack.length > 0) {
68
+ outputQueue.push(operatorStack.pop());
69
+ }
70
+ //执行运算
71
+ const calcStack = [];
72
+ for (let token of outputQueue) {
73
+ if (typeof token === 'string') {
74
+ const b = calcStack.pop();
75
+ const a = calcStack.pop();
76
+ switch (token) {
77
+ case '+':
78
+ calcStack.push(a.add(b));
79
+ break;
80
+ case '-':
81
+ calcStack.push(a.sub(b));
82
+ break;
83
+ case '*':
84
+ calcStack.push(a.mul(b));
85
+ break;
86
+ case '/':
87
+ if (b.eq(0))
88
+ throw new Error("除数不能为零");
89
+ calcStack.push(a.div(b));
90
+ break;
91
+ }
92
+ }
93
+ else {
94
+ calcStack.push(token);
95
+ }
96
+ }
97
+ return calcStack.pop();
98
+ };
99
+ try {
100
+ //调用计算器并返回结果
101
+ const result = evaluate(expression);
102
+ return Number(result.toFixed(point)); //保留指定的小数位数
103
+ }
104
+ catch (error) {
105
+ throw new Error("表达式计算失败:" + error.message);
106
+ }
107
+ };
@@ -0,0 +1,12 @@
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 declare const libJsConvertAngle: (value: number, type: "rad" | "deg") => number;