diff --git a/src/index.ts b/src/index.ts index fdeaf73..a32e360 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,10 @@ -import { CamelCase, JoinByCase, PascalCase, SplitByCase } from "./types"; +import type { + CamelCase, + KebabCase, + PascalCase, + SnakeCase, + SplitByCase, +} from "./types"; const NUMBER_CHAR_RE = /\d/; const STR_SPLITTERS = ["-", "_", "/", "."] as const; @@ -106,11 +112,11 @@ export function camelCase(string_?: T) { export function kebabCase(): ""; export function kebabCase( string_: T, -): JoinByCase; +): KebabCase; export function kebabCase< T extends string | readonly string[], Joiner extends string, ->(string_: T, joiner: Joiner): JoinByCase; +>(string_: T, joiner: Joiner): KebabCase; export function kebabCase< T extends string | readonly string[], Joiner extends string, @@ -118,14 +124,16 @@ export function kebabCase< return string_ ? ((Array.isArray(string_) ? string_ : splitByCase(string_ as string)) .map((p) => p.toLowerCase()) - .join(joiner ?? "-") as JoinByCase) + .join(joiner ?? "-") as KebabCase) : ""; } export function snakeCase(): ""; export function snakeCase( string_: T, -): JoinByCase; +): SnakeCase; export function snakeCase(string_?: T) { - return kebabCase(string_ || "", "_") as JoinByCase; + return kebabCase(string_ || "", "_") as SnakeCase; } + +export * from "./types"; diff --git a/src/types.ts b/src/types.ts index eeb39ac..fcf36d4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -99,6 +99,18 @@ export type SplitByCase< : string[] : Accumulator; +type JoinByCase = string extends T + ? string + : string[] extends T + ? string + : T extends string + ? SplitByCase extends readonly string[] + ? JoinLowercaseWords, Joiner> + : never + : T extends readonly string[] + ? JoinLowercaseWords + : never; + export type PascalCase = string extends T ? string : string[] extends T @@ -117,17 +129,15 @@ export type CamelCase = string extends T ? string : Uncapitalize>; -export type JoinByCase = string extends T - ? string - : string[] extends T - ? string - : T extends string - ? SplitByCase extends readonly string[] - ? JoinLowercaseWords, Joiner> - : never - : T extends readonly string[] - ? JoinLowercaseWords - : never; +export type KebabCase< + T extends string | readonly string[], + Joiner extends string = "-", +> = JoinByCase; + +export type SnakeCase = JoinByCase< + T, + "_" +>; // eslint-disable-next-line @typescript-eslint/no-unused-vars type __tests = [