- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 276
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
Conversation
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.
Awesome to have this in XRegExp. Thanks @josephfrazier.
src/addons/build.js
Outdated
* | ||
* [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals | ||
* [2]: https://github.com/slevithan/xregexp/issues/103 | ||
*/ |
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.
Please add an @example
of using this to the comment
src/addons/build.js
Outdated
* [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) { |
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.
nit: remove space after function
to match style used by the rest of the library
src/addons/build.js
Outdated
* [2]: https://github.com/slevithan/xregexp/issues/103 | ||
*/ | ||
XRegExp.tag = function (flags) { | ||
return function tag (literals /*, ...substitutions */) { |
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.
nit: remove space after tag
. probably just remove the function name tag
as well unless you feel this improves it.
src/addons/build.js
Outdated
*/ | ||
XRegExp.tag = function (flags) { | ||
return function tag (literals /*, ...substitutions */) { | ||
var substitutions = [].slice.call(arguments, 1); |
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.
Will you be converting this to ES2015 with ...substitutions
and getting rid of this?
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.
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.
src/addons/build.js
Outdated
return XRegExp.build(pattern, subpatterns, flags); | ||
}; | ||
|
||
function interpolate (substitution) { |
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.
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. :)
src/addons/build.js
Outdated
XRegExp.tag = function (flags) { | ||
return function tag (literals /*, ...substitutions */) { | ||
var substitutions = [].slice.call(arguments, 1); | ||
var subpatterns = substitutions.concat('').map(interpolate); |
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.
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() { |
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.
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.
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)
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! |
tests/spec/s-addons-build.js
Outdated
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'); |
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.
Nit: I'd prefer to use toBe
wherever strict equality is appropriate, even if just for consistency (toBe
is used one line up).
src/addons/build.js
Outdated
@@ -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' |
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.
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.
Also add a test that verifies the example. See #180 (comment)
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)
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
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
Also add a test that verifies the example. See slevithan/xregexp#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/xregexp#180 (comment)
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)
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
Also add a test that verifies the example. See slevithan/xregexp#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/xregexp#180 (comment)
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)
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
Fixes #103