ai-digital-live/src/util/strings.ts

103 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime"
import {padStart} from "lodash";
dayjs.extend(relativeTime)
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];
}
export function formatWordCount(count: number) {
if (count < 10000) {
return count;
}
return (count / 10000).toFixed(2).replace(/.[0]+$/g,'') + '万'
}
const REGEX = {
phone: /^(\d{3})\d{4}(\d{4})$/
}
export function hidePhone(phone?: string | null) {
if (!phone || !REGEX.phone.test(phone)) return phone;
return phone.replace(REGEX.phone, '$1****$2')
}
export function isPhone(phone?: string) {
return phone && REGEX.phone.test(phone)
}
function getDayjs(time:any){
const str = time.toString();
if(str.length == 10){
time *= 1000;
}
return dayjs(time);
}
export function formatTime(time: any, template: 'min' | 'date' | string = 'YYYY-MM-DD HH:mm:ss') {
if (!time) return '-';
if (template == 'min') {
template = 'YYYY-MM-DD HH:mm'
} else if (template == 'date') {
template = 'YYYY-MM-DD'
}
return getDayjs(time).format(template)
}
export function timeFromNow(time: any) {
if(!time) return '';
return getDayjs(time).fromNow();
}
export function calcContentLengthLikeWord(str:string) {
try {
//先将回车换行符做特殊处理
// eslint-disable-next-line no-irregular-whitespace
str = str.replace(/(\r\n+|\s+| +)/g, "龘");
// eslint-disable-next-line no-control-regex
str = str.replace(/[\x00-\xff]/g, "m");
//合并字符m连续字母、数字、英文符号视为一个单词
str = str.replace(/m+/g, "*");
//去掉回车换行符
str = str.replace(/龘+/g, "");
//返回字数
return str.length;
} catch (e) {
return str.length
}
}
// 将时长转换成 时:分:秒
export function formatDuration(duration: number) {
if(duration < 0 || isNaN(duration)){
return '00:00:00';
}
duration = Math.ceil(duration);
const hour = Math.floor(duration / 3600);
const minute = Math.floor((duration - hour * 3600) / 60);
const second = duration - hour * 3600 - minute * 60;
// 需要补0
return padStart(hour.toString(), 2, '0') + ':' + padStart(minute.toString(), 2, '0') + ':' + padStart(second.toString(), 2, '0')
// return `${hour}:${minute}:${second}`
}