diff --git a/src/convertToObject.js b/src/convertToObject.js index 44d498c1d..7359c5b3a 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -3,16 +3,30 @@ /** * Implement convertToObject function: * - * Function takes a string with styles (see an example in [stylesString.js](./stylesString.js)) +* Function takes a string with styles +(see an example in [stylesString.js](./stylesString.js)) * and returns an object where CSS properties are keys - * and values are the values of related CSS properties (see an exampl in [test file](./convertToObject.test.js)) + * and values are the values of related CSS properties + * (see an exampl in [test file](./convertToObject.test.js)) * * @param {string} sourceString * * @return {object} */ function convertToObject(sourceString) { - // write your code here + const result = {}; + const regex = /([^:;]+):([^;]+)/g; + const matches = sourceString.match(regex); + + if (matches) { + matches.forEach(pair => { + const [key, value] = pair.split(':'); + + result[key.trim()] = value.trim(); + }); + } + + return result; } module.exports = convertToObject;