Last active
February 15, 2019 00:56
-
-
Save uchcode/446aeba0f160f0231fcabd908e490715 to your computer and use it in GitHub Desktop.
css converter
This file contains hidden or 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
/* | |
"background-color:red;font-size:32px;" | |
{backgroundColor:"red",fontSize:"32px"} | |
[{backgroundColor:"red"},{fontSize:"32px"}] | |
*/ | |
const kebab_case = s => s.replace(/[A-Z]/g,"-$&").toLowerCase(); | |
const camel_case = s => s.replace(/(\-)([a-z])/g,(...a)=>a[2].toUpperCase()); | |
const CASCADE = {}; | |
// string -> array | |
CASCADE.split = (s='') => s.split(';').map(e=>e.trim()).filter(e=>e).map(e=>e.split(':').reduce((a,b)=>({[camel_case(a)]:b}))); | |
// array -> string | |
CASCADE.join = (a=[]) => a.length?a.map(e=>Object.keys(e).map(k=>`${kebab_case(k)}:${e[k]}`)).join(';')+';':''; | |
// object -> string | |
CASCADE.stringify = (o={}) => Object.keys(o).map(k=>`${kebab_case(k)}:${o[k]};`).join(''); | |
// string -> object | |
CASCADE.parse = (s='') => CASCADE.split(s).reduce((a,b)=>Object.assign(a,b),{}); | |
// object -> array | |
CASCADE.entries = (o={}) => Object.entries(o).map(([k,v])=>({[k]:v})); | |
// array -> object | |
CASCADE.merge = (a=[]) => a.reduce((a,b)=>Object.assign(a,b),{}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment