Skip to content

Commit

Permalink
feat: impl flat tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Apr 21, 2020
1 parent 904e4a4 commit 3e397d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/core/flat-tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { withPrefix } from './with-prefix'
import { Shape, FlattenToken, Token, TokensMap } from './token.h'

/**
* Returns flatten shape with tokens.
*
* @param tokens Map with tokens.
*/
export function flatTokens(tokens: TokensMap, __prefix__?: string): Shape<FlattenToken> {
const result: Shape<FlattenToken> = {}
for (const key in tokens) {
const transformedKey = withPrefix(key, __prefix__)
const maybeToken = tokens[key]
if (maybeToken.value === undefined) {
Object.assign(result, flatTokens(maybeToken as TokensMap, transformedKey))
} else {
result[transformedKey] = {
...maybeToken as Token,
name: transformedKey,
}
}
}
return result
}
12 changes: 12 additions & 0 deletions src/core/with-prefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Returns string with prefix.
*
* @param value Raw string.
* @param prefix Prefix for string.
*/
export function withPrefix(value: string, prefix?: string): string {
if (prefix === undefined) {
return value
}
return `${prefix}_${value}`
}

0 comments on commit 3e397d9

Please sign in to comment.