Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task finished #2757

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions src/convertToObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to use reduce, but you could process every item inside this method and get rid of other methods like

return sourceString.split(";").reduce((result, style) => {
there you get array from the whole string style.split(":");
there you trim every pair key = yourPair[0]?.trim();
there you trim every pair value = yourPair[1]?.trim();
if (key && value) {
result[key] = value;
}
return result;
}, {});

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESLint forbids "?" before trim :(


return objectWithCssParams;
}

module.exports = convertToObject;
Loading