staff_data/packages/config/src/utils.ts

26 lines
909 B
TypeScript
Raw Normal View History

2025-02-06 16:32:31 +08:00
// 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
}, {})
}