Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use destructuring for explanatory variables #37

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

Merged

Conversation

SebastienElet
Copy link
Contributor

@SebastienElet SebastienElet commented Jan 6, 2017

This use the ES2015 way.

I removed the match because it's not used and we could also declare a const all to grab the first item of the match if needed.

@filipecatraia
Copy link

Is this syntax not more complicated to understand?

@SebastienElet
Copy link
Contributor Author

There a learning curve for the destructuring in my opinion but this avoid to play with array indexes

const match = cityStateRegex.match(cityStateRegex)
const city = match[1];
const state = match[2];
const [, city, state] = cityStateRegex.match(cityStateRegex);
Copy link
Owner

Choose a reason for hiding this comment

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

I find destructuring cleaner as well. For the record, that's my opinion and not Clean Code's.

This is a great fix all in all! Can you fix the regex above so that we don't have to omit the first item in the array? I should have used a better one to begin with.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, i will also add a string for the example because apply a regex on a regex is a non sense :-)

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Jan 8, 2017

@SebastienElet Some corrections:

  1. .match() is a method of String, not RegExp.
  2. The first character set lacks quantifier (otherwise .match() returns null and each code throws).
  3. The first element in the results array is always the full match, so it should be ignored with any regexp structure.

So the code should be this one:

Bad:

const address = 'One Infinite Loop, Cupertino 95014';
const cityStateRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/;
console.log(address.match(cityStateRegex)[1], address.match(cityStateRegex)[2]);

Good:

const address = 'One Infinite Loop, Cupertino 95014';
const cityStateRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/;
const [, city, state] = address.match(cityStateRegex);
console.log(city, state);

@SebastienElet SebastienElet force-pushed the feat/use-destructuring branch from cfe07d3 to f446783 Compare January 9, 2017 07:10
@SebastienElet
Copy link
Contributor Author

@vsemozhetbyt thanks a lot, this gives me ideas for my next PR :-)

@ryanmcdermott ryanmcdermott merged commit cea298d into ryanmcdermott:master Jan 11, 2017
@ryanmcdermott
Copy link
Owner

This has been a crappy lingering problem, thank you tons Sebastien for taking the lead and fixing this!

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.

4 participants