Skip to content

Commit

Permalink
Add Replace type (#389)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
skarab42 and sindresorhus committed May 24, 2022
1 parent f445cc6 commit 5c793ce
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -78,6 +78,7 @@ export {DelimiterCasedPropertiesDeep} from './source/delimiter-cased-properties-
export {Join} from './source/join';
export {Split} from './source/split';
export {Trim} from './source/trim';
export {Replace} from './source/replace';
export {Includes} from './source/includes';
export {Get} from './source/get';
export {LastArrayElement} from './source/last-array-element';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -210,6 +210,7 @@ Click the type names for complete docs.

- [`Trim`](source/trim.d.ts) - Remove leading and trailing spaces from a string.
- [`Split`](source/split.d.ts) - Represents an array of strings split using a given character or character set.
- [`Replace`](source/replace.d.ts) - Represents a string with some or all matches replaced by a replacement.

### Array

Expand Down
67 changes: 67 additions & 0 deletions source/replace.d.ts
@@ -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;
28 changes: 28 additions & 0 deletions test-d/replace.ts
@@ -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', ' ', ''));

0 comments on commit 5c793ce

Please sign in to comment.