Skip to content

Commit

Permalink
refactor: simplify string variable name
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 15, 2023
1 parent 8bdcf51 commit abb2d2b
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ export function isUppercase(char = ""): boolean | undefined {
return char.toUpperCase() === char;
}

export function splitByCase<T extends string>(string_: T): SplitByCase<T>;
export function splitByCase<T extends string>(str: T): SplitByCase<T>;
export function splitByCase<
T extends string,
Separator extends readonly string[],
>(string_: T, separators: Separator): SplitByCase<T, Separator[number]>;
>(str: T, separators: Separator): SplitByCase<T, Separator[number]>;
export function splitByCase<
T extends string,
Separator extends readonly string[],
>(string_: T, separators?: Separator) {
>(str: T, separators?: Separator) {
const splitters = separators ?? STR_SPLITTERS;
const parts: string[] = [];

if (!string_ || typeof string_ !== "string") {
if (!str || typeof str !== "string") {
return parts as SplitByCase<T, Separator[number]>;
}

Expand All @@ -37,7 +37,7 @@ export function splitByCase<
let previousUpper: boolean | undefined;
let previousSplitter: boolean | undefined;

for (const char of string_) {
for (const char of str) {
// Splitter
const isSplitter = (splitters as unknown as string).includes(char);
if (isSplitter === true) {
Expand Down Expand Up @@ -77,63 +77,59 @@ export function splitByCase<
return parts as SplitByCase<T, Separator[number]>;
}

export function upperFirst<S extends string>(string_: S): Capitalize<S> {
return (
string_ ? string_[0].toUpperCase() + string_.slice(1) : ""
) as Capitalize<S>;
export function upperFirst<S extends string>(str: S): Capitalize<S> {
return (str ? str[0].toUpperCase() + str.slice(1) : "") as Capitalize<S>;
}

export function lowerFirst<S extends string>(string_: S): Uncapitalize<S> {
return (
string_ ? string_[0].toLowerCase() + string_.slice(1) : ""
) as Uncapitalize<S>;
export function lowerFirst<S extends string>(str: S): Uncapitalize<S> {
return (str ? str[0].toLowerCase() + str.slice(1) : "") as Uncapitalize<S>;
}

export function pascalCase(): "";
export function pascalCase<T extends string | readonly string[]>(
string_: T,
str: T,
): PascalCase<T>;
export function pascalCase<T extends string | readonly string[]>(string_?: T) {
return string_
? ((Array.isArray(string_) ? string_ : splitByCase(string_ as string))
export function pascalCase<T extends string | readonly string[]>(str?: T) {
return str
? ((Array.isArray(str) ? str : splitByCase(str as string))
.map((p) => upperFirst(p))
.join("") as PascalCase<T>)
: "";
}

export function camelCase(): "";
export function camelCase<T extends string | readonly string[]>(
string_: T,
str: T,
): CamelCase<T>;
export function camelCase<T extends string | readonly string[]>(string_?: T) {
return lowerFirst(pascalCase(string_ || "")) as CamelCase<T>;
export function camelCase<T extends string | readonly string[]>(str?: T) {
return lowerFirst(pascalCase(str || "")) as CamelCase<T>;
}

export function kebabCase(): "";
export function kebabCase<T extends string | readonly string[]>(
string_: T,
str: T,
): KebabCase<T>;
export function kebabCase<
T extends string | readonly string[],
Joiner extends string,
>(string_: T, joiner: Joiner): KebabCase<T, Joiner>;
>(str: 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))
>(str?: T, joiner?: Joiner) {
return str
? ((Array.isArray(str) ? str : splitByCase(str as string))
.map((p) => p.toLowerCase())
.join(joiner ?? "-") as KebabCase<T, Joiner>)
: "";
}

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

export * from "./types";

0 comments on commit abb2d2b

Please sign in to comment.