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 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | export type Words<
|
40 | Sentence extends string,
|
41 | LastCharacter extends string = '',
|
42 | CurrentWord extends string = '',
|
43 | > = Sentence extends `${infer FirstCharacter}${infer RemainingCharacters}`
|
44 | ? FirstCharacter extends WordSeparators
|
45 |
|
46 | ? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters>]
|
47 | : LastCharacter extends ''
|
48 |
|
49 | ? Words<RemainingCharacters, FirstCharacter, FirstCharacter>
|
50 |
|
51 | : [false, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
|
52 | ? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
|
53 |
|
54 | : [true, false] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
|
55 | ? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
|
56 |
|
57 | : [true, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
|
58 | ? Words<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
|
59 |
|
60 | : [true, true] extends [IsLowerCase<LastCharacter>, IsUpperCase<FirstCharacter>]
|
61 | ? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
|
62 |
|
63 | : [true, true] extends [IsUpperCase<LastCharacter>, IsLowerCase<FirstCharacter>]
|
64 | ? [...RemoveLastCharacter<CurrentWord, LastCharacter>, ...Words<RemainingCharacters, FirstCharacter, `${LastCharacter}${FirstCharacter}`>]
|
65 |
|
66 | : Words<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
|
67 | : [...SkipEmptyWord<CurrentWord>];
|