import pinyin from 'pinyin';
|
|
/**
|
* 计算某个类名元素的可用高度(视口高度 - 元素顶部距离)
|
* @param className 元素类名(不带.)
|
* @returns 可用高度(px),找不到元素则返回 0
|
*/
|
export function getAvailableHeightByClass(className: string): number {
|
const el = document.querySelector(`.${className}`) as HTMLElement | null
|
if (!el) return 0
|
const rect = el.getBoundingClientRect()
|
return window.innerHeight - rect.top
|
}
|
|
/**
|
* 将字符串转为数字
|
* @param value
|
* @returns
|
*/
|
export function tryConvertToInt(value: string): string | number {
|
const num = Number(value);
|
// 判断是否是有效数字且是整数(小数部分为 0)
|
if (!isNaN(num) && Number.isInteger(num)) {
|
return num;
|
}
|
|
// 判断是否是有效数字且小数部分为 0(例如 "1.0")
|
if (!isNaN(num) && Number(value).toString().endsWith('.0')) {
|
return parseInt(value, 10);
|
}
|
|
return value;
|
}
|
/**
|
* 深拷贝
|
* @param obj
|
* @returns
|
*/
|
export function deepClone<T>(obj: T): T {
|
if (obj === null || typeof obj !== "object") return obj;
|
|
if (Array.isArray(obj)) {
|
return obj.map(item => deepClone(item)) as unknown as T;
|
}
|
|
const result: any = {};
|
for (const key in obj) {
|
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
result[key] = deepClone(obj[key]);
|
}
|
}
|
return result;
|
}
|
|
/**
|
* 姓名脱敏:第二个字变成首字母大写
|
* @param name 患者姓名
|
* @returns 脱敏后的姓名
|
*/
|
export function maskName(name: string): string {
|
if (!name) return ''
|
|
const chars = Array.from(name)
|
if (chars.length < 2) return name
|
|
const secondChar = chars[1]
|
const [pinyinArr] = pinyin(secondChar, { style: pinyin.STYLE_FIRST_LETTER })
|
const masked = pinyinArr[0]?.toUpperCase?.() || '*'
|
|
chars[1] = masked
|
return chars.join('')
|
}
|
|
/**
|
* 延时函数
|
* @param {number} ms - 延时时间,单位毫秒
|
*/
|
export function delay(ms: number) {
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
}
|