Skip to content

Commit

Permalink
feat: export type helpers (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Nov 15, 2023
1 parent ba39ce6 commit a83e505
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
20 changes: 14 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -106,26 +112,28 @@ export function camelCase<T extends string | readonly string[]>(string_?: T) {
export function kebabCase(): "";
export function kebabCase<T extends string | readonly string[]>(
string_: T,
): JoinByCase<T, "-">;
): KebabCase<T>;
export function kebabCase<
T extends string | readonly string[],
Joiner extends string,
>(string_: T, joiner: Joiner): JoinByCase<T, Joiner>;
>(string_: T, joiner: Joiner): KebabCase<T, Joiner>;
export function kebabCase<
T extends string | readonly string[],
Joiner extends string,
>(string_?: T, joiner?: Joiner) {
return string_
? ((Array.isArray(string_) ? string_ : splitByCase(string_ as string))
.map((p) => p.toLowerCase())
.join(joiner ?? "-") as JoinByCase<T, Joiner>)
.join(joiner ?? "-") as KebabCase<T, Joiner>)
: "";
}

export function snakeCase(): "";
export function snakeCase<T extends string | readonly string[]>(
string_: T,
): JoinByCase<T, "_">;
): SnakeCase<T>;
export function snakeCase<T extends string | readonly string[]>(string_?: T) {
return kebabCase(string_ || "", "_") as JoinByCase<T, "_">;
return kebabCase(string_ || "", "_") as SnakeCase<T>;
}

export * from "./types";
32 changes: 21 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ export type SplitByCase<
: string[]
: Accumulator;

type JoinByCase<T, Joiner extends string> = string extends T
? string
: string[] extends T
? string
: T extends string
? SplitByCase<T> extends readonly string[]
? JoinLowercaseWords<SplitByCase<T>, Joiner>
: never
: T extends readonly string[]
? JoinLowercaseWords<T, Joiner>
: never;

export type PascalCase<T> = string extends T
? string
: string[] extends T
Expand All @@ -117,17 +129,15 @@ export type CamelCase<T> = string extends T
? string
: Uncapitalize<PascalCase<T>>;

export type JoinByCase<T, Joiner extends string> = string extends T
? string
: string[] extends T
? string
: T extends string
? SplitByCase<T> extends readonly string[]
? JoinLowercaseWords<SplitByCase<T>, Joiner>
: never
: T extends readonly string[]
? JoinLowercaseWords<T, Joiner>
: never;
export type KebabCase<
T extends string | readonly string[],
Joiner extends string = "-",
> = JoinByCase<T, Joiner>;

export type SnakeCase<T extends string | readonly string[]> = JoinByCase<
T,
"_"
>;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type __tests = [
Expand Down

0 comments on commit a83e505

Please sign in to comment.