-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add a function to replace matches with a callback #3
Changes from 4 commits
1ee3d10
672fcfd
577707d
815f2e4
dbf9f64
6f4c2de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,3 +49,23 @@ export function scan(regex, string) { | |
export function replace(regex, original_string, replacement) { | ||
return original_string.replaceAll(regex, replacement); | ||
} | ||
|
||
export function replace_map(regex, original_string, replacement) { | ||
let replace = (match, ...args) => { | ||
const hasNamedGroups = typeof args.at(-1) === "object"; | ||
const groups = args.slice(0, hasNamedGroups ? -3 : -2); | ||
const submatches = []; | ||
for (let n = 0; n < groups.length; n++) { | ||
if (groups[n]) { | ||
submatches[n] = new Some(groups[n]); | ||
continue; | ||
} | ||
if (submatches.length > 0) { | ||
submatches[n] = new None(); | ||
} | ||
} | ||
let regexMatch = new RegexMatch(match, List.fromArray(submatches)); | ||
return replacement(regexMatch); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a copy/paste of the code above? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's very similar, but not identical. I originally pulled it out into its own function to reuse, but when I did, my test failed. I didn't dig in to understand why the above code iterated backwards, but when I iterated forwards, the test passed. I'm not opposed to trying to find a way to unify the two, but I didn't want to go altering existing code that works without discussing it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at it again, I bet I didn't account for the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems a shame there's such similar code here! Can they not be unified? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think they can. I'll take a stab at it later today. |
||
return original_string.replaceAll(regex, replace); | ||
} |
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.
Shouldn't this use the same submatches function above?
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.
I tried to use it and it didn't work. The function signature for the replacement is
fn((binary(), [binary()]) -> iodata() | unicode:charlist())
so the submatches are just[binary()]
.