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
regexp: add S.replace #693
Conversation
cd9d191
to
94b9a23
Compare
return function(pattern) { | ||
return function(text) { | ||
return text.replace (pattern, function() { | ||
var groups = []; |
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.
Is it possible to know the length of this array upfront? Say, using var groups = new Array(arguments.length)
. If so, then that would get you slightly better performance at relatively low implementation and maintenance cost. You would have to alter groups.push
statements to index assignment statements instead, eg: groups[idx] = (...)
.
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.
Is it possible to know the length of this array upfront?
No, due to the existence of named capturing groups:
> '@sanctuary-js'.replace (/@([-\w]+)/, function() { return arguments.length; })
'4'
> '@sanctuary-js'.replace (/@(?<username>[-\w]+)/, function() { return arguments.length; })
'5'
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.
Ah, so these named groups are skipped? Is that what the typeof
check below is doing?
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.
That's right. I imagine groups
is omitted when the pattern contains no named capturing groups to preserve backward compatibility. Providing the sensible empty value, {}
, as an additional argument would break programs that assume that Array.prototype.slice.call(arguments, 1, -2)
extracts (only) the captured values.
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.
Oh, wow. It took me a really long time to realize that what's happening there is: skip one argument, and keep taking arguments until a number is encountered. The numeric argument signifies the "end" of the capture group arguments. Wow. The signature of JavaScript's replace
is crazier than I remember.
94b9a23
to
94e450d
Compare
Supersedes #686
This pull request adds the following function:
In #686 (comment), @Avaq made a strong case for naming this more general function
replace
. @Avaq also noted that the specialized function,replaceWith
, is trivially derivable fromreplace
. For this reason I decided not to includereplaceWith
in this pull request; it could easily be added in the future ifreplace (K ('...'))
proves irksome.I have opened sanctuary-js/sanctuary-site#90 to provide several examples of
replace
in use.