Files
LTbinglingfeng cd44dca9c0 refactor(utils): share isRecord and getErrorMessage helpers
isRecord was declared locally in 15 modules (with two divergent shapes) and getErrorMessage in 7. Move a single canonical pair into utils/helpers and import it everywhere. The shared isRecord excludes arrays; the call sites that previously allowed them only read named properties, so behavior is unchanged.
2026-06-13 02:11:21 +08:00

28 lines
811 B
TypeScript

/**
* 辅助工具函数
* 从原项目 src/utils/array.js, dom.js, html.js 迁移
*/
/**
* 生成唯一 ID
*/
export function generateId(): string {
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
}
/**
* 判断是否为普通对象(排除 null 与数组)
*/
export const isRecord = (value: unknown): value is Record<string, unknown> =>
value !== null && typeof value === 'object' && !Array.isArray(value);
/**
* 从 unknown 错误中提取可读消息
*/
export const getErrorMessage = (error: unknown, fallback = ''): string => {
if (error instanceof Error) return error.message || fallback;
if (typeof error === 'string') return error || fallback;
if (isRecord(error) && typeof error.message === 'string') return error.message || fallback;
return fallback;
};