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

Use string processing instead of regex #14

Merged
merged 5 commits into from
Jan 25, 2017

Conversation

ruipneves
Copy link
Contributor

@ruipneves ruipneves commented Jan 20, 2017

I saw this issue and decided to fork and give it a try, mainly because regular expressions can become really messy and confusing especially if things scale. The string processing suggested by you, was definitely a good idea to straighten things up.

The solution that I found for this problem was to iterate through the string maintaining a state, which is defined by the betweenQuotes variable. It is used to know when the processing is happening between two matching quotes and when the closing quote is found. While processing between matching quotes, escape and backslashes characters are processed in order to know when to escape a single quote or simply to escape the backslash.

What do you guys think?
Any suggestions?! Feel free to give me your advice.

TODO:

  • It would be interesting to add options in order to make the other way possible (single quotes -> double quotes).

Fixes #13

@satazor
Copy link

satazor commented Jan 20, 2017

@ruipneves Could do update this PR to obey the code style? See: https://travis-ci.org/sindresorhus/to-single-quotes/jobs/193572769

If you are using atom or sublime search for linter-xo plugins, otherwise you may manually fix the issues by running xo manually inside node_modules.

Same thing applies to sindresorhus/round-to#6 PR.

@ruipneves
Copy link
Contributor Author

I pushed the fix for the linter-xo but it has a problem for node version 0.10 and 0.12 as you can see in Travis log. This is due to the xo library being installed with the latest version which causes an error by using const which is not supported in versions 0.10 and 0.12.

@satazor
Copy link

satazor commented Jan 20, 2017

@ruipneves You are right, we probably need to remove support for 0.10 and 0.12. We should do this by removing them from .travis.yml and also forcing the engines in package.json. @sindresorhus could you please give your feedback here?

var x, y, len = str.length;
var result = '';
var x;
var len = str.length;
var betweenQuotes = false;
var QuoteChar;
Copy link

Choose a reason for hiding this comment

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

Should be quoteChar

var betweenQuotes = false;
var QuoteChar;
for (x = 0; x < len; x++) {
// FOUND DOUBLE-QUOTE OR SINGLE-QUOTE
Copy link

Choose a reason for hiding this comment

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

Change comments to lowercase.

// FOUND DOUBLE-QUOTE OR SINGLE-QUOTE
if (str[x] === '"' || str[x] === '\'') {
// IF NOT PROCESSING IN BETWEEN QUOTES
if (betweenQuotes === false) {
Copy link

Choose a reason for hiding this comment

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

Change to !betweenQuotes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had !betweenQuotes on the initial version but linter-xo pointed an error there when I ran the fix. Maybe the chain of issues below caused linter-xo to report that as an error as well?

// ESCAPE + DOUBLE QUOTE
result += '"';
x++;
} else {
Copy link

@satazor satazor Jan 20, 2017

Choose a reason for hiding this comment

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

@satazor
Copy link

satazor commented Jan 21, 2017

@ruipneves I've added a few comments, sorry for the delay.

@ruipneves
Copy link
Contributor Author

ruipneves commented Jan 22, 2017

@satazor Thank you for your feedback. I just pushed the fix for the code styling errors. I still don't know how the strange indentations got there. The only thing I did in the previous submission was applying the linter-xo fix to the code and manually fixing the errors that linter-xo couldn't fix automatically.

Copy link

@satazor satazor left a comment

Choose a reason for hiding this comment

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

Leaving this out for @sindresorhus to approve the code logic

@satazor
Copy link

satazor commented Jan 22, 2017

@sindresorhus tests are failing on node < 4 because of xo, is it ok to remove support for older nodes right?

@SamVerschueren
Copy link

That or set XO to a fixed version. If I'm not mistaken, it should be 0.16.1.

@sindresorhus
Copy link
Owner

That or set XO to a fixed version. If I'm not mistaken, it should be 0.16.1.

👍 I will remove support for Node.js 0.10/0.12 after this PR is merged.

@sindresorhus sindresorhus changed the title Update from regex to string processing Use string processing instead of regex Jan 22, 2017
.replace(/^"|"$/g, '\'');
});
var result = '';
var x;
Copy link
Owner

Choose a reason for hiding this comment

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

Use i, not x.

});
var result = '';
var x;
var len = str.length;
Copy link
Owner

Choose a reason for hiding this comment

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

There's no point in caching the length. V8 is smart enough to do it for us.

result += str[x];
}
} else if (str[x] === '\\' && str[x + 1] === '\\') {
// backslashes --> don't touch them
Copy link
Owner

Choose a reason for hiding this comment

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

Make it:

don't touch backslashes

@sindresorhus
Copy link
Owner

Looks pretty good :)


It would be interesting to add options in order to make the other way possible (single quotes -> double quotes).

Agreed. Not as a documented option, but to be able to reuse the logic here in to-double-quotes without having to duplicate the code.

@ruipneves
Copy link
Contributor Author

@sindresorhus Thank you for the feedback! Just pushed the new version with the requested changes.

@sindresorhus
Copy link
Owner

👍 Looks really good. Thank you @ruipneves :)

@sindresorhus sindresorhus merged commit 7f203d0 into sindresorhus:master Jan 25, 2017
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