Skip to content

Instantly share code, notes, and snippets.

@uchcode
Last active February 15, 2019 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uchcode/446aeba0f160f0231fcabd908e490715 to your computer and use it in GitHub Desktop.
Save uchcode/446aeba0f160f0231fcabd908e490715 to your computer and use it in GitHub Desktop.
css converter
/*
"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