@tmsfe/tms-core 0.0.18 → 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/package.json +1 -1
- package/src/index.js +6 -0
- package/src/numUtils.js +16 -0
- package/src/objUtils.js +21 -2
- package/src/request.js +3 -2
- package/src/stringUtils.js +2 -2
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -20,6 +20,9 @@ import {
|
|
|
20
20
|
isValidAuthCode,
|
|
21
21
|
roundStr,
|
|
22
22
|
} from './stringUtils';
|
|
23
|
+
import {
|
|
24
|
+
round,
|
|
25
|
+
} from './numUtils';
|
|
23
26
|
import {
|
|
24
27
|
formatTime,
|
|
25
28
|
formatTimeStr,
|
|
@@ -152,6 +155,9 @@ const api = {
|
|
|
152
155
|
isValidAuthCode,
|
|
153
156
|
roundStr,
|
|
154
157
|
|
|
158
|
+
/* 数字方法 */
|
|
159
|
+
round,
|
|
160
|
+
|
|
155
161
|
/* 时间方法 */
|
|
156
162
|
formatTime,
|
|
157
163
|
formatTimeStr,
|
package/src/numUtils.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { roundStr } from './stringUtils';
|
|
2
|
+
/**
|
|
3
|
+
* 四舍五入(支持保留n位小数,n>=0)
|
|
4
|
+
* @param {any} x 原数字
|
|
5
|
+
* 如果n不是合法数字或者无法转换为合法数字,round结果返回NaN
|
|
6
|
+
* @param {any} n 保留几位小数,默认0
|
|
7
|
+
* 如果n不是合法数字或者无法转换为合法数字,round结果返回NaN
|
|
8
|
+
* 如果n小于0,round结果返回NaN
|
|
9
|
+
* 如果n的值包含小数部分,round处理时只关注n的整数部分值
|
|
10
|
+
* @return {number} 返回一个保留n位小数的数字,异常情况下可能是NaN
|
|
11
|
+
*/
|
|
12
|
+
const round = (x, n = 0) => parseFloat(roundStr(x, n, false));
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
round,
|
|
16
|
+
};
|
package/src/objUtils.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @param {Object} queryObj 需要进行序列化的对象
|
|
10
10
|
* @returns {String} 拼接后的字符串
|
|
11
11
|
*/
|
|
12
|
-
const serialize = (queryObj = {}) => {
|
|
12
|
+
export const serialize = (queryObj = {}) => {
|
|
13
13
|
if (!queryObj) {
|
|
14
14
|
return '';
|
|
15
15
|
}
|
|
@@ -23,4 +23,23 @@ const serialize = (queryObj = {}) => {
|
|
|
23
23
|
return queryArray.join('&');
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
export
|
|
26
|
+
export class JsonParseError extends Error {
|
|
27
|
+
constructor(text, data) {
|
|
28
|
+
super(text);
|
|
29
|
+
this.data = data;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 安全的JSON.parse
|
|
35
|
+
*/
|
|
36
|
+
export function safeJsonParse(data, throwErrIfParseFail = false) {
|
|
37
|
+
try {
|
|
38
|
+
return JSON.parse(data);
|
|
39
|
+
} catch (e) {
|
|
40
|
+
if (throwErrIfParseFail) {
|
|
41
|
+
throw new JsonParseError('JSON.parse error', data);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return data;
|
|
45
|
+
}
|
package/src/request.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import md5 from './md5';
|
|
12
12
|
import { getLogManager } from './log';
|
|
13
13
|
import { getEnvInfo, getAuthInfo } from './env';
|
|
14
|
+
import { safeJsonParse } from './objUtils';
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* 用于序列化需要签名的参数
|
|
@@ -254,7 +255,7 @@ export default class Request {
|
|
|
254
255
|
});
|
|
255
256
|
|
|
256
257
|
if (typeof res?.data === 'string') {
|
|
257
|
-
return
|
|
258
|
+
return safeJsonParse(res?.data);
|
|
258
259
|
}
|
|
259
260
|
return res?.data;
|
|
260
261
|
}
|
|
@@ -271,7 +272,7 @@ export default class Request {
|
|
|
271
272
|
const res = await this.createRequestTask(path, param, method, header);
|
|
272
273
|
|
|
273
274
|
if (typeof res?.data === 'string') {
|
|
274
|
-
return
|
|
275
|
+
return safeJsonParse(res?.data);
|
|
275
276
|
}
|
|
276
277
|
|
|
277
278
|
return res?.data;
|
package/src/stringUtils.js
CHANGED
|
@@ -113,8 +113,8 @@ const isValidPlate = (plate) => {
|
|
|
113
113
|
|
|
114
114
|
/**
|
|
115
115
|
* 四舍五入,并返回格式化的字符串
|
|
116
|
-
* 支持保留n位小数,n>=0,如
|
|
117
|
-
* 支持格式化字符串时取出末尾的0,如
|
|
116
|
+
* 支持保留n位小数,n>=0,如 roundStr(1.325, 2)=1.33
|
|
117
|
+
* 支持格式化字符串时取出末尾的0,如roundStr(1.109, 2, true)=1.1
|
|
118
118
|
* @param {any} x 原数字
|
|
119
119
|
* 如果n不是合法数字或者无法转换为合法数字,roundStr结果返回''
|
|
120
120
|
* @param {any} n 保留几位小数,默认0
|