Skip to content

Instantly share code, notes, and snippets.

@niksumeiko
Created June 30, 2015 08:32
Show Gist options
  • Save niksumeiko/f1853d62c042903e3835 to your computer and use it in GitHub Desktop.
Save niksumeiko/f1853d62c042903e3835 to your computer and use it in GitHub Desktop.
Converts style object into the corresponding CSS `style` attribute value
/**
* Takes a style object and returns the corresponding
* attribute value. Converts camel case property names
* to proper CSS selector names.
* @param {Object} obj Map of CSS properties to values.
* @return {string} The style attribute value.
*/
function toStyleAttribute = function(obj) {
return Object.keys(obj).map(function(key) {
// Camel case property names to CSS selector names.
return (key.replace(/([A-Z])/g, '-$1').toLowerCase()) +
':' + obj[key];
}).join(';');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment