1 | import type {Join} from './join';
|
2 | import type {ArraySlice} from './array-slice';
|
3 | import type {StringToArray} from './internal';
|
4 |
|
5 | /**
|
6 | Returns a string slice of a given range, just like `String#slice()`.
|
7 |
|
8 | @see {ArraySlice}
|
9 |
|
10 | @example
|
11 | ```
|
12 | import type {StringSlice} from 'type-fest';
|
13 |
|
14 | StringSlice<'abcde', 0, 2>;
|
15 | //=> 'ab'
|
16 |
|
17 | StringSlice<'abcde', 1>;
|
18 | //=> 'bcde'
|
19 |
|
20 | StringSlice<'abcde', 0, -1>;
|
21 | //=> 'abcd'
|
22 |
|
23 | StringSlice<'abcde', -2, -1>;
|
24 | //=> 'd'
|
25 | ```
|
26 |
|
27 | @category String
|
28 | */
|
29 | export type StringSlice<
|
30 | S extends string,
|
31 | Start extends number = 0,
|
32 | End extends number = StringToArray<S>['length'],
|
33 | > = string extends S
|
34 | ? string[]
|
35 | : ArraySlice<StringToArray<S>, Start, End> extends infer R extends readonly string[]
|
36 | ? Join<R, ''>
|
37 | : never;
|