import __camelize from '../../string/camelize.js'; import __parse from '../../string/parse.js'; /** * @name styleString2Object * @namespace js.css.transform * @type Function * @platform js * @platform node * @status stable * * Transform a style string to an object representation * * @param {String} style The style string * @return (Object) The string object representation * * @todo tests * * @snippet __styleStringToObject($1) * * @example js * import { __styleStringToObject } from '@lotsof/sugar/css' * const styleString =  __styleStringToObject('padding-left:20px; display:block;'); * // output => { * // paddingLeft : '20px', * // display : 'block' * // } * * @since 1.0.0 * @author Olivier Bossel (https://lotsof.dev) */ export default function __styleStringToObject(style: string): any { if (!style || style === '') return {}; const obj = {}; const split = style.replace(/\s/g, '').split(';'); split.forEach((statement) => { // split statement by key value pairs const spl = statement.split(':'), key = __camelize(spl[0]), value = spl[1]; // add element into object obj[key] = __parse(value); }); // return the style object return obj; }