import type {AsciiPunctuationCharacter} from './ascii-punctuation-character.d.ts'; import type {Or} from './or.d.ts'; import type {UnwrapBrandedPrimitive} from './unwrap-branded-primitive.d.ts'; import type {ApplyDefaultOptions} from './apply-default-options.d.ts'; import type {StartsWith} from './starts-with.d.ts'; import type {IsStringLiteral} from './is-string-literal.d.ts'; import type {Merge} from './merge.d.ts'; import type {_DefaultWordsOptions, _Words, WordsOptions} from './words.d.ts'; import type {KebabCase} from './kebab-case.d.ts'; // For `@link` import type {SnakeCase} from './snake-case.d.ts'; // For `@link` export type _DefaultDelimiterCaseOptions = Merge<_DefaultWordsOptions, {splitOnNumbers: false}>; /** Convert an array of words to delimiter case, lowercasing words while preserving the delimiter. */ type DelimiterCaseFromArray< WordList extends string[], Delimiter extends string, OutputString extends string = '', > = WordList extends [ infer FirstWord extends string, ...infer RemainingWords extends string[], ] ? DelimiterCaseFromArray : `${OutputString}${ // With `splitOnPunctuation: false`, a delimiter is not emitted next to punctuation, or the two double up. `OutputString` ends with the last character of the previous word, so testing it covers the word that ends in punctuation, and `FirstWord` covers the one that begins with it. Or, EndsWithPunctuation> extends true ? '' : Delimiter }${Lowercase}`> : OutputString; /** Whether the given string ends in punctuation. */ type EndsWithPunctuation = String_ extends `${string}${AsciiPunctuationCharacter}` ? true : false; /** Convert a string literal to a custom string delimiter casing. This can be useful when, for example, converting a camel-cased object property to an oddly cased one. @see {@link KebabCase} @see {@link SnakeCase} @example ``` import type {DelimiterCase} from 'type-fest'; // Simple const someVariable: DelimiterCase<'fooBar', '#'> = 'foo#bar'; const someVariableNoSplitOnNumbers: DelimiterCase<'p2pNetwork', '#', {splitOnNumbers: false}> = 'p2p#network'; const someVariableWithPunctuation: DelimiterCase<'div.card::after', '#'> = 'div#card#after'; const someVariableNoSplitOnPunctuation: DelimiterCase<'div.card::after', '#', {splitOnPunctuation: false}> = 'div.card::after'; // Advanced type OddlyCasedProperties = { [Key in keyof Value as DelimiterCase]: Value[Key]; }; type SomeOptions = { dryRun: boolean; includeFile: string; foo: number; }; const rawCliOptions: OddlyCasedProperties = { 'dry#run': true, 'include#file': 'bar.js', foo: 123, }; ``` @category Change case @category Template literal */ export type DelimiterCase< Value, Delimiter extends string, Options extends WordsOptions = {}, > = Value extends string ? Delimiter extends unknown // For distributing `Delimiter` ? IsStringLiteral extends false ? Value : DelimiterCaseFromArray< _Words, ApplyDefaultOptions>, Delimiter extends object ? UnwrapBrandedPrimitive : Delimiter > : never : Value; export {};