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

Fix Replace and add recursive option. #416

Merged
merged 6 commits into from Jul 3, 2022

Conversation

JonghwanWon
Copy link
Sponsor Contributor

@JonghwanWon JonghwanWon commented Jul 2, 2022

If specify the {all:true} option for the current type Replace, we can expect such as .replaceAll in Javascript.

But the current Replace is not. Constructs the result of replace recursively execution.

declare function replaceAll<
	Input extends string,
	Search extends string,
	Replacement extends string,
>(
	input: Input,
	search: Search,
	replacement: Replacement
): Replace<Input, Search, Replacement, {all: true}>;

replaceAll('foobarfoobar', 'ob', 'b'); //  expected to be "fobarfobar". but is "fbarfbar"

Therefore, the recursive option is added so that it can be accurately distinguished from all.

declare function replaceAll<
	Input extends string,
	Search extends string,
	Replacement extends string,
>(
	input: Input,
	search: Search,
	replacement: Replacement
): Replace<Input, Search, Replacement, {all: true}>;

declare function replaceRecursively<
	Input extends string,
	Search extends string,
	Replacement extends string,
>(
	input: Input,
	search: Search,
	replacement: Replacement
): Replace<Input, Search, Replacement, {recursive: true}>;

replaceAll('foobarfoobar', 'ob', 'b'); // 'fobarfobar'
replaceRecursively('foobarfoobar', 'ob', 'b'); //  'fbarfbar'

@sindresorhus
Copy link
Owner

sindresorhus commented Jul 2, 2022

This just sounds like a bug in the all option.

// @skarab42

@skarab42
Copy link
Collaborator

skarab42 commented Jul 3, 2022

Therefore, the recursive option is added so that it can be accurately distinguished from all.

I think that we should not add complexity, but simply fix the bug. For me "all" and "recursive" are the same thing.

@skarab42
Copy link
Collaborator

skarab42 commented Jul 3, 2022

In any case, thank you for poiting this bug. Your solution seems very good (without the complexity) ;)

@sindresorhus
Copy link
Owner

@JonghwanWon Can you drop the recursive option and just make this a bug fix?

@JonghwanWon
Copy link
Sponsor Contributor Author

@JonghwanWon Can you drop the recursive option and just make this a bug fix?

Reverted recursive option and simply fixed the bug.

@sindresorhus
Copy link
Owner

Can you include a test that would fail before this fix? To ensure it doesn't regress in the future.

@sindresorhus sindresorhus merged commit 1483de3 into sindresorhus:main Jul 3, 2022
@JonghwanWon JonghwanWon deleted the replace branch July 4, 2022 01:27
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.

None yet

3 participants