d-utils
Version:
d-utils
26 lines (25 loc) • 1.11 kB
JavaScript
/**
* d-utils version: 4.0.3
* by ifmiss
*/
;var e=require("./type.js");
/**
* @description 字符串的去除空格
* @param { String } str 操作的字符串
* @param { Number } type 类型 0: 去除首位空格;1: 去除所有空格; 2: 去除左边空格; 3: 去除右边空格; 默认为去除首位空格
* @return { String } 返回操作之后的字符串
* @example
* const str = ' d -js- ut ils '
* // 0: 去除首尾空格 默认为0
* strTrim(str)
* strTrim(str, 0)
* @example
* // 1: 去除所有空格
* strTrim(str, 1)
* @example
* // 2: 去除左边空格
* strTrim(str, 2)
* @example
* // 3: 去除右边空格
* strTrim(str, 3)
*/module.exports=function(r,s){if(void 0===s&&(s=e.GenericType.StrTrimType.LEFT_RIGHT),"string"==typeof r)switch(s){case 0:return r.replace(/(^\s*)|(\s*$)/g,"");case 1:return r.replace(/\s/g,"");case 2:return r.replace(/(^\s*)/g,"");case 3:return r.replace(/(\s*$)/g,"");default:return r.replace(/(^\s*)|(\s*$)/g,"")}else console.error("str must be string but found "+typeof r,"[d-utils] GenericUtils strTrim error => ")};