-
Notifications
You must be signed in to change notification settings - Fork 43
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
Use Regexp.exec
instead of replace
for side-effects
#13
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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.
Left a couple of comments!
// actually matched, so that we can extract the appropriate keyword. | ||
combineText(post).replace(termsRegex, (_, ...terms: string[]) => { | ||
let match; | ||
while ((match = termsRegex.exec(combined))) { |
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 this an equals sign =
or an equality ==
comparison?
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.
Equals. In this situation, the extra parens are meant to indicate that this is an intentional assignment. See https://eslint.org/docs/latest/rules/no-cond-assign
@@ -47,22 +47,22 @@ export function postScanner(teamsAndKeywords: TeamAndKeywords) { | |||
const text = combineText(post); // combine text from post's title, text, and url | |||
const teamsInterestedInThisPost = new Set() as Set<string>; // set of team IDs that are interested in this post | |||
|
|||
text.replace(scanner, (_, ...terms) => { | |||
let match; | |||
while ((match = scanner.exec(text))) { |
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.
Should this be an equality ==
comparison?
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.
No, scanner.exec(text) checks null. So == is not needed.
Could we add a description so it's clear what this PR solves? 🙏 |
This replaces the
String.p.replace
uses that are only used for side-effects with a fasterRegExp.p.exec
loop. This avoids the string allocation of the output string, and makes it a little more clear the loop is used for side-effects.