Skip to content

Instantly share code, notes, and snippets.

@tclain
Created May 11, 2018 22:04
Show Gist options
  • Save tclain/f2b2105a4e14b014d3a0ba3a24c7bd03 to your computer and use it in GitHub Desktop.
Save tclain/f2b2105a4e14b014d3a0ba3a24c7bd03 to your computer and use it in GitHub Desktop.
css string to styled components theme
import flow from 'lodash.flow';
import set from 'lodash.set';
export const cssString = `
:root {
--root: #a;
--sub-theme__variable: #a;
--sub-theme__variable2: #a1;
--sub-theme__variable3__a2: #cecea2;
--sub-theme__variable3__a3: #cecea3;
}
`
export const cssToTheme = flow(
str => str.replace(/[\s\n\t]+/g, ''),
str => /:root.*\{(.*)\}/gm.exec(str)[1],
str => str.split(';'),
arr => arr.filter(el => el !== ''),
arr => arr.map(flow(
value => value.split(':'),
([key, value ]) => [key.replace('--', ''), value],
([key, value ]) => [key.replace(/__/g, '.'), value]
)),
arr => arr.reduce ((object, [key, value]) => {
set(object, key, value)
return object;
}
,{})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment