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

Solution #3745

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
13 changes: 12 additions & 1 deletion src/convertToObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
* @return {object}
*/
function convertToObject(sourceString) {
// write your code here
const styleObject = {};
const declarations = sourceString.split(';');

declarations.forEach((declaration) => {
const [property, value] = declaration.split(':');

Choose a reason for hiding this comment

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

The declaration.split(':') might not handle cases where the value contains colons, such as in URLs. Consider using a more robust parsing method if such cases are expected.


if (property && value) {

Choose a reason for hiding this comment

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

The check if (property && value) ensures that both property and value exist, but it does not handle cases where either might be an empty string. Consider trimming the declaration before splitting to avoid issues with leading or trailing semicolons.

styleObject[property.trim()] = value.trim();
}
});

return styleObject;
}

module.exports = convertToObject;