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
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,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
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,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
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;
28 changes: 28 additions & 0 deletions test-d/replace.ts
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', ' ', ''));