-
-
Notifications
You must be signed in to change notification settings - Fork 702
Expand file tree
/
Copy pathremove-suffix.d.ts
More file actions
115 lines (87 loc) · 3.46 KB
/
remove-suffix.d.ts
File metadata and controls
115 lines (87 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import type {ApplyDefaultOptions} from './internal/object.d.ts';
import type {IfNotAnyOrNever, Not} from './internal/type.d.ts';
import type {IsStringLiteral} from './is-literal.d.ts';
import type {IsNever} from './is-never.d.ts';
import type {Or} from './or.d.ts';
import type {If} from './if.d.ts';
/**
@see {@link RemoveSuffix}
*/
export type RemoveSuffixOptions = {
/**
When enabled, instantiations with non-literal suffixes (e.g., `string`, `Uppercase<string>`, `` `.${string}` ``) simply return `string`, since their precise structure cannot be statically determined.
Note: Disabling this option can produce misleading results that might not reflect the actual runtime behavior.
For example, ``RemoveSuffix<'report.pdf', `.${string}`, {strict: false}>`` returns `'report'`, but at runtime, suffix could be `'.txt'` (which satisfies `` `.${string}` ``) and removing `'.txt'` from `'report.pdf'` would not result in `'report'`.
So, it is recommended to not disable this option unless you are aware of the implications.
@default true
@example
```
import type {RemoveSuffix} from 'type-fest';
type A = RemoveSuffix<'report.pdf', `.${string}`, {strict: true}>;
//=> string
type B = RemoveSuffix<'report.pdf', `.${string}`, {strict: false}>;
//=> 'report'
type C = RemoveSuffix<'on-change', string, {strict: true}>;
//=> string
type D = RemoveSuffix<'on-change', string, {strict: false}>;
//=> 'o'
type E = RemoveSuffix<`${number}/${string}`, `/${string}`, {strict: true}>;
//=> string
type F = RemoveSuffix<`${number}/${string}`, `/${string}`, {strict: false}>;
//=> `${number}`
```
Note: This option has no effect when only the input string type is non-literal. For example, ``RemoveSuffix<`${string}.pdf`, '.pdf'>`` will always return `string`.
@example
```
import type {RemoveSuffix} from 'type-fest';
type A = RemoveSuffix<`${string}.pdf`, '.pdf', {strict: true}>;
//=> string
type B = RemoveSuffix<`${string}.pdf`, '.pdf', {strict: false}>;
//=> string
type C = RemoveSuffix<`${number}px`, 'px', {strict: true}>;
//=> `${number}`
type D = RemoveSuffix<`${number}px`, 'px', {strict: false}>;
//=> `${number}`
```
*/
strict?: boolean;
};
type DefaultRemoveSuffixOptions = {
strict: true;
};
/**
Remove the specified suffix from the end of a string.
@example
```
import type {RemoveSuffix} from 'type-fest';
type A = RemoveSuffix<'report.pdf', '.pdf'>;
//=> 'report'
type B = RemoveSuffix<'bg-blue-500' | 'text-green-500' | 'border-slate-500', '-500'>;
//=> 'bg-blue' | 'border-slate' | 'text-green'
type C = RemoveSuffix<'report.pdf', '.txt'>;
//=> 'report.pdf'
type D = RemoveSuffix<`api/${string}/analytics`, '/analytics'>;
//=> `api/${string}`
```
@see {@link RemoveSuffixOptions}
@category String
@category Template literal
*/
export type RemoveSuffix<S extends string, Suffix extends string, Options extends RemoveSuffixOptions = {}> =
IfNotAnyOrNever<
S,
If<
IsNever<Suffix>,
S,
_RemoveSuffix<S, Suffix, ApplyDefaultOptions<RemoveSuffixOptions, DefaultRemoveSuffixOptions, Options>>
>
>;
type _RemoveSuffix<S extends string, Suffix extends string, Options extends Required<RemoveSuffixOptions>> =
Suffix extends string // For distributing `Suffix`
? Or<IsStringLiteral<Suffix>, Not<Options['strict']>> extends true
? S extends `${infer Rest}${Suffix}`
? Rest
: S // Return back `S` when `Suffix` is not present at the end of `S`
: string // Fallback to `string` when `Suffix` is non-literal and `strict` is enabled
: never;
export {};