zmdms-utils 0.0.17 → 0.0.19
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/dist/es/_virtual/_commonjsHelpers.js +3 -0
- package/dist/es/_virtual/decimal.js +7 -0
- package/dist/es/_virtual/test.js +3 -0
- package/dist/es/baseUrl.js +1 -1
- package/dist/es/common.d.ts +7 -1
- package/dist/es/common.js +10 -2
- package/dist/es/constants.d.ts +18 -0
- package/dist/es/constants.js +23 -0
- package/dist/es/file.js +6 -6
- package/dist/es/math.js +64 -13
- package/dist/es/node_modules/decimal.js/decimal.js +4896 -0
- package/dist/es/node_modules/decimal.js/decimal2.js +4930 -0
- package/dist/es/src/test.js +106 -0
- package/dist/es/test.js +2 -54
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/package.json +2 -1
- package/src/baseUrl.ts +1 -1
- package/src/common.ts +10 -1
- package/src/constants.ts +25 -0
- package/src/file.ts +6 -6
- package/src/index.ts +6 -0
- package/src/math.ts +64 -13
- package/src/test.js +101 -3
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import '../node_modules/decimal.js/decimal2.js';
|
|
2
|
+
import { exports as decimalExports } from '../_virtual/decimal.js';
|
|
3
|
+
|
|
4
|
+
// console.log(unescapeString("&&&啊你"));
|
|
5
|
+
|
|
6
|
+
// console.log(dangerouslySetXss("&&&啊你"));
|
|
7
|
+
|
|
8
|
+
// console.log(dangerouslySetXss1("&&&啊你"));
|
|
9
|
+
|
|
10
|
+
// functions and constants
|
|
11
|
+
// console.log("functions and constants");
|
|
12
|
+
// print(math.round(math.e, 3)); // 2.718
|
|
13
|
+
// print(math.atan2(3, -3) / math.pi); // 0.75
|
|
14
|
+
// print(math.log(10000, 10)); // 4
|
|
15
|
+
// print(math.sqrt(-4)); // 2i
|
|
16
|
+
// print(
|
|
17
|
+
// math.pow(
|
|
18
|
+
// [
|
|
19
|
+
// [-1, 2],
|
|
20
|
+
// [3, 1],
|
|
21
|
+
// ],
|
|
22
|
+
// 2
|
|
23
|
+
// )
|
|
24
|
+
// ); // [[7, 0], [0, 7]]
|
|
25
|
+
// print(math.derivative("x^2 + x", "x")); // 2 * x + 1
|
|
26
|
+
// console.log();
|
|
27
|
+
|
|
28
|
+
// // expressions
|
|
29
|
+
// console.log("expressions");
|
|
30
|
+
// print(math.evaluate("1.2 * (2 + 4.5)")); // 7.8
|
|
31
|
+
// print(math.evaluate("12.7 cm to inch")); // 5 inch
|
|
32
|
+
// print(math.evaluate("sin(45 deg) ^ 2")); // 0.5
|
|
33
|
+
// print(math.evaluate("9 / 3 + 2i")); // 3 + 2i
|
|
34
|
+
// print(math.evaluate("det([-1, 2; 3, 1])")); // -7
|
|
35
|
+
// print(math.evaluate("NaN + '0.2'")); // -7
|
|
36
|
+
// console.log();
|
|
37
|
+
|
|
38
|
+
// // chained operations
|
|
39
|
+
// console.log("chained operations");
|
|
40
|
+
// const a = math.chain(3).add(4).multiply(2).done();
|
|
41
|
+
// print(a); // 14
|
|
42
|
+
// console.log();
|
|
43
|
+
|
|
44
|
+
// // mixed use of different data types in functions
|
|
45
|
+
// console.log("mixed use of data types");
|
|
46
|
+
// print(math.add(4, [5, 6])); // number + Array, [9, 10]
|
|
47
|
+
// print(math.multiply(math.unit("5 mm"), 3)); // Unit * number, 15 mm
|
|
48
|
+
// print(math.subtract([2, 3, 4], 5)); // Array - number, [-3, -2, -1]
|
|
49
|
+
// print(math.add(math.matrix([2, 3]), [4, 5])); // Matrix + Array, [6, 8]
|
|
50
|
+
// console.log();
|
|
51
|
+
|
|
52
|
+
// /**
|
|
53
|
+
// * Helper function to output a value in the console. Value will be formatted.
|
|
54
|
+
// * @param {*} value
|
|
55
|
+
// */
|
|
56
|
+
// function print(value) {
|
|
57
|
+
// const precision = 14;
|
|
58
|
+
// console.log(math.format(value, precision));
|
|
59
|
+
// }
|
|
60
|
+
|
|
61
|
+
// const math = require("mathjs");
|
|
62
|
+
// function print(value) {
|
|
63
|
+
// const precision = 14;
|
|
64
|
+
// console.log(math.format(value, precision));
|
|
65
|
+
// }
|
|
66
|
+
|
|
67
|
+
// console.log(performance.now());
|
|
68
|
+
// console.log(math.add(0.1, 0.2));
|
|
69
|
+
// console.log(performance.now());
|
|
70
|
+
// console.log(performance.now());
|
|
71
|
+
// console.log(0.1 + 0.2);
|
|
72
|
+
// console.log(performance.now());
|
|
73
|
+
// const BigNumber = require("bignumber.js");
|
|
74
|
+
// console.log(performance.now());
|
|
75
|
+
// const num1 = new BigNumber(0.1);
|
|
76
|
+
// const num2 = new BigNumber(0.2);
|
|
77
|
+
|
|
78
|
+
// const result = num1.plus(num2);
|
|
79
|
+
|
|
80
|
+
// console.log(result.toString());
|
|
81
|
+
|
|
82
|
+
// console.log(performance.now());
|
|
83
|
+
|
|
84
|
+
const Decimal = decimalExports;
|
|
85
|
+
|
|
86
|
+
const num1 = new Decimal("0.1");
|
|
87
|
+
|
|
88
|
+
console.log(num1.add(0.21).toNumber(), typeof num1.add(0.21).toNumber());
|
|
89
|
+
|
|
90
|
+
console.log(num1.sub(0.21).toNumber(), typeof num1.sub(0.21).toNumber());
|
|
91
|
+
|
|
92
|
+
console.log(num1.mul(0.21).toNumber(), typeof num1.mul(0.21).toNumber());
|
|
93
|
+
Decimal.set({ precision: 5 });
|
|
94
|
+
console.log(
|
|
95
|
+
new Decimal(1).div(1.1).toNumber(),
|
|
96
|
+
typeof new Decimal(1).div(1.1).toNumber()
|
|
97
|
+
);
|
|
98
|
+
console.log(
|
|
99
|
+
new Decimal(1).dividedBy(1.1).toNumber(),
|
|
100
|
+
typeof new Decimal(1).dividedBy(1.1).toNumber()
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
console.log(
|
|
104
|
+
Number(new Decimal(1.1).toFixed(2)),
|
|
105
|
+
typeof Number(new Decimal(1.1).toFixed(2)).toFixed(2)
|
|
106
|
+
);
|
package/dist/es/test.js
CHANGED
|
@@ -1,54 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return "";
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
const escapedChars = {
|
|
7
|
-
"&": "&",
|
|
8
|
-
"<": "<",
|
|
9
|
-
">": ">",
|
|
10
|
-
""": '"',
|
|
11
|
-
"'": "'",
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
return input.replace(/&(amp|lt|gt|quot|#039);/g, (match) => {
|
|
15
|
-
return escapedChars[match] || match;
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function dangerouslySetXss(input) {
|
|
20
|
-
if (!input) {
|
|
21
|
-
return "";
|
|
22
|
-
}
|
|
23
|
-
const escapedChars = {
|
|
24
|
-
"<": "",
|
|
25
|
-
">": "",
|
|
26
|
-
" ": " ",
|
|
27
|
-
"&": "&",
|
|
28
|
-
""": '"',
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
return input.replace(/&(amp|lt|gt|quot|nbsp);/g, (match) => {
|
|
32
|
-
return escapedChars[match] || match;
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function dangerouslySetXss1(str) {
|
|
37
|
-
if (!str) {
|
|
38
|
-
return str;
|
|
39
|
-
}
|
|
40
|
-
try {
|
|
41
|
-
const arrEntities = { lt: "", gt: "", nbsp: " ", amp: "&", quot: '"' };
|
|
42
|
-
return str?.replace?.(/&(lt|gt|nbsp|amp|quot);/gi, function (all, t) {
|
|
43
|
-
return arrEntities[t];
|
|
44
|
-
});
|
|
45
|
-
} catch (err) {
|
|
46
|
-
return str;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
console.log(unescapeString("&&&啊你"));
|
|
51
|
-
|
|
52
|
-
console.log(dangerouslySetXss("&&&啊你"));
|
|
53
|
-
|
|
54
|
-
console.log(dangerouslySetXss1("&&&啊你"));
|
|
1
|
+
import './src/test.js';
|
|
2
|
+
export { __exports as default } from './_virtual/test.js';
|
package/dist/index.d.ts
CHANGED
|
@@ -5,8 +5,9 @@ export { getToken, removeToken, setToken } from './es/auth.js';
|
|
|
5
5
|
export { AxiosRequest, IOptions, IOtherOptions } from './es/request.js';
|
|
6
6
|
export { getBaseUrl } from './es/baseUrl.js';
|
|
7
7
|
export { Crypto, md5 } from './es/crypto.js';
|
|
8
|
-
export { addThousedSeparator, copyToClipboard, dangerouslyXss, delay, emitter, getFileExtension, isImageExtension, parseQueryParams, specialString, unescapeString } from './es/common.js';
|
|
8
|
+
export { addThousedSeparator, copyToClipboard, dangerouslyXss, delay, emitter, getFileExtension, isImageExtension, isNonEmpty, parseQueryParams, specialString, unescapeString } from './es/common.js';
|
|
9
9
|
export { divide, exactRound, formatUnit, minus, plus, times } from './es/math.js';
|
|
10
10
|
export { validatePassword } from './es/passwordValidate.js';
|
|
11
11
|
export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate } from './es/validate.js';
|
|
12
12
|
export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOriginalLink, createThumbnailLink, createUploadFileLink, downloadFile, exportBolb, previewFile } from './es/file.js';
|
|
13
|
+
export { EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE } from './es/constants.js';
|
package/dist/index.js
CHANGED
|
@@ -5,8 +5,9 @@ export { getToken, removeToken, setToken } from './es/auth.js';
|
|
|
5
5
|
export { AxiosRequest } from './es/request.js';
|
|
6
6
|
export { getBaseUrl } from './es/baseUrl.js';
|
|
7
7
|
export { Crypto, md5 } from './es/crypto.js';
|
|
8
|
-
export { addThousedSeparator, copyToClipboard, dangerouslyXss, delay, emitter, getFileExtension, isImageExtension, parseQueryParams, specialString, unescapeString } from './es/common.js';
|
|
8
|
+
export { addThousedSeparator, copyToClipboard, dangerouslyXss, delay, emitter, getFileExtension, isImageExtension, isNonEmpty, parseQueryParams, specialString, unescapeString } from './es/common.js';
|
|
9
9
|
export { divide, exactRound, formatUnit, minus, plus, times } from './es/math.js';
|
|
10
10
|
export { validatePassword } from './es/passwordValidate.js';
|
|
11
11
|
export { emailValidate, idCardValidate, morePhoneValidate, phoneValidate, strLenValidate } from './es/validate.js';
|
|
12
12
|
export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOriginalLink, createThumbnailLink, createUploadFileLink, downloadFile, exportBolb, previewFile } from './es/file.js';
|
|
13
|
+
export { EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE } from './es/constants.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zmdms-utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"qiankun": "^2.10.8"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
+
"decimal.js": "^10.4.3",
|
|
21
22
|
"mitt": "^3.0.0"
|
|
22
23
|
},
|
|
23
24
|
"devDependencies": {
|
package/src/baseUrl.ts
CHANGED
|
@@ -4,7 +4,7 @@ export function getBaseUrl(processObj: any) {
|
|
|
4
4
|
|
|
5
5
|
// api各个环境地址
|
|
6
6
|
const apiBaseUrlObj: any = {
|
|
7
|
-
dev: processObj.REACT_APP_API_DEV || "http://192.168.0.
|
|
7
|
+
dev: processObj.REACT_APP_API_DEV || "http://192.168.0.134:8000",
|
|
8
8
|
test: processObj.REACT_APP_API_TEST || window.location.origin,
|
|
9
9
|
stage: processObj.REACT_APP_API_STAGE || window.location.origin,
|
|
10
10
|
product: processObj.REACT_APP_API_PRODUCT || window.location.origin,
|
package/src/common.ts
CHANGED
|
@@ -186,7 +186,7 @@ export function getFileExtension(fileName: string): [string, string] {
|
|
|
186
186
|
}
|
|
187
187
|
const lastPointIndex = fileName.lastIndexOf("."); // 从最后取点
|
|
188
188
|
const filePreName = fileName.substring(0, lastPointIndex); // 前面的名字
|
|
189
|
-
const fileExtension = fileName.substring(lastPointIndex + 1); // 后面的文件后缀
|
|
189
|
+
const fileExtension = fileName.substring(lastPointIndex + 1).toLowerCase(); // 后面的文件后缀
|
|
190
190
|
return [filePreName, fileExtension];
|
|
191
191
|
}
|
|
192
192
|
|
|
@@ -211,3 +211,12 @@ export function isImageExtension(extension: string): boolean {
|
|
|
211
211
|
|
|
212
212
|
return imageExtensions.includes(extension.toLowerCase());
|
|
213
213
|
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* 判断值是否为非空
|
|
217
|
+
* @param number 传入一个值
|
|
218
|
+
* @returns
|
|
219
|
+
*/
|
|
220
|
+
export function isNonEmpty(number: any) {
|
|
221
|
+
return number !== null && number !== undefined;
|
|
222
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// 薪酬中心-app name
|
|
2
|
+
export const SALARY_CENTER_APPNAME = "cicohr-salary";
|
|
3
|
+
|
|
4
|
+
// 人事中心-app name
|
|
5
|
+
export const PERSONNEL_CENTER_APPNAME = "cicohr-personnel";
|
|
6
|
+
|
|
7
|
+
// 发布订阅的常量 parent call children
|
|
8
|
+
/**
|
|
9
|
+
* 子应用 通知 父应用
|
|
10
|
+
*/
|
|
11
|
+
export const EMITTER_CCP_KEY = "emitter-ccp-key";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 父应用 通知 子应用订阅
|
|
15
|
+
*/
|
|
16
|
+
export const EMITTER_PCC_KEY = "emitter-pcc-key";
|
|
17
|
+
|
|
18
|
+
export const EMITTER_TYPE = {
|
|
19
|
+
delTab: "delTab", // 删除单个页签
|
|
20
|
+
addTab: "addTab", // 增加单个页签
|
|
21
|
+
delTabs: "delTabs", // 删除多个页签
|
|
22
|
+
routes: "routes", // 传递路由信息
|
|
23
|
+
treeSearchWhitePath: "treeSearchWhitePath", // 子应用传递搜索树的路由白名单
|
|
24
|
+
treeSearchSet: "treeSearchSet", // 父应用传递搜索树的值
|
|
25
|
+
};
|
package/src/file.ts
CHANGED
|
@@ -39,7 +39,7 @@ export function downloadFile(
|
|
|
39
39
|
} else {
|
|
40
40
|
xhr.open(
|
|
41
41
|
"GET",
|
|
42
|
-
`${apiBaseURL}/api/zmdms-resource/oss/endpoint/download-file/${fileId}?Zmdms-Auth
|
|
42
|
+
`${apiBaseURL}/api/zmdms-resource/oss/endpoint/download-file/${fileId}?Zmdms-Auth=${token}&addWaterMark=${waterMark}`,
|
|
43
43
|
true
|
|
44
44
|
);
|
|
45
45
|
}
|
|
@@ -183,13 +183,13 @@ export function previewFile(
|
|
|
183
183
|
window.open(
|
|
184
184
|
`${apiBaseURL}/picturesAttchPreview?attachIds=${
|
|
185
185
|
attachIds ? `${fileId},${attachIds}` : fileId
|
|
186
|
-
}&Zmdms-Auth
|
|
186
|
+
}&Zmdms-Auth=${token}` +
|
|
187
187
|
(fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
|
|
188
188
|
);
|
|
189
189
|
} else {
|
|
190
190
|
// 普通预览
|
|
191
191
|
window.open(
|
|
192
|
-
`${apiBaseURL}/attchPreview?attachId=${fileId}&Zmdms-Auth
|
|
192
|
+
`${apiBaseURL}/attchPreview?attachId=${fileId}&Zmdms-Auth=${token}&officePreviewType=pdf` +
|
|
193
193
|
(fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
|
|
194
194
|
);
|
|
195
195
|
}
|
|
@@ -264,7 +264,7 @@ export function createDownloadLink(
|
|
|
264
264
|
if (open) {
|
|
265
265
|
return `${apiBaseURL}/api/zmdms-resource/oss/endpoint/public/download-file/${fileId}`;
|
|
266
266
|
}
|
|
267
|
-
return `${apiBaseURL}/api/zmdms-resource/oss/endpoint/download-file/${fileId}?Zmdms-Auth
|
|
267
|
+
return `${apiBaseURL}/api/zmdms-resource/oss/endpoint/download-file/${fileId}?Zmdms-Auth=${token}`;
|
|
268
268
|
}
|
|
269
269
|
|
|
270
270
|
/**
|
|
@@ -298,7 +298,7 @@ export function createOriginalLink(
|
|
|
298
298
|
// 所以将文件名 与 文件id组合来生成不同的文件名
|
|
299
299
|
previewFileName = `${filePreName}-${fileId}.${fileExtension}`;
|
|
300
300
|
return (
|
|
301
|
-
`${apiBaseURL}/attchPreview?attachId=${fileId}&Zmdms-Auth
|
|
301
|
+
`${apiBaseURL}/attchPreview?attachId=${fileId}&Zmdms-Auth=${token}&officePreviewType=pdf` +
|
|
302
302
|
(fileName ? `&titleName=${encodeURIComponent(previewFileName)}` : "")
|
|
303
303
|
);
|
|
304
304
|
}
|
|
@@ -321,7 +321,7 @@ export function createThumbnailLink(
|
|
|
321
321
|
) {
|
|
322
322
|
const { API_BASEURL, authToken, scale = 0.5 } = options;
|
|
323
323
|
const { token, apiBaseURL } = getBasicInfo(API_BASEURL || "", authToken);
|
|
324
|
-
return `${apiBaseURL}/api/zmdms-resource/oss/endpoint/thumbnail/${fileId}?scale=${scale}&Zmdms-Auth
|
|
324
|
+
return `${apiBaseURL}/api/zmdms-resource/oss/endpoint/thumbnail/${fileId}?scale=${scale}&Zmdms-Auth=${token}`;
|
|
325
325
|
}
|
|
326
326
|
|
|
327
327
|
/**
|
package/src/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ export {
|
|
|
16
16
|
isImageExtension,
|
|
17
17
|
dangerouslyXss,
|
|
18
18
|
specialString,
|
|
19
|
+
isNonEmpty,
|
|
19
20
|
} from "./common";
|
|
20
21
|
export { plus, minus, times, divide, exactRound, formatUnit } from "./math";
|
|
21
22
|
export { validatePassword } from "./passwordValidate";
|
|
@@ -38,3 +39,8 @@ export {
|
|
|
38
39
|
createUploadFileLink,
|
|
39
40
|
createDeleteFileLink,
|
|
40
41
|
} from "./file";
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 常量相关
|
|
45
|
+
*/
|
|
46
|
+
export { EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE } from "./constants";
|
package/src/math.ts
CHANGED
|
@@ -1,12 +1,38 @@
|
|
|
1
|
+
import Decimal from "decimal.js";
|
|
1
2
|
type numType = number | string;
|
|
3
|
+
/**
|
|
4
|
+
* 如果传入的数,有非数字的情况 那么忽略这个数
|
|
5
|
+
* 非数字的情况包括 null undefined
|
|
6
|
+
* 因为null转数字的结果是 0 可能会影响计算结果。
|
|
7
|
+
* 而这个可能不符合预期
|
|
8
|
+
*/
|
|
9
|
+
function initNum(num1: any, num2: any) {
|
|
10
|
+
// 如果两个数都是非数字 那么直接返回0
|
|
11
|
+
if (isNaN(num1) && isNaN(num2)) {
|
|
12
|
+
return 0;
|
|
13
|
+
}
|
|
14
|
+
if (isNaN(num1)) {
|
|
15
|
+
return num2;
|
|
16
|
+
}
|
|
17
|
+
if (isNaN(num2)) {
|
|
18
|
+
return num1;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
2
22
|
/**
|
|
3
23
|
* 加法
|
|
4
24
|
*/
|
|
5
25
|
export function plus(num1: numType, num2: numType) {
|
|
6
26
|
try {
|
|
7
|
-
const _num1 = Number(num1);
|
|
8
|
-
const _num2 = Number(num2);
|
|
9
|
-
|
|
27
|
+
const _num1 = num1 == null ? NaN : Number(num1);
|
|
28
|
+
const _num2 = num2 == null ? NaN : Number(num2);
|
|
29
|
+
// 如果两个数都是非数字 那么直接返回0
|
|
30
|
+
const result = initNum(_num1, _num2);
|
|
31
|
+
if (typeof result === "number") {
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
return new Decimal(_num1).add(_num2).toNumber();
|
|
35
|
+
// return (isNaN(_num1) ? 0 : _num1) + (isNaN(_num2) ? 0 : _num2);
|
|
10
36
|
} catch (err) {
|
|
11
37
|
console.error("计算加法出错", err);
|
|
12
38
|
return 0;
|
|
@@ -18,9 +44,15 @@ export function plus(num1: numType, num2: numType) {
|
|
|
18
44
|
*/
|
|
19
45
|
export function minus(num1: numType, num2: numType) {
|
|
20
46
|
try {
|
|
21
|
-
const _num1 = Number(num1);
|
|
22
|
-
const _num2 = Number(num2);
|
|
23
|
-
|
|
47
|
+
const _num1 = num1 == null ? NaN : Number(num1);
|
|
48
|
+
const _num2 = num2 == null ? NaN : Number(num2);
|
|
49
|
+
// 如果两个数都是非数字 那么直接返回0
|
|
50
|
+
const result = initNum(_num1, _num2);
|
|
51
|
+
if (typeof result === "number") {
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
return new Decimal(_num1).sub(_num2).toNumber();
|
|
55
|
+
// return (isNaN(_num1) ? 0 : _num1) - (isNaN(_num2) ? 0 : _num2);
|
|
24
56
|
} catch (err) {
|
|
25
57
|
console.error("计算减法出错", err);
|
|
26
58
|
return 0;
|
|
@@ -32,9 +64,15 @@ export function minus(num1: numType, num2: numType) {
|
|
|
32
64
|
*/
|
|
33
65
|
export function times(num1: numType, num2: numType) {
|
|
34
66
|
try {
|
|
35
|
-
const _num1 = Number(num1);
|
|
36
|
-
const _num2 = Number(num2);
|
|
37
|
-
|
|
67
|
+
const _num1 = num1 == null ? NaN : Number(num1);
|
|
68
|
+
const _num2 = num2 == null ? NaN : Number(num2);
|
|
69
|
+
// 如果两个数都是非数字 那么直接返回0
|
|
70
|
+
const result = initNum(_num1, _num2);
|
|
71
|
+
if (typeof result === "number") {
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
return new Decimal(_num1).mul(_num2).toNumber();
|
|
75
|
+
// return (isNaN(_num1) ? 0 : _num1) * (isNaN(_num2) ? 0 : _num2);
|
|
38
76
|
} catch (err) {
|
|
39
77
|
console.error("计算乘法出错", err);
|
|
40
78
|
return 0;
|
|
@@ -46,9 +84,16 @@ export function times(num1: numType, num2: numType) {
|
|
|
46
84
|
*/
|
|
47
85
|
export function divide(num1: numType, num2: numType) {
|
|
48
86
|
try {
|
|
49
|
-
const _num1 = Number(num1);
|
|
50
|
-
const _num2 = Number(num2);
|
|
51
|
-
|
|
87
|
+
const _num1 = num1 == null ? NaN : Number(num1);
|
|
88
|
+
const _num2 = num2 == null ? NaN : Number(num2);
|
|
89
|
+
// 如果两个数都是非数字 那么直接返回0
|
|
90
|
+
const result = initNum(_num1, _num2);
|
|
91
|
+
if (typeof result === "number") {
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
Decimal.set({ precision: 5 });
|
|
95
|
+
return new Decimal(_num1).div(_num2).toNumber();
|
|
96
|
+
// return (isNaN(_num1) ? 0 : _num1) / (isNaN(_num2) ? 0 : _num2);
|
|
52
97
|
} catch (err) {
|
|
53
98
|
console.error("计算除法出错", err);
|
|
54
99
|
return 0;
|
|
@@ -60,7 +105,13 @@ export function divide(num1: numType, num2: numType) {
|
|
|
60
105
|
*/
|
|
61
106
|
export function exactRound(num: numType, ratio: number) {
|
|
62
107
|
try {
|
|
63
|
-
|
|
108
|
+
const _num1 = num == null ? NaN : Number(num);
|
|
109
|
+
if (isNaN(_num1)) {
|
|
110
|
+
return 0;
|
|
111
|
+
// return (0).toFixed(ratio);
|
|
112
|
+
}
|
|
113
|
+
return new Decimal(num).toFixed(ratio);
|
|
114
|
+
// return Number(num).toFixed(ratio);
|
|
64
115
|
} catch (err) {
|
|
65
116
|
return num;
|
|
66
117
|
}
|
package/src/test.js
CHANGED
|
@@ -47,8 +47,106 @@ function dangerouslySetXss1(str) {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
console.log(unescapeString("&&&啊你"));
|
|
50
|
+
// console.log(unescapeString("&&&啊你"));
|
|
51
51
|
|
|
52
|
-
console.log(dangerouslySetXss("&&&啊你"));
|
|
52
|
+
// console.log(dangerouslySetXss("&&&啊你"));
|
|
53
53
|
|
|
54
|
-
console.log(dangerouslySetXss1("&&&啊你"));
|
|
54
|
+
// console.log(dangerouslySetXss1("&&&啊你"));
|
|
55
|
+
|
|
56
|
+
// functions and constants
|
|
57
|
+
// console.log("functions and constants");
|
|
58
|
+
// print(math.round(math.e, 3)); // 2.718
|
|
59
|
+
// print(math.atan2(3, -3) / math.pi); // 0.75
|
|
60
|
+
// print(math.log(10000, 10)); // 4
|
|
61
|
+
// print(math.sqrt(-4)); // 2i
|
|
62
|
+
// print(
|
|
63
|
+
// math.pow(
|
|
64
|
+
// [
|
|
65
|
+
// [-1, 2],
|
|
66
|
+
// [3, 1],
|
|
67
|
+
// ],
|
|
68
|
+
// 2
|
|
69
|
+
// )
|
|
70
|
+
// ); // [[7, 0], [0, 7]]
|
|
71
|
+
// print(math.derivative("x^2 + x", "x")); // 2 * x + 1
|
|
72
|
+
// console.log();
|
|
73
|
+
|
|
74
|
+
// // expressions
|
|
75
|
+
// console.log("expressions");
|
|
76
|
+
// print(math.evaluate("1.2 * (2 + 4.5)")); // 7.8
|
|
77
|
+
// print(math.evaluate("12.7 cm to inch")); // 5 inch
|
|
78
|
+
// print(math.evaluate("sin(45 deg) ^ 2")); // 0.5
|
|
79
|
+
// print(math.evaluate("9 / 3 + 2i")); // 3 + 2i
|
|
80
|
+
// print(math.evaluate("det([-1, 2; 3, 1])")); // -7
|
|
81
|
+
// print(math.evaluate("NaN + '0.2'")); // -7
|
|
82
|
+
// console.log();
|
|
83
|
+
|
|
84
|
+
// // chained operations
|
|
85
|
+
// console.log("chained operations");
|
|
86
|
+
// const a = math.chain(3).add(4).multiply(2).done();
|
|
87
|
+
// print(a); // 14
|
|
88
|
+
// console.log();
|
|
89
|
+
|
|
90
|
+
// // mixed use of different data types in functions
|
|
91
|
+
// console.log("mixed use of data types");
|
|
92
|
+
// print(math.add(4, [5, 6])); // number + Array, [9, 10]
|
|
93
|
+
// print(math.multiply(math.unit("5 mm"), 3)); // Unit * number, 15 mm
|
|
94
|
+
// print(math.subtract([2, 3, 4], 5)); // Array - number, [-3, -2, -1]
|
|
95
|
+
// print(math.add(math.matrix([2, 3]), [4, 5])); // Matrix + Array, [6, 8]
|
|
96
|
+
// console.log();
|
|
97
|
+
|
|
98
|
+
// /**
|
|
99
|
+
// * Helper function to output a value in the console. Value will be formatted.
|
|
100
|
+
// * @param {*} value
|
|
101
|
+
// */
|
|
102
|
+
// function print(value) {
|
|
103
|
+
// const precision = 14;
|
|
104
|
+
// console.log(math.format(value, precision));
|
|
105
|
+
// }
|
|
106
|
+
|
|
107
|
+
// const math = require("mathjs");
|
|
108
|
+
// function print(value) {
|
|
109
|
+
// const precision = 14;
|
|
110
|
+
// console.log(math.format(value, precision));
|
|
111
|
+
// }
|
|
112
|
+
|
|
113
|
+
// console.log(performance.now());
|
|
114
|
+
// console.log(math.add(0.1, 0.2));
|
|
115
|
+
// console.log(performance.now());
|
|
116
|
+
// console.log(performance.now());
|
|
117
|
+
// console.log(0.1 + 0.2);
|
|
118
|
+
// console.log(performance.now());
|
|
119
|
+
// const BigNumber = require("bignumber.js");
|
|
120
|
+
// console.log(performance.now());
|
|
121
|
+
// const num1 = new BigNumber(0.1);
|
|
122
|
+
// const num2 = new BigNumber(0.2);
|
|
123
|
+
|
|
124
|
+
// const result = num1.plus(num2);
|
|
125
|
+
|
|
126
|
+
// console.log(result.toString());
|
|
127
|
+
|
|
128
|
+
// console.log(performance.now());
|
|
129
|
+
|
|
130
|
+
const Decimal = require("decimal.js");
|
|
131
|
+
|
|
132
|
+
const num1 = new Decimal("0.1");
|
|
133
|
+
|
|
134
|
+
console.log(num1.add(0.21).toNumber(), typeof num1.add(0.21).toNumber());
|
|
135
|
+
|
|
136
|
+
console.log(num1.sub(0.21).toNumber(), typeof num1.sub(0.21).toNumber());
|
|
137
|
+
|
|
138
|
+
console.log(num1.mul(0.21).toNumber(), typeof num1.mul(0.21).toNumber());
|
|
139
|
+
Decimal.set({ precision: 5 });
|
|
140
|
+
console.log(
|
|
141
|
+
new Decimal(1).div(1.1).toNumber(),
|
|
142
|
+
typeof new Decimal(1).div(1.1).toNumber()
|
|
143
|
+
);
|
|
144
|
+
console.log(
|
|
145
|
+
new Decimal(1).dividedBy(1.1).toNumber(),
|
|
146
|
+
typeof new Decimal(1).dividedBy(1.1).toNumber()
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
console.log(
|
|
150
|
+
Number(new Decimal(1.1).toFixed(2)),
|
|
151
|
+
typeof Number(new Decimal(1.1).toFixed(2)).toFixed(2)
|
|
152
|
+
);
|