Last active
September 2, 2022 14:33
-
-
Save Sejmou/1c015c519b9a3ac65043b083b6bf468c to your computer and use it in GitHub Desktop.
Random JS helper functions
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
// https://stackoverflow.com/a/23013726/13727176 | |
function swap(obj) { | |
const ret = {}; | |
for (const key in obj) { | |
ret[json[key]] = key; | |
} | |
return ret; | |
} |
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
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