-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.ts
44 lines (37 loc) · 1.11 KB
/
lib.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
type ColorHex = `#${string}`
type CSSValue = string | number | ColorHex
interface ITokensSchema {
[key: string | number]: CSSValue | ITokensSchema
}
type Param<T> = T extends ITokensSchema
? keyof T extends infer K
? K extends string | number | `${number}`
? T[K] extends ITokensSchema
? `${K}.${Param<T[K]>}`
: K | `${K}`
: never
: never
: never
type ThemeFactory<T extends ITokensSchema> = (param: Param<T>) => CSSValue | unknown
const CACHE = new Map()
const get = (
config: ITokensSchema | CSSValue,
path: string | undefined,
): ITokensSchema | CSSValue => {
const pathArray = path ? path.split('.') : []
let obj = config
for (let p = 0; p < pathArray.length; p++) {
obj = (obj as ITokensSchema)[pathArray[p]]
if (obj === undefined) return ''
}
return obj
}
export const createTheme = <T extends ITokensSchema>(config: T): ThemeFactory<T> => {
return (param) => {
const paramStr = param.toString()
if (CACHE.has(paramStr)) return CACHE.get(paramStr)
const result = get(config, paramStr)
CACHE.set(paramStr, result)
return result
}
}