From d91e90cc9de3e5feaa9e3f3eef3b5e11d1a248bc Mon Sep 17 00:00:00 2001 From: Zyrak Nur Date: Wed, 1 Nov 2023 23:35:29 +0200 Subject: [PATCH 1/2] add task solution --- src/convertToObject.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/convertToObject.js b/src/convertToObject.js index 44d498c1d..c6d60a308 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -12,7 +12,31 @@ * @return {object} */ function convertToObject(sourceString) { - // write your code here + const strings = sourceString + .trim() + .split(';'); + + const properties = strings + .map(property => property + .split(':') + .map(string => string.trim()) + ); + + const props = properties + .filter(prop => prop.length > 1); + + const result = props + .reduce((styles, property) => { + if (property.length === 0) { + return styles; + } + + styles[`${property[0]}`] = property[1]; + + return styles; + }, {}); + + return result; } module.exports = convertToObject; From ece447aea034c7acf5afa7347cdd71c026c2a661 Mon Sep 17 00:00:00 2001 From: Zyrak Nur Date: Thu, 2 Nov 2023 10:06:22 +0200 Subject: [PATCH 2/2] improve code --- src/convertToObject.js | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/convertToObject.js b/src/convertToObject.js index c6d60a308..4b7c500d1 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -3,9 +3,11 @@ /** * 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 * @@ -14,29 +16,22 @@ function convertToObject(sourceString) { const strings = sourceString .trim() - .split(';'); + .split(';') + .map(property => { + return property + .split(':') + .map(prop => prop.trim()); + }); - const properties = strings - .map(property => property - .split(':') - .map(string => string.trim()) - ); + const listOfProperties = strings.filter(property => property.length > 1); - const props = properties - .filter(prop => prop.length > 1); + const properties = listOfProperties.reduce((styles, property) => { + styles[property[0]] = property[1]; - const result = props - .reduce((styles, property) => { - if (property.length === 0) { - return styles; - } + return styles; + }, {}); - styles[`${property[0]}`] = property[1]; - - return styles; - }, {}); - - return result; + return properties; } module.exports = convertToObject;