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

regexp: add S.replace #693

Merged
merged 1 commit into from Aug 24, 2020
Merged

regexp: add S.replace #693

merged 1 commit into from Aug 24, 2020

Conversation

davidchambers
Copy link
Member

Supersedes #686

This pull request adds the following function:

replace :: (Array (Maybe String) -> String) -> RegExp -> String -> String

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 from replace. For this reason I decided not to include replaceWith in this pull request; it could easily be added in the future if replace (K ('...')) proves irksome.

I have opened sanctuary-js/sanctuary-site#90 to provide several examples of replace in use.

index.js Show resolved Hide resolved
return function(pattern) {
return function(text) {
return text.replace (pattern, function() {
var groups = [];
Copy link
Member

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] = (...).

Copy link
Member Author

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'

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

@davidchambers davidchambers merged commit 13a6312 into master Aug 24, 2020
@davidchambers davidchambers deleted the davidchambers/replace branch August 24, 2020 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants