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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Conversation

AndriiZakharenko
Copy link

No description provided.

// write your code here
const stylesObject = {};

const declarations = sourceString

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.

Copy link
Author

Choose a reason for hiding this comment

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

done

const stylesObject = {};

const declarations = sourceString
.split(/;(?![^(]*\))/)

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.

Copy link
Author

Choose a reason for hiding this comment

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

done

.filter((decl) => decl);

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

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.

Copy link
Author

Choose a reason for hiding this comment

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

done

const [property, ...valueParts] = declaration.split(':');
const value = valueParts.join(':').trim();

if (property && value) {

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.

Copy link
Author

Choose a reason for hiding this comment

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

done

Copy link

@BudnikOleksii BudnikOleksii left a 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

Comment on lines 16 to 24
const stringWithoutComments = sourceString.replace(
/\/\*[^*]*\*+([^/*][^*]*\*+)*\//g,
'',
);

const matches = stringWithoutComments.match(/(?:[^;"']|"[^"]*"|'[^']*')+/g);
const cssDeclarations = matches
? matches.map((decl) => decl.trim()).filter((decl) => decl)
: [];

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.

Comment on lines 26 to 27
cssDeclarations.forEach((declaration) => {
const [property, ...valueParts] = declaration.split(':');

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 like color: red;, so you can just split and remove spaces then

Copy link

@BudnikOleksii BudnikOleksii left a comment

Choose a reason for hiding this comment

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

Good job 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants