Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export type helpers #58

Merged
merged 6 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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