TkAstral3D/packages/sdk/lib/utils/common/helper.ts
2025-10-04 23:36:07 +08:00

37 lines
831 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

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.

/**
* 递归访问嵌套属性
* @param {object} obj
* @param {string} path 属性路径字符串eg: "a.b.c"
*/
export function getNestedProperty(obj:object, path:string):any {
return path.split('.').reduce((o, key) => o?.[key], obj);
}
/**
* 转义正则特殊字符
* @param {string} str
*/
export function escapeRegExp(str:string) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
/**
* 获取系统主题色
*/
export function getOsTheme(){
const isDarkTheme = window.matchMedia("(prefers-color-scheme: dark)"); // 是深色
if (isDarkTheme.matches) {
return 'dark';
} else {
return "light";
}
}
/**
* 获取rem的px值
*/
export function remToPxNumber(rem: number): number {
const f = parseFloat(document.documentElement.style.fontSize);
return f * rem;
}