Skip to content

Add XRegExp.tag in build plugin for tagged template string construction #180

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
merged 8 commits into from
Apr 25, 2017

Conversation

josephfrazier
Copy link
Collaborator

Fixes #103

Copy link
Owner

@slevithan slevithan left a comment

Choose a reason for hiding this comment

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

Awesome to have this in XRegExp. Thanks @josephfrazier.

*
* [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals
* [2]: https://github.com/slevithan/xregexp/issues/103
*/
Copy link
Owner

Choose a reason for hiding this comment

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

Please add an @example of using this to the comment

* [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals
* [2]: https://github.com/slevithan/xregexp/issues/103
*/
XRegExp.tag = function (flags) {
Copy link
Owner

Choose a reason for hiding this comment

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

nit: remove space after function to match style used by the rest of the library

* [2]: https://github.com/slevithan/xregexp/issues/103
*/
XRegExp.tag = function (flags) {
return function tag (literals /*, ...substitutions */) {
Copy link
Owner

Choose a reason for hiding this comment

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

nit: remove space after tag. probably just remove the function name tag as well unless you feel this improves it.

*/
XRegExp.tag = function (flags) {
return function tag (literals /*, ...substitutions */) {
var substitutions = [].slice.call(arguments, 1);
Copy link
Owner

Choose a reason for hiding this comment

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

Will you be converting this to ES2015 with ...substitutions and getting rid of this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I just wanted to make it clear for now. I'll definitely follow up with a PR that uses rest parameters across the codebase.

return XRegExp.build(pattern, subpatterns, flags);
};

function interpolate (substitution) {
Copy link
Owner

Choose a reason for hiding this comment

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

Please move interpolate and embedSubpatternAfter out of and above XRegExp.tag, to the same level as asXRegExp. And remove the spaces after the function names. :)

XRegExp.tag = function (flags) {
return function tag (literals /*, ...substitutions */) {
var substitutions = [].slice.call(arguments, 1);
var subpatterns = substitutions.concat('').map(interpolate);
Copy link
Owner

@slevithan slevithan Apr 25, 2017

Choose a reason for hiding this comment

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

Feels a little inelegant to always insert an additional unnecessary empty string substitution at the end of pattern (enabled here by concat-ing an empty string). I'm okay with this as is but if there's a simple way to avoid this, that might be better.

@@ -1,5 +1,81 @@
describe('XRegExp.build addon:', function() {

describe('XRegExp.tag()', function() {
Copy link
Owner

Choose a reason for hiding this comment

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

These tests are great--thanks!

Also add a test that verifies the example.

See slevithan#180 (comment)
There were a couple problems here:

1. Using the index as a subpattern name clashed with capturing groups,
   so I prepended 'subpattern' to the index.
2. XRegExp.tag does not infer capturing group names like XRegExp.build,
   so I changed the @example and test accordingly.
Since numbered groups can get hard to keep track of, show a better
example instead.
josephfrazier added a commit to josephfrazier/xregexp that referenced this pull request Apr 25, 2017
This will make it easier to automatically enforce the pre-existing code
style of the project, using [eslint]. It was done by running:

    npm install --save-dev eslint
    ./node_modules/.bin/eslint --init

[eslint]: https://github.com/eslint/eslint/

This script interactively generates an eslint configuration (see [here]).
Here's how I answered its questions:

* Choosing `Inspect your JavaScript file(s)`
* Entering `src` as the files to inspect
* Choosing a JavaScript config file format
* Answering yes to ES6 features/classes
* Choosing both Node/Browser as environment
* Answering yes to CommonJS
* Answering no to JSX/React

[here]: http://devnull.guru/adding-eslint-to-your-project-is-easier-than-ever/

After that, I added the `lint` script to package.json, and manually
changed the `space-before-function-paren` to:

    "space-before-function-paren": [
        "error",
        "never"
    ],

to enforce the rule mentioned here:

* slevithan#180 (comment)
* slevithan#180 (comment)
embedSubpatternAfter has access to the index of the element it's
operating on, so we can just check it to see whether the subpattern is
present.

See slevithan#180 (comment)
@josephfrazier
Copy link
Collaborator Author

Thanks for the review! I just pushed up a batch of commits that should address your comments. The one about adding an example actually prompted me to add another test and discover/fix a bug. Let me know what you think!

var minutes = /^[0-5][0-9]$/;
var time = XRegExp.tag('x')`^ ${hours} (${minutes}) $`
expect(time.test('10:59')).toBe(true);
expect(XRegExp.exec('10:59', time).minutes).toEqual('59');
Copy link
Owner

Choose a reason for hiding this comment

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

Nit: I'd prefer to use toBe wherever strict equality is appropriate, even if just for consistency (toBe is used one line up).

@@ -83,12 +88,12 @@ module.exports = function(XRegExp) {
* var minutes = /^[0-5][0-9]$/;
* var time = XRegExp.tag('x')`^ ${hours} (${minutes}) $`
* time.test('10:59'); // -> true
* XRegExp.exec('10:59', time).minutes; // -> '59'
* XRegExp.exec('10:59', time)[1]; // -> '59'
Copy link
Owner

@slevithan slevithan Apr 25, 2017

Choose a reason for hiding this comment

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

Maybe just remove this line and the capturing group around ${minutes} since this is no longer showing an additional feature.

Edit: Nevermind. I see you modified this in a valuable way (to show an explicitly named capturing group) in a following diff.

@slevithan slevithan merged commit 81d36fe into slevithan:master Apr 25, 2017
slevithan pushed a commit that referenced this pull request Apr 25, 2017
Also add a test that verifies the example.

See #180 (comment)
slevithan pushed a commit that referenced this pull request Apr 25, 2017
slevithan pushed a commit that referenced this pull request Apr 25, 2017
This will make it easier to automatically enforce the pre-existing code
style of the project, using [eslint]. It was done by running:

    npm install --save-dev eslint
    ./node_modules/.bin/eslint --init

[eslint]: https://github.com/eslint/eslint/

This script interactively generates an eslint configuration (see [here]).
Here's how I answered its questions:

* Choosing `Inspect your JavaScript file(s)`
* Entering `src` as the files to inspect
* Choosing a JavaScript config file format
* Answering yes to ES6 features/classes
* Choosing both Node/Browser as environment
* Answering yes to CommonJS
* Answering no to JSX/React

[here]: http://devnull.guru/adding-eslint-to-your-project-is-easier-than-ever/

After that, I added the `lint` script to package.json, and manually
changed the `space-before-function-paren` to:

    "space-before-function-paren": [
        "error",
        "never"
    ],

to enforce the rule mentioned here:

* #180 (comment)
* #180 (comment)
josephfrazier added a commit to josephfrazier/xregexp that referenced this pull request Apr 25, 2017
As mentioned in slevithan#180 (comment)
and slevithan#108 (comment)

This was done with the help of [lebab], using `lebeb --replace src --transform arg-rest`.

[lebab]: https://github.com/lebab/lebab
slevithan pushed a commit that referenced this pull request Apr 26, 2017
As mentioned in #180 (comment)
and #108 (comment)

This was done with the help of [lebab], using `lebeb --replace src --transform arg-rest`.

[lebab]: https://github.com/lebab/lebab
@josephfrazier josephfrazier deleted the template branch April 30, 2017 23:42
speecyy added a commit to speecyy/xregexp that referenced this pull request Jan 6, 2018
Also add a test that verifies the example.

See slevithan/xregexp#180 (comment)
speecyy added a commit to speecyy/xregexp that referenced this pull request Jan 6, 2018
speecyy added a commit to speecyy/xregexp that referenced this pull request Jan 6, 2018
embedSubpatternAfter has access to the index of the element it's
operating on, so we can just check it to see whether the subpattern is
present.

See slevithan/xregexp#180 (comment)
speecyy added a commit to speecyy/xregexp that referenced this pull request Jan 6, 2018
This will make it easier to automatically enforce the pre-existing code
style of the project, using [eslint]. It was done by running:

    npm install --save-dev eslint
    ./node_modules/.bin/eslint --init

[eslint]: https://github.com/eslint/eslint/

This script interactively generates an eslint configuration (see [here]).
Here's how I answered its questions:

* Choosing `Inspect your JavaScript file(s)`
* Entering `src` as the files to inspect
* Choosing a JavaScript config file format
* Answering yes to ES6 features/classes
* Choosing both Node/Browser as environment
* Answering yes to CommonJS
* Answering no to JSX/React

[here]: http://devnull.guru/adding-eslint-to-your-project-is-easier-than-ever/

After that, I added the `lint` script to package.json, and manually
changed the `space-before-function-paren` to:

    "space-before-function-paren": [
        "error",
        "never"
    ],

to enforce the rule mentioned here:

* slevithan/xregexp#180 (comment)
* slevithan/xregexp#180 (comment)
speecyy added a commit to speecyy/xregexp that referenced this pull request Jan 6, 2018
As mentioned in slevithan/xregexp#180 (comment)
and slevithan/xregexp#108 (comment)

This was done with the help of [lebab], using `lebeb --replace src --transform arg-rest`.

[lebab]: https://github.com/lebab/lebab
3590212051 added a commit to 3590212051/POPmotion that referenced this pull request Jan 8, 2018
Also add a test that verifies the example.

See slevithan/xregexp#180 (comment)
3590212051 added a commit to 3590212051/POPmotion that referenced this pull request Jan 8, 2018
3590212051 added a commit to 3590212051/POPmotion that referenced this pull request Jan 8, 2018
3590212051 added a commit to 3590212051/POPmotion that referenced this pull request Jan 8, 2018
embedSubpatternAfter has access to the index of the element it's
operating on, so we can just check it to see whether the subpattern is
present.

See slevithan/xregexp#180 (comment)
3590212051 added a commit to 3590212051/POPmotion that referenced this pull request Jan 8, 2018
This will make it easier to automatically enforce the pre-existing code
style of the project, using [eslint]. It was done by running:

    npm install --save-dev eslint
    ./node_modules/.bin/eslint --init

[eslint]: https://github.com/eslint/eslint/

This script interactively generates an eslint configuration (see [here]).
Here's how I answered its questions:

* Choosing `Inspect your JavaScript file(s)`
* Entering `src` as the files to inspect
* Choosing a JavaScript config file format
* Answering yes to ES6 features/classes
* Choosing both Node/Browser as environment
* Answering yes to CommonJS
* Answering no to JSX/React

[here]: http://devnull.guru/adding-eslint-to-your-project-is-easier-than-ever/

After that, I added the `lint` script to package.json, and manually
changed the `space-before-function-paren` to:

    "space-before-function-paren": [
        "error",
        "never"
    ],

to enforce the rule mentioned here:

* slevithan/xregexp#180 (comment)
* slevithan/xregexp#180 (comment)
3590212051 added a commit to 3590212051/POPmotion that referenced this pull request Jan 8, 2018
As mentioned in slevithan/xregexp#180 (comment)
and slevithan/xregexp#108 (comment)

This was done with the help of [lebab], using `lebeb --replace src --transform arg-rest`.

[lebab]: https://github.com/lebab/lebab
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.

None yet

2 participants