From a3107cb2452e158438ced93401380a2d93369a0c Mon Sep 17 00:00:00 2001 From: Fluebubble Date: Thu, 9 Nov 2023 20:06:01 +0200 Subject: [PATCH 1/3] task finished --- src/convertToObject.js | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/convertToObject.js b/src/convertToObject.js index 44d498c1d..43b5a950a 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -3,16 +3,42 @@ /** * 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 objectWithCssParams = {}; + + sourceString + .split(/\n/) + .filter((param) => { + const trimmedParam = param.trim(); + + return !(trimmedParam.length <= 1); + }) + .map((param) => { + return param.split(':').map((value) => { + const fixedValue = value.includes(';') + ? value.slice(0, value.indexOf(';')) + : value; + + return fixedValue.trim(); + }); + }) + .reduce((accumulator, param) => { + accumulator[param[0]] = param[1]; + + return accumulator; + }, objectWithCssParams); + + return objectWithCssParams; } module.exports = convertToObject; From 053efd68f95cc47fc12c04032c4a41b651628f1c Mon Sep 17 00:00:00 2001 From: Fluebubble Date: Sat, 11 Nov 2023 19:58:52 +0200 Subject: [PATCH 2/3] fixed --- src/convertToObject.js | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/convertToObject.js b/src/convertToObject.js index 43b5a950a..8bf46980e 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -14,29 +14,26 @@ * @return {object} */ function convertToObject(sourceString) { - const objectWithCssParams = {}; - - sourceString + const objectWithCssParams = sourceString .split(/\n/) .filter((param) => { const trimmedParam = param.trim(); return !(trimmedParam.length <= 1); }) - .map((param) => { - return param.split(':').map((value) => { - const fixedValue = value.includes(';') - ? value.slice(0, value.indexOf(';')) - : value; + .reduce((accumulator, stringParam) => { + const [key, value] = stringParam.split(':').map((param) => { + const fixedValue = param.includes(';') + ? param.slice(0, param.indexOf(';')) + : param; return fixedValue.trim(); }); - }) - .reduce((accumulator, param) => { - accumulator[param[0]] = param[1]; + + accumulator[key] = value; return accumulator; - }, objectWithCssParams); + }, {}); return objectWithCssParams; } From 8f3b226651d642d63703974491e08a1b1791d46a Mon Sep 17 00:00:00 2001 From: Fluebubble Date: Sat, 11 Nov 2023 21:00:14 +0200 Subject: [PATCH 3/3] app rewritten --- src/convertToObject.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/convertToObject.js b/src/convertToObject.js index 8bf46980e..cf11928d6 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -15,22 +15,16 @@ */ function convertToObject(sourceString) { const objectWithCssParams = sourceString - .split(/\n/) - .filter((param) => { - const trimmedParam = param.trim(); - - return !(trimmedParam.length <= 1); - }) + .split(';') .reduce((accumulator, stringParam) => { - const [key, value] = stringParam.split(':').map((param) => { - const fixedValue = param.includes(';') - ? param.slice(0, param.indexOf(';')) - : param; + const separatedParam = stringParam.split(':'); - return fixedValue.trim(); - }); + if (separatedParam.length === 2) { + const key = separatedParam[0].trim(); + const value = separatedParam[1].trim(); - accumulator[key] = value; + accumulator[key] = value; + } return accumulator; }, {});