string-process.js 963 Bytes
/**
 * 字符串处理
 */

'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
};