utils.ts 835 B

1234567891011121314151617181920
  1. import type { GlobalConfig } from '../share/types.ts';
  2. export function getEnumOptions<T extends string | number, M extends Record<T, string>>(enumObj: Record<string, T>, nameMap: M) {
  3. return Object.entries(enumObj)
  4. .filter(([_key, value]) => !isNaN(Number(value)) || typeof value === 'string') // 保留数字和字符串类型的值
  5. .filter(([key, _value]) => isNaN(Number(key))) // 过滤掉数字键(枚举的反向映射)
  6. .map(([_key, value]) => ({
  7. label: nameMap[value as T],
  8. value: value
  9. }));
  10. }
  11. /**
  12. * 获取全局配置项 (严格类型版本)
  13. * @param key 配置键名
  14. * @returns 配置值或undefined
  15. */
  16. export function getGlobalConfig<T extends keyof GlobalConfig>(key: T): GlobalConfig[T] | undefined {
  17. return (window as typeof window & { CONFIG?: GlobalConfig }).CONFIG?.[key];
  18. }