web-report/utils/strings.ts
2023-08-21 15:36:10 +08:00

36 lines
931 B
TypeScript

export function countString(str: string) {
str = str.replace(/\n/, '')
const chinese = Array.from(str)
.filter(ch => /[\u4e00-\u9fa5]/.test(ch))
.length
const english = Array.from(str)
.map(ch => /[a-zA-Z0-9\s]/.test(ch) ? ch : '')
.join('').length//.split(/\s+/).filter(s => s)
return chinese + english
}
export function formatFileSize(bytes: number) {
if (bytes <= 0) return '0KB';
const units = ['KB', 'MB', 'GB', 'TB'];
let i = 0;
while (bytes >= 1024 && i < units.length - 1) {
bytes /= 1024;
i++;
}
return bytes.toFixed(2) + units[i];
}
const REGEX = {
phone: /^(\d{3})\d{4}(\d{4})$/
}
export function hidePhone(phone?: string | null) {
if (!phone || phone.length < 11) return phone;
return phone.replace(REGEX.phone, '$1****$2')
}
export function isPhone(phone?: string) {
return phone && REGEX.phone.test(phone)
}