From 176989beff250f2b8bfe46a538b17be693fbe090 Mon Sep 17 00:00:00 2001 From: Maxim Reznichenko Date: Thu, 12 Dec 2024 21:43:04 +0200 Subject: [PATCH] Solution --- src/convertToObject.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/convertToObject.js b/src/convertToObject.js index 27b6fc45..778ce727 100644 --- a/src/convertToObject.js +++ b/src/convertToObject.js @@ -1,19 +1,18 @@ -function convertToObject(sourceString) { - return sourceString - .split(';') - .filter((line) => line.trim()) - .reduce((acc, line) => { - const [property, value] = line.split(':'); +const convertToObject = (sourceString) => { + const trimmedSourceString = sourceString.trim(); + const styleDeclarations = trimmedSourceString.split(';'); - if (property && value) { - const trimmedProperty = property.trim(); - const trimmedValue = value.trim(); + const stylesObject = styleDeclarations.reduce((acc, declaration) => { + const [property, value] = declaration.split(':').map((item) => item.trim()); - acc[trimmedProperty] = trimmedValue; - } + if (property && value) { + acc[property] = value; + } - return acc; - }, {}); -} + return acc; + }, {}); + + return stylesObject; +}; module.exports = convertToObject;