Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
f445cc6
commit 5c793ce
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
type ReplaceOptions = { | ||
all?: boolean; | ||
}; | ||
|
||
/** | ||
Represents a string with some or all matches replaced by a replacement. | ||
Use-case: | ||
- `snake-case-path` to `dotted.path.notation` | ||
- Changing date/time format: `01-08-2042` β `01/08/2042` | ||
- Manipulation of type properties, for example, removal of prefixes | ||
@example | ||
``` | ||
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}>; | ||
// The return type is the exact string literal, not just `string`. | ||
replace('hello ?', '?', 'π¦'); | ||
//=> 'hello π¦' | ||
replace('hello ??', '?', 'β'); | ||
//=> 'hello β?' | ||
replaceAll('10:42:00', ':', '-'); | ||
//=> '10-42-00' | ||
replaceAll('__userName__', '__', ''); | ||
//=> 'userName' | ||
replaceAll('My Cool Title', ' ', ''); | ||
//=> 'MyCoolTitle' | ||
``` | ||
@category String | ||
@category Template literal | ||
*/ | ||
export type Replace< | ||
Input extends string, | ||
Search extends string, | ||
Replacement extends string, | ||
Options extends ReplaceOptions = {}, | ||
> = Input extends `${infer Head}${Search}${infer Tail}` | ||
? Options['all'] extends true | ||
? Replace<`${Head}${Replacement}${Tail}`, Search, Replacement, Options> | ||
: `${Head}${Replacement}${Tail}` | ||
: Input; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {expectType} from 'tsd'; | ||
import type {Replace} from '../index'; | ||
|
||
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}>; | ||
|
||
expectType<'hello π¦'>(replace('hello ?', '?', 'π¦')); | ||
expectType<'hello β?'>(replace('hello ??', '?', 'β')); | ||
expectType<'10-42-00'>(replaceAll('10:42:00', ':', '-')); | ||
expectType<'userName'>(replaceAll('__userName__', '__', '')); | ||
expectType<'MyCoolTitle'>(replaceAll('My Cool Title', ' ', '')); |