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 #3686

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion src/convertToObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@
* @return {object}
*/
function convertToObject(sourceString) {
// write your code here
const styles = {};

if (sourceString.length === 0) {
return styles;
}

sourceString
.split(';')
.map((el) => el.trim())
.filter((el) => el.includes(':'))
.forEach((el) => {
Comment on lines +17 to +19

Choose a reason for hiding this comment

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

  • arr/array/str/string/el/elem/obj/object/res/result are bad names, name should describe variable
  • You can use reduce to generate an object with it

const [key, ...values] = el.split(':');
const value = values.join(':').trim();
Comment on lines +20 to +21

Choose a reason for hiding this comment

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

The use of ...values and values.join(':') is correct for handling cases where the value itself might contain colons. However, ensure that the key is properly trimmed before using it to avoid any leading or trailing spaces in the object keys.


if (key && value) {
styles[key.trim()] = value;
Comment on lines +23 to +24

Choose a reason for hiding this comment

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

Ensure that both key and value are trimmed before checking their truthiness. This will prevent any issues with keys or values that might have only whitespace.

}
});

return styles;
}

module.exports = convertToObject;
Loading