-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import toColor from 'color' | ||
import { paramCase, constantCase } from 'change-case' | ||
|
||
import { withPrefix } from './with-prefix' | ||
import { Shape, FlattenToken } from './token.h' | ||
|
||
type Transform = { | ||
type: 'name' | 'value' | ||
matcher?: (token: FlattenToken) => boolean | ||
transformer: (tokenn: FlattenToken, options?: any) => string | ||
} | ||
|
||
export const transforms: Shape<Transform> = { | ||
'name.param': { | ||
type: 'name', | ||
// prettier-ignore | ||
transformer: (token, options) => | ||
paramCase(withPrefix(token.name, options.prefix)) | ||
}, | ||
'name.const': { | ||
type: 'name', | ||
// prettier-ignore | ||
transformer: (token, options) => | ||
constantCase(withPrefix(token.name, options.prefix)) | ||
}, | ||
'color.hex': { | ||
type: 'value', | ||
matcher: (token) => token.type === 'color', | ||
transformer: (token) => { | ||
const color = toColor(token.value) | ||
// prettier-ignore | ||
return (color as any).valpha < 1 | ||
? color.rgb().toString().toLowerCase() | ||
: color.hex().toString().toLowerCase() | ||
}, | ||
}, | ||
// TODO: Fix transform for multiply value, ex. `0 10px`. | ||
'size.px': { | ||
type: 'value', | ||
matcher: (token) => token.type === 'size', | ||
transformer: (token) => { | ||
if (token.value.toString().match(/px|%/)) { | ||
return `${token.value}` | ||
} | ||
return `${token.value}px` | ||
}, | ||
}, | ||
} | ||
|
||
// TODO: Use shape instead array for same struct as original. | ||
export function transformTokens(tokens: {}, options: any) { | ||
const result = [] | ||
for (const key in tokens) { | ||
const token = tokens[key] | ||
for (const transformKey of options.transforms) { | ||
const transform = transforms[transformKey] | ||
if (transform.type === 'name') { | ||
token.name = transform.transformer(token, options) | ||
} | ||
if (transform.type === 'value' && transform.matcher(token)) { | ||
token.value = transform.transformer(token, options) | ||
} | ||
} | ||
result.push(token) | ||
} | ||
return result | ||
} |