/** * 中划线格式 -转-> 驼峰格式 * @param name 原名称 * @return 转换后的名称 */ export const kebab2Camel = (name: string) => name.replace(/(?:^|-)([a-zA-Z0-9])/g, (m, $1) => $1.toUpperCase()); /** * 驼峰格式 -转-> 中划线格式 * @param name 原名称 * @return 转换后的名称 */ export const Camel2kebab = (name: string) => name.replace(/([A-Z]|[0-9]+)/g, (m, $1, offset) => (offset ? '-' : '') + $1.toLowerCase()); /** * 首字母大写 * @param value 字符串 */ export const firstUpperCase = (value: string) => value.replace(/^\S/, (letter) => letter.toUpperCase()); export const firstLowerCase = (value: string) => value.replace(/^\S/, (letter) => letter.toLowerCase()); /** * @param key 键 * @param set 数据集合,Array | Map | Set * @param start 数字起始 */ export function unique(key: string, set: Set | Map | Array | { [name: string]: string }, start: number = 1) { const has = (_key: string) => { if (set instanceof Set || set instanceof Map) return set.has(_key); else if (Array.isArray(set)) return set.includes(_key); else return set[_key]; }; while (has(key)) key = key.replace(/\d*$/, (m) => String(m === '' ? start : +m + 1)); return key; } export const getLastOfPath = (path: string) => { const arr = path.split('/'); return arr[arr.length - 1]; }; /** * 切出标签内的内容 * @param source 原始内容 * @param tag 标签 * @param attrs 属性匹配 */ export const sliceTagContent = (source: string, tag: string, attrs: string = '') => { const reg = new RegExp(`<${tag}${attrs ? ' ' + attrs : ''}.*?>([\\s\\S]+)<\\/${tag}>`); const m = source.match(reg); return m ? m[1].replace(/^\n+/, '') : ''; };