string-process.js
969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* 字符串处理
*/
'use strict';
const _ = require('lodash');
const urlencode = require('urlencode');
const logger = global.yoho.logger;
/**
* [判断数字]
* @param {[string]} str [验证参数]
* @return {[Boolean]}
*/
const isNumeric = (str) => {
return /^\d+(\.\d+)?$/.test(str);
};
/**
* 参数过滤
* @param {*} param
*/
const paramsFilter = (param) => {
if (param) {
return _.escape(param);
} else {
return param;
}
};
/**
* 解决 decodeURIComponent 字符串编码不同的工具
*/
const decodeURIComponent = (param) => {
let result = '';
try {
result = urlencode.decode(param);
} catch (e) {
try {
result = urlencode.decode(param, 'gbk');
} catch (e2) {
logger.error(`decodeURIComponent param:${e2},url:${param}`);
}
}
return result || '';
};
module.exports = {
isNumeric,
paramsFilter,
decodeURIComponent
};