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

feat: fix .replace() when searching string, add .replaceAll() #222

Merged
merged 5 commits into from Sep 22, 2022

Conversation

fisker
Copy link
Contributor

@fisker fisker commented Sep 7, 2022

Fix #221

@@ -1305,7 +1305,28 @@ describe('MagicString', () => {

s.replace('2', '3');

assert.strictEqual(s.toString(), '1 3 1 2');
assert.strictEqual(s.toString(), '1 3 1 3');
Copy link
Contributor Author

@fisker fisker Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't want add .replaceAll, I guess we should just replace them all.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it makes better sense to align with JavaScript's string, so I'd prefer adding .replaceAll and keeping .replace only replaced once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

@fisker
Copy link
Contributor Author

fisker commented Sep 13, 2022

@antfu Can you take a look?

}

replace(searchValue, replacement) {
return this[typeof searchValue === 'string' ? '_replaceString' : '_replaceRegexp'](
Copy link
Collaborator

@antfu antfu Sep 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use an if condition instead of props accessing? Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@fisker
Copy link
Contributor Author

fisker commented Sep 22, 2022

All review points addressed.

@fisker fisker changed the title Fix .replace() when searching string Fix .replace() when searching string, add .replaceAll() Sep 22, 2022
@antfu antfu changed the title Fix .replace() when searching string, add .replaceAll() feat: fix .replace() when searching string, add .replaceAll() Sep 22, 2022
@antfu
Copy link
Collaborator

antfu commented Sep 22, 2022

Awesome, thanks!

@antfu antfu merged commit 04a05bd into Rich-Harris:master Sep 22, 2022
@fisker fisker deleted the replace-string branch September 22, 2022 09:19
@Tal500
Copy link
Contributor

Tal500 commented Sep 30, 2022

Should have added "replaceAll" to the typescript definition file!

See the PR I have submitted right now: #224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected .replace behavior
3 participants