Parse html style string for a react component
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 parseStyles | |
* Parses a string of inline styles into a javascript object with casing for react | |
* | |
* @param {string} styles | |
* @returns {Object} | |
*/ | |
const parseStyles = styles => styles | |
.split(';') | |
.filter(style => style.split(':')[0] && style.split(':')[1]) | |
.map(style => [ | |
style.split(':')[0].trim().replace(/-./g, c => c.substr(1).toUpperCase()), | |
style.split(':')[1].trim() | |
]) | |
.reduce((styleObj, style) => ({ | |
...styleObj, | |
[style[0]]: style[1], | |
}), {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code will significantly improve the performance :