|
| 1 | +import { UpperCaseCharacters, WordSeparators } from "./sourceUtilities"; |
| 2 | + |
| 3 | +/** |
| 4 | +Unlike a simpler split, this one includes the delimiter splitted on in the resulting array literal. This is to enable splitting on, for example, upper-case characters. |
| 5 | +
|
| 6 | +@category Template Literals |
| 7 | +*/ |
| 8 | +export type SplitIncludingDelimiters<Source extends string, Delimiter extends string> = Source extends "" |
| 9 | + ? [] |
| 10 | + : Source extends `${infer FirstPart}${Delimiter}${infer SecondPart}` |
| 11 | + ? Source extends `${FirstPart}${infer UsedDelimiter}${SecondPart}` |
| 12 | + ? UsedDelimiter extends Delimiter |
| 13 | + ? Source extends `${infer FirstPart}${UsedDelimiter}${infer SecondPart}` |
| 14 | + ? [ |
| 15 | + ...SplitIncludingDelimiters<FirstPart, Delimiter>, |
| 16 | + UsedDelimiter, |
| 17 | + ...SplitIncludingDelimiters<SecondPart, Delimiter> |
| 18 | + ] |
| 19 | + : never |
| 20 | + : never |
| 21 | + : never |
| 22 | + : [Source]; |
| 23 | + |
| 24 | +/** |
| 25 | +Format a specific part of the splitted string literal that `StringArrayToDelimiterCase<>` fuses together, ensuring desired casing. |
| 26 | +
|
| 27 | +@see StringArrayToDelimiterCase |
| 28 | +*/ |
| 29 | +type StringPartToDelimiterCase< |
| 30 | + StringPart extends string, |
| 31 | + UsedWordSeparators extends string, |
| 32 | + UsedUpperCaseCharacters extends string, |
| 33 | + Delimiter extends string |
| 34 | +> = StringPart extends UsedWordSeparators |
| 35 | + ? Delimiter |
| 36 | + : StringPart extends UsedUpperCaseCharacters |
| 37 | + ? `${Delimiter}${Lowercase<StringPart>}` |
| 38 | + : StringPart; |
| 39 | + |
| 40 | +/** |
| 41 | +Takes the result of a splitted string literal and recursively concatenates it together into the desired casing. |
| 42 | +
|
| 43 | +It receives `UsedWordSeparators` and `UsedUpperCaseCharacters` as input to ensure it's fully encapsulated. |
| 44 | +
|
| 45 | +@see SplitIncludingDelimiters |
| 46 | +*/ |
| 47 | +type StringArrayToDelimiterCase< |
| 48 | + Parts extends any[], |
| 49 | + UsedWordSeparators extends string, |
| 50 | + UsedUpperCaseCharacters extends string, |
| 51 | + Delimiter extends string |
| 52 | +> = Parts extends [`${infer FirstPart}`, ...infer RemainingParts] |
| 53 | + ? `${StringPartToDelimiterCase< |
| 54 | + FirstPart, |
| 55 | + UsedWordSeparators, |
| 56 | + UsedUpperCaseCharacters, |
| 57 | + Delimiter |
| 58 | + >}${StringArrayToDelimiterCase<RemainingParts, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}` |
| 59 | + : ""; |
| 60 | + |
| 61 | +/** |
| 62 | +Convert a string literal to a custom string delimiter casing. |
| 63 | +
|
| 64 | +This can be useful when, for example, converting a camel-cased object property to an oddly cased one. |
| 65 | +
|
| 66 | +@see KebabCase |
| 67 | +@see SnakeCase |
| 68 | +
|
| 69 | +@example |
| 70 | +``` |
| 71 | +import {DelimiterCase} from 'type-fest'; |
| 72 | +
|
| 73 | +// Simple |
| 74 | +
|
| 75 | +const someVariable: DelimiterCase<'fooBar', '#'> = 'foo#bar'; |
| 76 | +
|
| 77 | +// Advanced |
| 78 | +
|
| 79 | +type OddlyCasedProperties<T> = { |
| 80 | + [K in keyof T as DelimiterCase<K, '#'>]: T[K] |
| 81 | +}; |
| 82 | +
|
| 83 | +interface SomeOptions { |
| 84 | + dryRun: boolean; |
| 85 | + includeFile: string; |
| 86 | + foo: number; |
| 87 | +} |
| 88 | +
|
| 89 | +const rawCliOptions: OddlyCasedProperties<SomeOptions> = { |
| 90 | + 'dry#run': true, |
| 91 | + 'include#file': 'bar.js', |
| 92 | + foo: 123 |
| 93 | +}; |
| 94 | +``` |
| 95 | +
|
| 96 | +@category Template Literals |
| 97 | +*/ |
| 98 | +export type DelimiterCase<Value, Delimiter extends string> = Value extends string |
| 99 | + ? StringArrayToDelimiterCase< |
| 100 | + SplitIncludingDelimiters<Value, WordSeparators | UpperCaseCharacters>, |
| 101 | + WordSeparators, |
| 102 | + UpperCaseCharacters, |
| 103 | + Delimiter |
| 104 | + > |
| 105 | + : Value; |
0 commit comments