1 | import type {IsLowerCase, IsNumeric, IsUpperCase, WordSeparators} from './internal';
|
2 |
|
3 | type SkipEmptyWord<Word extends string> = Word extends '' ? [] : [Word];
|
4 |
|
5 | type RemoveLastCharacter<Sentence extends string, Character extends string> = Sentence extends `${infer LeftSide}${Character}`
|
6 | ? SkipEmptyWord<LeftSide>
|
7 | : never;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export type SplitWords<
|
30 | Sentence extends string,
|
31 | LastCharacter extends string = '',
|
32 | CurrentWord extends string = '',
|
33 | > = Sentence extends `${infer FirstCharacter}${infer RemainingCharacters}`
|
34 | ? FirstCharacter extends WordSeparators
|
35 |
|
36 | ? [...SkipEmptyWord<CurrentWord>, ...SplitWords<RemainingCharacters>]
|
37 | : LastCharacter extends ''
|
38 |
|
39 | ? SplitWords<RemainingCharacters, FirstCharacter, FirstCharacter>
|
40 |
|
41 | : [false, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
|
42 | ? [...SkipEmptyWord<CurrentWord>, ...SplitWords<RemainingCharacters, FirstCharacter, FirstCharacter>]
|
43 |
|
44 | : [true, false] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
|
45 | ? [...SkipEmptyWord<CurrentWord>, ...SplitWords<RemainingCharacters, FirstCharacter, FirstCharacter>]
|
46 |
|
47 | : [true, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
|
48 | ? SplitWords<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
|
49 |
|
50 | : [true, true] extends [IsLowerCase<LastCharacter>, IsUpperCase<FirstCharacter>]
|
51 | ? [...SkipEmptyWord<CurrentWord>, ...SplitWords<RemainingCharacters, FirstCharacter, FirstCharacter>]
|
52 |
|
53 | : [true, true] extends [IsUpperCase<LastCharacter>, IsLowerCase<FirstCharacter>]
|
54 | ? [...RemoveLastCharacter<CurrentWord, LastCharacter>, ...SplitWords<RemainingCharacters, FirstCharacter, `${LastCharacter}${FirstCharacter}`>]
|
55 |
|
56 | : SplitWords<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
|
57 | : [...SkipEmptyWord<CurrentWord>];
|