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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -165,9 +165,9 @@ Same as `s.appendLeft(...)`, except that the inserted content will go *before* a

Same as `s.appendRight(...)`, except that the inserted content will go *before* any previous appends or prepends at `index`

### s.replace( regexp, substitution )
### s.replace( regexpOrString, substitution )

String replacement with RegExp or string, a replacer function is also supported. Returns `this`.
String replacement with RegExp or string. When using a RegExp, replacer function is also supported. Returns `this`.

```ts
import MagicString from 'magic-string'
Expand Down
25 changes: 23 additions & 2 deletions src/MagicString.js
Expand Up @@ -736,7 +736,7 @@ export default class MagicString {
return this.original !== this.toString();
}

replace(searchValue, replacement) {
_replaceRegexp(searchValue, replacement) {
function getReplacement(match, str) {
if (typeof replacement === 'string') {
return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
Expand All @@ -759,7 +759,7 @@ export default class MagicString {
}
return matches;
}
if (typeof searchValue !== 'string' && searchValue.global) {
if (searchValue.global) {
const matches = matchAll(searchValue, this.original);
matches.forEach((match) => {
if (match.index != null)
Expand All @@ -780,4 +780,25 @@ export default class MagicString {
}
return this;
}

_replaceString(string, replacement) {
const { original } = this;
const stringLength = string.length;
for (
let index = original.indexOf(string);
index !== -1;
index = original.indexOf(string, index + stringLength)
) {
this.overwrite(index, index + stringLength, replacement);
}

return this;
}

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

searchValue,
replacement
);
}
}
23 changes: 22 additions & 1 deletion test/MagicString.js
Expand Up @@ -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!

});

it('Should not treat string as regexp', () => {
assert.strictEqual(
new MagicString('1234').replace('.', '*').toString(),
'1234'
);
});

it('Should use substitution directly', () => {
assert.strictEqual(
new MagicString('11').replace('1', '$0$1').toString(),
'$0$1$0$1'
);
});

it('Should not search back', () => {
assert.strictEqual(
new MagicString('121212').replace('12', '21').toString(),
'212121'
);
});

it('works with global regex replace', () => {
Expand Down