Skip to content

Commit

Permalink
fix: cast token to string
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed May 9, 2020
1 parent a1bbc60 commit 12c85ab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/core/flat-tokens.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { withPrefix } from './with-prefix'
import { Shape, FlattenToken, Token, TokensMap } from './types'
import { Shape, FlattenToken, Token, TokensMap, TokenType } from './types'

function getTokenType(value: string): Token['type'] {
function getTokenType(value: string | number): TokenType {
// TODO: Order is necessary, cuz color maybe has percent.
if (value.match(/#|rgb|hsl/) !== null) {
if (value.toString().match(/#|rgb|hsl/) !== null) {
return 'color'
}
if (value.match(/px|%|em|rem/) !== null) {
if (value.toString().match(/px|%|em|rem/) !== null) {
return 'size'
}
return 'unknown'
}

function isTokensMap(token: Token | TokensMap): token is TokensMap {
function isTokensMap(token: TokensMap | Token): token is TokensMap {
return token.value === undefined
}

Expand All @@ -26,7 +26,7 @@ export function flatTokens(tokens: TokensMap, prefix?: string): Shape<FlattenTok
for (const key in tokens) {
const computedKey = withPrefix(key, prefix)
const token = tokens[key]
if (typeof token === 'string') {
if (typeof token === 'string' || typeof token === 'number') {
result[computedKey] = {
value: token,
type: getTokenType(token),
Expand Down
4 changes: 3 additions & 1 deletion src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ export type Shape<T> = {

export type Primitive = string | number

export type TokenType = 'color' | 'size' | 'unknown'

export type Token = {
value: Primitive
type: 'color' | 'size' | 'unknown'
type: TokenType
comment?: string
}

Expand Down

0 comments on commit 12c85ab

Please sign in to comment.