UNPKG

d-utils

Version:

d-utils

26 lines (25 loc) 1.1 kB
/** * d-utils version: 4.0.3 * by ifmiss */ import{GenericType as e}from"./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) */export default function(r,t){if(void 0===t&&(t=e.StrTrimType.LEFT_RIGHT),"string"==typeof r)switch(t){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 => ")}