Skip to content

Commit

Permalink
refactor(bezier-vscode): separate assignToTokenMap function
Browse files Browse the repository at this point in the history
  • Loading branch information
yangwooseong committed Nov 11, 2024
1 parent 209f8c1 commit c22fb56
Showing 1 changed file with 31 additions and 35 deletions.
66 changes: 31 additions & 35 deletions packages/bezier-vscode/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,41 @@ import { TextDocument } from 'vscode-languageserver-textdocument'

import { deepMerge, hexToRGBA } from './utils'

const alphaTokenMap = {} as Record<string, Record<string, string>>

Object.entries(alphaTokens.lightTheme).forEach(([key, value]) => {
if (alphaTokenMap[key] === undefined) {
alphaTokenMap[key] = {}
}
Object.entries(value).forEach(([token, valueRefObject]) => {
alphaTokenMap[key][token] = valueRefObject.value
})
})
type TokenMap = Record<
string,
Record<string, string | number | Record<string, string | number>>
>

Object.entries(alphaTokens.global).forEach(([key, value]) => {
if (alphaTokenMap[key] === undefined) {
alphaTokenMap[key] = {}
}
Object.entries(value).forEach(([token, valueRefObject]) => {
alphaTokenMap[key][token] = valueRefObject.value
const assignToTokenMap = (
target: TokenMap,
source: TokenMap,
fn: Function = (v: Record<string, string> | string) => v
) => {
Object.entries(source).forEach(([key, value]) => {
if (target[key] === undefined) {
target[key] = {}
}
Object.entries(value).forEach(([token, tokenValue]) => {
target[key][token] = fn(tokenValue)
})
})
})
}

const tokenMap = {} as Record<string, Record<string, string>>
const alphaTokenMap = {} as TokenMap
const tokenMap = {} as TokenMap

Object.entries(tokens.lightTheme).forEach(([key, value]) => {
if (tokenMap[key] === undefined) {
tokenMap[key] = {}
}
Object.entries(value).forEach(([token, value]) => {
tokenMap[key][token] = value
})
})

Object.entries(tokens.global).forEach(([key, value]) => {
if (tokenMap[key] === undefined) {
tokenMap[key] = {}
}
Object.entries(value).forEach(([token, value]) => {
tokenMap[key][token] = value
})
})
assignToTokenMap(
alphaTokenMap,
alphaTokens.lightTheme,
(v: Record<string, string>) => v.value
)
assignToTokenMap(
alphaTokenMap,
alphaTokens.global,
(v: Record<string, string>) => v.value
)
assignToTokenMap(tokenMap, tokens.lightTheme)
assignToTokenMap(tokenMap, tokens.global)

const allTokenMap = deepMerge(alphaTokenMap, tokenMap) as Record<
| keyof typeof alphaTokens.global
Expand Down

0 comments on commit c22fb56

Please sign in to comment.