1 | import type {UpperCaseCharacters, WordSeparators} from './internal';
|
2 |
|
3 |
|
4 | type UpperCaseToLowerCase<T extends string> = T extends Uppercase<T> ? Lowercase<T> : T;
|
5 |
|
6 |
|
7 | type SplitIncludingDelimiters_<Source extends string, Delimiter extends string> =
|
8 | Source extends '' ? [] :
|
9 | Source extends `${infer FirstPart}${Delimiter}${infer SecondPart}` ?
|
10 | (
|
11 | Source extends `${FirstPart}${infer UsedDelimiter}${SecondPart}`
|
12 | ? UsedDelimiter extends Delimiter
|
13 | ? Source extends `${infer FirstPart}${UsedDelimiter}${infer SecondPart}`
|
14 | ? [...SplitIncludingDelimiters<FirstPart, Delimiter>, UsedDelimiter, ...SplitIncludingDelimiters<SecondPart, Delimiter>]
|
15 | : never
|
16 | : never
|
17 | : never
|
18 | ) :
|
19 | [Source];
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export type SplitIncludingDelimiters<Source extends string, Delimiter extends string> = SplitIncludingDelimiters_<UpperCaseToLowerCase<Source>, Delimiter>;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | type StringPartToDelimiterCase<StringPart extends string, Start extends boolean, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
|
34 | StringPart extends UsedWordSeparators ? Delimiter :
|
35 | Start extends true ? Lowercase<StringPart> :
|
36 | StringPart extends UsedUpperCaseCharacters ? `${Delimiter}${Lowercase<StringPart>}` :
|
37 | StringPart;
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | type StringArrayToDelimiterCase<Parts extends readonly any[], Start extends boolean, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
|
47 | Parts extends [`${infer FirstPart}`, ...infer RemainingParts]
|
48 | ? `${StringPartToDelimiterCase<FirstPart, Start, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}${StringArrayToDelimiterCase<RemainingParts, false, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}`
|
49 | : Parts extends [string]
|
50 | ? string
|
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 | export type DelimiterCase<Value, Delimiter extends string> = string extends Value ? Value : Value extends string
|
92 | ? StringArrayToDelimiterCase<
|
93 | SplitIncludingDelimiters<Value, WordSeparators | UpperCaseCharacters>,
|
94 | true,
|
95 | WordSeparators,
|
96 | UpperCaseCharacters,
|
97 | Delimiter
|
98 | >
|
99 | : Value;
|