ice.fo.utils
Version:
26 lines (19 loc) • 688 B
JavaScript
/**
* Parse inline styles string to object
*
* Ex: "display: block; width: 100%;" => { display: 'block'; width: '100%' }
* @param {*} text
* @returns
*/
export default function convertToStylesObject(text) {
if (!text || !text.split) { return {}; }
text = text.replace(/(?:\r\n|\r|\n)/g, ';');
text = text.replace(/\{|\}/g, '');
const result = text.split(';').filter((i) => !!i.trim() && i.includes(':')).reduce((ruleMap, ruleString) => {
const [left, right] = ruleString.split(':');
const attribute = left.trim().replace(/-(.)/g, (match, group1) => group1.toUpperCase());
ruleMap[attribute] = right.trim();
return ruleMap;
}, {});
return result;
}