export function byteLength(str:string) { let b = 0; if (str.length) { for (let i = 0; i < str.length; i++) { if (str.charCodeAt(i) > 255) { b += 2; } else { b++; } } return b; } else { return 0; } } export function sliceStr(str:string, length = 24) { let a = ''; let b = ''; for (let i = 0; i <= str.length; i++) { if (byteLength(str.slice(0, i)) >= length) { a = str.slice(0, i - 1); b = str.slice(i - 1); return [a, b]; } } return [str.slice(0, str.length), str.slice(str.length)]; } export function addNames(list = [], name:string, needSplit = false) { const str = list && list.reduce((result, item) => (result + `${result ? ',' : ''}` + item[name]), ''); return str ? (needSplit ? `${str};` : str) : ''; } export function isOrderAdd(id:string) { return id.includes('_'); } export function handlePhone(str: string) { // @ts-ignore return str.match(/(\d{3})(\d{4})(\d{4})/) .slice(1) .reduce(function (value, item, index) { // 当index===1时,初始元素和当前元素累加并返回,value是初始值186,也是最终累加的返回值,item是当前索引下标是1的元素****。 return index === 1 ? value + '****' : value + item; }); }