import { FormatStr, ToCamelCase, FromCamelCase, Trim, EscapeHTML, UnescapeHTML, StripHTMLTags, SubGbString, GetGbLen, ChangeCase } from "../types"; /** * @fileOverview 字符串相关处理函数 * @module string */ /** * @description 格式化字符串 * @param {string} str 字符串 * @param {number} len 位数 * @param {string} char - 格式化字符 * @returns {string} */ export const formatStr: FormatStr = (str, len, char) => { let reg = new RegExp("\\d{1," + len + "}", "g"); return str.match(reg).join(char); }; /** * @description 字符串转换为驼峰 * @param {string} str 字符串 * @returns {string} */ export const toCamelCase: ToCamelCase = (str) => { return str.replace(/^([A-Z])|[\s-_]+(\w)/g, (match, p1, p2, offset) => p2 ? p2.toUpperCase() : p1.toLowerCase()); }; /** * @description 驼峰转字符串 * @param {string} str 字符串 * @param {string} separator 切割符 * @returns {string} */ export const fromCamelCase: FromCamelCase = (str, separator = '_') => { return str.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2') .replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2').toLowerCase(); }; /** * @description 去除空格 * @param {string} str 字符串 * @param {number} type 1-所有空格 2-前后空格 3-前空格 4-后空格 * @returns {string} */ export const trim: Trim = (str, type) => { type = type || 1; switch (type) { case 1: return str.replace(/\s+/g, ''); case 2: return str.replace(/(^\s*)|(\s*$)/g, ''); case 3: return str.replace(/(^\s*)/g, ''); case 4: return str.replace(/(\s*$)/g, ''); default: return str; } }; /** * @description 转义HTML字符串 * @param {string} str 字符串 * @returns {string} */ export const escapeHTML: EscapeHTML = (str) => { return str.replace( /[&<>'"]/g, tag => ({ '&': '&', '<': '<', '>': '>', "'": ''', '"': '"' }[tag] || tag) ); }; /** * @description 反转 escape * @param {string} str 字符串 * @returns {string} */ export const unescapeHTML: UnescapeHTML = (str) => { return str.replace( /&|<|>|'|"/g, tag => ({ '&': '&', '<': '<', '>': '>', ''': "'", '"': '"' }[tag] || tag) ); }; /** * @description 过滤html标签 * @param {string} str 字符串 * @returns {string} */ export const stripHTMLTags: StripHTMLTags = (str) => { return str.replace(/<[^>]*>/g, ''); }; /** * @description 截取字符,区分中英文,中文两字符,空格一字符 * @param {String} str - 字符串 * @param {Number} len - 字符串截取长度 * @returns {String} - 返回截取后字符 */ export const subGbString: SubGbString = (str, len) => { const regexp = /[^\x00-\xff]/g // 正则表达式匹配中文 // 当字符串字节长度小于指定的字节长度时 if (str.replace(regexp, 'aa').length <= len) { return str } // 假设指定长度内都是中文 const m = Math.floor(len / 2) for (let i = m, j = str.length; i < j; i++) { // 当截取字符串字节长度满足指定的字节长度 if (str.substring(0, i).replace(regexp, 'aa').length >= len) { return str.substring(0, i) } } }; /** * @description 判断字符长度,区分中英文,包括空格 * @param {String} str - 字符串 * @returns {Number} - 返回字符长度 */ export const getGbLen: GetGbLen = (str) => { let len = 0 const content = str || '' for (let i = 0; i < content.length; i++) { if (content.charCodeAt(i) > 127 || content.charCodeAt(i) === 94) { len += 2 } else { len++ } } return len }; /** * @description 字符串写法转换 * @param {string} str 字符串 * @param {number} type 1:首字母大写其他小写 2:首页母小写其他大写 3:大小写转换 4:全部大写 5:全部小写 * @returns {string} */ export const changeCase: ChangeCase = (str, type) => { type = type || 4; switch (type) { case 1: return str.replace(/\b\w+\b/g, function (word): string { return ( word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase() ); }); case 2: return str.replace(/\b\w+\b/g, function (word): string { return ( word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase() ); }); case 3: return str .split('') .map(function (word): string { if (/[a-z]/.test(word)) { return word.toUpperCase(); } else { return word.toLowerCase(); } }) .join(''); case 4: return str.toUpperCase(); case 5: return str.toLowerCase(); default: return str; } };