67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
const pattern
|
|
= /[a-zA-Z0-9_\u0392-\u03C9\u00C0-\u00FF\u0600-\u06FF\u0400-\u04FF]+|[\u4E00-\u9FFF\u3400-\u4DBF\uF900-\uFAFF\u3040-\u309F\uAC00-\uD7AF]+/g
|
|
|
|
export const countWord = (data: string)=> {
|
|
const m = data.match(pattern)
|
|
let count = 0
|
|
if (!m) {
|
|
return 0
|
|
}
|
|
for (let i = 0; i < m.length; i += 1) {
|
|
if (m[i].charCodeAt(0) >= 0x4E00) {
|
|
count += m[i].length
|
|
}
|
|
else {
|
|
count += 1
|
|
}
|
|
}
|
|
return count
|
|
}
|
|
|
|
export const formatNumberUnit = (num: number, fixed = 1) => {
|
|
if (typeof num !== 'number') {
|
|
num = parseFloat(num);
|
|
}
|
|
|
|
if (isNaN(num)) return '0';
|
|
|
|
const absNum = Math.abs(num);
|
|
let result;
|
|
|
|
if (absNum >= 100000000) {
|
|
// 亿
|
|
result = (num / 100000000).toFixed(fixed) + '亿';
|
|
} else if (absNum >= 10000) {
|
|
// 万
|
|
result = (num / 10000).toFixed(fixed) + '万';
|
|
} else {
|
|
result = num.toFixed(0);
|
|
}
|
|
|
|
// 移除多余的 .0
|
|
return result.replace(/\.0+($|亿|万)/, '$1');
|
|
}
|
|
|
|
/**
|
|
* 判断是否为移动端
|
|
*/
|
|
export const isMobile = (): boolean => {
|
|
if (typeof window === 'undefined') {
|
|
return false;
|
|
}
|
|
|
|
const userAgent = navigator.userAgent;
|
|
const mobileRegex = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
|
|
|
|
return mobileRegex.test(userAgent);
|
|
}
|
|
|
|
export const startBlogDate = new Date('2025-11-26');
|
|
|
|
export const getDaysDifference = (date1: Date, date2: Date): number => {
|
|
let d1 = new Date(date1);
|
|
let d2 = new Date(date2);
|
|
|
|
const diffMs = Math.abs(d2.getTime() - d1.getTime());
|
|
return Math.floor(diffMs / (1000 * 60 * 60 * 24));
|
|
} |