From fd47cf9733594513b74c24299aad089b298c8421 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 23 Oct 2023 22:48:20 +0900 Subject: [PATCH 1/5] feat: export type helpers --- src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.ts b/src/index.ts index e314b85..7180c57 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,7 @@ import { CamelCase, JoinByCase, PascalCase, SplitByCase } from "./types"; +export type { CamelCase, JoinByCase, PascalCase, SplitByCase }; + const NUMBER_CHAR_RE = /\d/; const STR_SPLITTERS = ["-", "_", "/", "."] as const; From 1a6ba3a1c25eb18a50306454c4652e8be97c21ff Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 23 Oct 2023 22:59:49 +0900 Subject: [PATCH 2/5] feat: add `SnakeCase` and `KebabCase` helpers --- src/index.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7180c57..21ea5dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -105,10 +105,12 @@ export function camelCase(string_?: T) { return lowerFirst(pascalCase(string_ || "")) as CamelCase; } +export type KebabCase = JoinByCase + export function kebabCase(): ""; export function kebabCase( string_: T -): JoinByCase; +): KebabCase; export function kebabCase< T extends string | readonly string[], Joiner extends string @@ -124,10 +126,12 @@ export function kebabCase< .join(joiner ?? "-") as JoinByCase); } +export type SnakeCase = JoinByCase + 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; } From 1c495f9d3a1d00b2eda25b17240d866916c58062 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 23 Oct 2023 23:02:14 +0900 Subject: [PATCH 3/5] style: lint --- src/index.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 21ea5dc..d503083 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,5 @@ import { CamelCase, JoinByCase, PascalCase, SplitByCase } from "./types"; -export type { CamelCase, JoinByCase, PascalCase, SplitByCase }; - const NUMBER_CHAR_RE = /\d/; const STR_SPLITTERS = ["-", "_", "/", "."] as const; @@ -105,7 +103,10 @@ export function camelCase(string_?: T) { return lowerFirst(pascalCase(string_ || "")) as CamelCase; } -export type KebabCase = JoinByCase +export type KebabCase = JoinByCase< + T, + "-" +>; export function kebabCase(): ""; export function kebabCase( @@ -126,7 +127,10 @@ export function kebabCase< .join(joiner ?? "-") as JoinByCase); } -export type SnakeCase = JoinByCase +export type SnakeCase = JoinByCase< + T, + "_" +>; export function snakeCase(): ""; export function snakeCase( @@ -135,3 +139,10 @@ export function snakeCase( export function snakeCase(string_?: T) { return kebabCase(string_ || "", "_") as SnakeCase; } + +export { + type CamelCase, + type JoinByCase, + type PascalCase, + type SplitByCase, +} from "./types"; From 210f7208b23d87745cd84f8bb6dd8d634a29e2e2 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 24 Oct 2023 09:48:16 +0900 Subject: [PATCH 4/5] refactor: move all types to `./types` and use consistent exports --- src/index.ts | 29 ++++++++++------------------- src/types.ts | 32 +++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/index.ts b/src/index.ts index d503083..c570589 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; @@ -103,11 +109,6 @@ export function camelCase(string_?: T) { return lowerFirst(pascalCase(string_ || "")) as CamelCase; } -export type KebabCase = JoinByCase< - T, - "-" ->; - export function kebabCase(): ""; export function kebabCase( string_: T @@ -115,7 +116,7 @@ export function 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 @@ -124,14 +125,9 @@ export function kebabCase< ? "" : ((Array.isArray(string_) ? string_ : splitByCase(string_ as string)) .map((p) => p.toLowerCase()) - .join(joiner ?? "-") as JoinByCase); + .join(joiner ?? "-") as KebabCase); } -export type SnakeCase = JoinByCase< - T, - "_" ->; - export function snakeCase(): ""; export function snakeCase( string_: T @@ -140,9 +136,4 @@ export function snakeCase(string_?: T) { return kebabCase(string_ || "", "_") as SnakeCase; } -export { - type CamelCase, - type JoinByCase, - type PascalCase, - type SplitByCase, -} from "./types"; +export * from "./types"; diff --git a/src/types.ts b/src/types.ts index 7511643..1459e3c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -96,6 +96,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 @@ -114,17 +126,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 = [ From 3e4cd493caab4cbbdc2243d3b2b6b9e7cde17aa4 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 15 Nov 2023 17:53:17 +0100 Subject: [PATCH 5/5] lint --- src/index.ts | 16 ++++++++-------- src/types.ts | 19 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index a274f4c..a32e360 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,11 +19,11 @@ export function isUppercase(char = ""): boolean | undefined { export function splitByCase(string_: T): SplitByCase; export function splitByCase< T extends string, - Separator extends readonly string[] + Separator extends readonly string[], >(string_: T, separators: Separator): SplitByCase; export function splitByCase< T extends string, - Separator extends readonly string[] + Separator extends readonly string[], >(string_: T, separators?: Separator) { const splitters = separators ?? STR_SPLITTERS; const parts: string[] = []; @@ -91,7 +91,7 @@ export function lowerFirst(string_: S): Uncapitalize { export function pascalCase(): ""; export function pascalCase( - string_: T + string_: T, ): PascalCase; export function pascalCase(string_?: T) { return string_ @@ -103,7 +103,7 @@ export function pascalCase(string_?: T) { export function camelCase(): ""; export function camelCase( - string_: T + string_: T, ): CamelCase; export function camelCase(string_?: T) { return lowerFirst(pascalCase(string_ || "")) as CamelCase; @@ -111,15 +111,15 @@ export function camelCase(string_?: T) { export function kebabCase(): ""; export function kebabCase( - string_: T + string_: T, ): KebabCase; export function kebabCase< T extends string | readonly string[], - Joiner extends string + Joiner extends string, >(string_: T, joiner: Joiner): KebabCase; export function kebabCase< T extends string | readonly string[], - Joiner extends string + Joiner extends string, >(string_?: T, joiner?: Joiner) { return string_ ? ((Array.isArray(string_) ? string_ : splitByCase(string_ as string)) @@ -130,7 +130,7 @@ export function kebabCase< export function snakeCase(): ""; export function snakeCase( - string_: T + string_: T, ): SnakeCase; export function snakeCase(string_?: T) { return kebabCase(string_ || "", "_") as SnakeCase; diff --git a/src/types.ts b/src/types.ts index 0a9e812..fcf36d4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -102,14 +102,14 @@ export type SplitByCase< 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; + ? string + : T extends string + ? SplitByCase extends readonly string[] + ? JoinLowercaseWords, Joiner> + : never + : T extends readonly string[] + ? JoinLowercaseWords + : never; export type PascalCase = string extends T ? string @@ -129,10 +129,9 @@ export type CamelCase = string extends T ? string : Uncapitalize>; - export type KebabCase< T extends string | readonly string[], - Joiner extends string = "-" + Joiner extends string = "-", > = JoinByCase; export type SnakeCase = JoinByCase<