-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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 #3376
base: master
Are you sure you want to change the base?
Solution #3376
Conversation
src/convertToObject.js
Outdated
// write your code here | ||
const stylesObject = {}; | ||
|
||
const declarations = sourceString |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name 'declarations' could be more descriptive. Consider renaming it to something that indicates it contains CSS declarations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/convertToObject.js
Outdated
const stylesObject = {}; | ||
|
||
const declarations = sourceString | ||
.split(/;(?![^(]*\))/) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regular expression used in split might not handle all edge cases, such as when there are semicolons within strings or comments in the CSS. Consider refining it to handle these cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/convertToObject.js
Outdated
.filter((decl) => decl); | ||
|
||
declarations.forEach((declaration) => { | ||
const [property, ...valueParts] = declaration.split(':'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 'split' operation assumes that there will be only one ':' character separating property and value. If the value contains additional ':' characters (e.g., in a data URI), this could lead to incorrect results.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
src/convertToObject.js
Outdated
const [property, ...valueParts] = declaration.split(':'); | ||
const value = valueParts.join(':').trim(); | ||
|
||
if (property && value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking if 'property' and 'value' are truthy is good, but consider also checking that 'property' is not an empty string after trimming.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work 👍
Let's improve your solution slightly
src/convertToObject.js
Outdated
const stringWithoutComments = sourceString.replace( | ||
/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g, | ||
'', | ||
); | ||
|
||
const matches = stringWithoutComments.match(/(?:[^;"']|"[^"]*"|'[^']*')+/g); | ||
const cssDeclarations = matches | ||
? matches.map((decl) => decl.trim()).filter((decl) => decl) | ||
: []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need regular expressions for this task. Just split the string ;
remove spaces, and empty lines.
src/convertToObject.js
Outdated
cssDeclarations.forEach((declaration) => { | ||
const [property, ...valueParts] = declaration.split(':'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- You can use
reduce
to generate an object const [key, value] = declaration.split(':');
After the first steps your string should looks likecolor: red;
, so you can just split and remove spaces then
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job 👍
No description provided.