-
-
Notifications
You must be signed in to change notification settings - Fork 547
/
words.d.ts
67 lines (56 loc) · 2.71 KB
/
words.d.ts
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
import type {IsLowerCase, IsNumeric, IsUpperCase, WordSeparators} from './internal';
type SkipEmptyWord<Word extends string> = Word extends '' ? [] : [Word];
type RemoveLastCharacter<Sentence extends string, Character extends string> = Sentence extends `${infer LeftSide}${Character}`
? SkipEmptyWord<LeftSide>
: never;
/**
Split a string (almost) like Lodash's `_.words()` function.
- Split on each word that begins with a capital letter.
- Split on each {@link WordSeparators}.
- Split on numeric sequence.
@example
```
import type {Words} from 'type-fest';
type Words0 = Words<'helloWorld'>;
//=> ['hello', 'World']
type Words1 = Words<'helloWORLD'>;
//=> ['hello', 'WORLD']
type Words2 = Words<'hello-world'>;
//=> ['hello', 'world']
type Words3 = Words<'--hello the_world'>;
//=> ['hello', 'the', 'world']
type Words4 = Words<'lifeIs42'>;
//=> ['life', 'Is', '42']
```
@category Change case
@category Template literal
*/
export type Words<
Sentence extends string,
LastCharacter extends string = '',
CurrentWord extends string = '',
> = Sentence extends `${infer FirstCharacter}${infer RemainingCharacters}`
? FirstCharacter extends WordSeparators
// Skip word separator
? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters>]
: LastCharacter extends ''
// Fist char of word
? Words<RemainingCharacters, FirstCharacter, FirstCharacter>
// Case change: non-numeric to numeric, push word
: [false, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
// Case change: numeric to non-numeric, push word
: [true, false] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
// No case change: concat word
: [true, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
? Words<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
// Case change: lower to upper, push word
: [true, true] extends [IsLowerCase<LastCharacter>, IsUpperCase<FirstCharacter>]
? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
// Case change: upper to lower, brings back the last character, push word
: [true, true] extends [IsUpperCase<LastCharacter>, IsLowerCase<FirstCharacter>]
? [...RemoveLastCharacter<CurrentWord, LastCharacter>, ...Words<RemainingCharacters, FirstCharacter, `${LastCharacter}${FirstCharacter}`>]
// No case change: concat word
: Words<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
: [...SkipEmptyWord<CurrentWord>];