Files
MetonaToast/src/utils.ts
T
tianhao 5f8b9d2f75
CI / test (18.x) (push) Successful in 10m8s
CI / test (20.x) (push) Successful in 10m10s
CI / test (22.x) (push) Successful in 10m4s
CI / test (24.x) (push) Successful in 10m2s
chore: 版本回退至 0.5.0 并重建产物(含 CJS 兼容修复)
2026-08-08 15:40:04 +08:00

41 lines
957 B
TypeScript

/**
* MetonaToast Utils — 工具函数
* @module utils
* @version 0.5.0
*/
/**
* 生成唯一ID
*/
export const generateId = (): string => {
return 'met-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 8);
};
/**
* HTML转义
* @param s - 输入字符串
*/
export const escapeHTML = (s: string | null | undefined): string => {
if (typeof document === 'undefined') {
return String(s == null ? '' : s)
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}
const div = document.createElement('div');
div.textContent = String(s == null ? '' : s);
return div.innerHTML;
};
/**
* 检测暗色模式偏好
*/
export const prefersDark = (): boolean => {
return typeof window !== 'undefined' &&
typeof window.matchMedia === 'function' &&
window.matchMedia('(prefers-color-scheme: dark)').matches;
};