export interface ISN { /** 简称 */ shorthand?: string; /** 全称 */ full: string } /** 拆分全称加简称 */ export const getSN = (value?: string): ISN | undefined => { if (!value) return undefined; const arr = value.match(/\*[^*]+\*/); if (arr && arr[0]) { return { shorthand: arr[0].split('*')[1], full: value.replace(arr[0], '') }; } else { return { shorthand: undefined, full: value }; } }; /** 组合全称加简称 */ export const getItemName = (value: ISN) => { if (value.shorthand) { return `*${value.shorthand}*${value.full}`; } else { return value.full; } } /** 设置全称 */ export const setFull = (name?:string, full?:string) => { const sn = getSN(name); if(!sn) return full; sn.full = full || ''; return getItemName(sn); } /** 设置简称 */ export const setShorthand = (name?:string, shorthand?:string) => { if(!name) return undefined const sn = getSN(name); if(!sn) return undefined; sn.shorthand = shorthand || ''; return getItemName(sn); } /** 组合全称加简称 */ export const getItemNameWithShorthand = (value: ISN) => { if (value.shorthand) { return `*${value.shorthand}*${value.full}`; } else { return value.full; } }