Skip to content

Instantly share code, notes, and snippets.

@Sejmou
Last active September 2, 2022 14:33
Show Gist options
  • Save Sejmou/1c015c519b9a3ac65043b083b6bf468c to your computer and use it in GitHub Desktop.
Save Sejmou/1c015c519b9a3ac65043b083b6bf468c to your computer and use it in GitHub Desktop.
Random JS helper functions
// https://stackoverflow.com/a/23013726/13727176
function swap(obj) {
const ret = {};
for (const key in obj) {
ret[json[key]] = key;
}
return ret;
}
function convertStylesObjToCSSString(
stylesObj,
propsAreClassNames = false,
camelToKebap = true
) {
return Object.entries(stylesObj)
.map(([k, v]) => {
if (camelToKebap) {
k = k.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`);
}
if (propsAreClassNames && !k.startsWith('.')) {
k = '.' + k;
}
if (typeof v === 'object') {
// key is CSS selector, value are its props (JS object!)
v = convertStylesObjToCSSString(v);
return `${k} {${v}}`;
}
return `${k}:${v}`;
})
.join(';');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment