Skip to content

Instantly share code, notes, and snippets.

@alexsasharegan
Created August 17, 2016 03:54
Show Gist options
  • Save alexsasharegan/ce3a8ba4cb95d7fb6d4e775edea17ca0 to your computer and use it in GitHub Desktop.
Save alexsasharegan/ce3a8ba4cb95d7fb6d4e775edea17ca0 to your computer and use it in GitHub Desktop.
Get an inline style string by passing in a hash of camelCased style attributes.
//es5
function mapObjStyleAttrs(obj) {
return Object.keys(obj)
.map(function (key) {
return [key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(), obj[key]]
.join(':');
})
.join(';');
}
//es6
const mapObjStyleAttrs = (obj) => Object.keys(obj).map((key) => [key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(), obj[key]].join(':')).join(';');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment