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

Add Replace type #389

Merged
merged 7 commits into from May 24, 2022
Merged

Add Replace type #389

merged 7 commits into from May 24, 2022

Conversation

skarab42
Copy link
Collaborator

@skarab42 skarab42 commented Apr 5, 2022

Adds the Replace type which may be useful in the following cases.

  • snake-case-path to dotted.path.notation
  • Date/Time format "01-08-2042" -> "01/08/2042"
  • Manipulation of type properties e.g. removal of prefixes
  • Your own case ...?
import {Replace} from 'type-fest';

declare function replace<
	Input extends string,
	Search extends string,
	Replacement extends string
>(
	input: Input,
	search: Search,
	replacement: Replacement
): Replace<Input, Search, Replacement>;

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

replace("hello ?", "?", "🦄"); // => "hello 🦄"
replace("hello ??", "?", "❓"); // => "hello ❓?"

replaceAll("10:42:00", ":", "-");     // => "10-42-00"
replaceAll("__userName__", "__", ""); // => "userName"
replaceAll("My Cool Title", " ", ""); // => "MyCoolTitle"

@sindresorhus sindresorhus merged commit 5c793ce into sindresorhus:main May 24, 2022
@sindresorhus
Copy link
Owner

Thanks :)

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

2 participants