26 lines
909 B
TypeScript
26 lines
909 B
TypeScript
// Helper function to generate conditional values based on dark mode
|
|
export function darkMode<T>(isDark: boolean, darkValue: T, lightValue: T): T {
|
|
return isDark ? darkValue : lightValue;
|
|
}
|
|
/**
|
|
* 将驼峰命名转换为kebab-case
|
|
*/
|
|
export function toKebabCase(str: string): string {
|
|
return str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase()
|
|
}
|
|
/**
|
|
* 将嵌套对象扁平化,使用点号连接键名
|
|
*/
|
|
export function flattenObject(obj: Record<string, any>, prefix = ''): Record<string, string> {
|
|
return Object.keys(obj).reduce((acc: Record<string, string>, k: string) => {
|
|
const pre = prefix.length ? prefix + '.' : ''
|
|
|
|
if (typeof obj[k] === 'object' && obj[k] !== null && !Array.isArray(obj[k])) {
|
|
Object.assign(acc, flattenObject(obj[k], pre + k))
|
|
} else {
|
|
acc[pre + k] = obj[k].toString()
|
|
}
|
|
|
|
return acc
|
|
}, {})
|
|
} |