From abb2d2b50208a21d50daa7e7c7e734578a24f7ea Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 15 Nov 2023 18:12:12 +0100 Subject: [PATCH] refactor: simplify string variable name --- src/index.ts | 52 ++++++++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/src/index.ts b/src/index.ts index a32e360..a7f4fd3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,19 +16,19 @@ export function isUppercase(char = ""): boolean | undefined { return char.toUpperCase() === char; } -export function splitByCase(string_: T): SplitByCase; +export function splitByCase(str: T): SplitByCase; export function splitByCase< T extends string, Separator extends readonly string[], ->(string_: T, separators: Separator): SplitByCase; +>(str: T, separators: Separator): SplitByCase; 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; } @@ -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) { @@ -77,25 +77,21 @@ export function splitByCase< return parts as SplitByCase; } -export function upperFirst(string_: S): Capitalize { - return ( - string_ ? string_[0].toUpperCase() + string_.slice(1) : "" - ) as Capitalize; +export function upperFirst(str: S): Capitalize { + return (str ? str[0].toUpperCase() + str.slice(1) : "") as Capitalize; } -export function lowerFirst(string_: S): Uncapitalize { - return ( - string_ ? string_[0].toLowerCase() + string_.slice(1) : "" - ) as Uncapitalize; +export function lowerFirst(str: S): Uncapitalize { + return (str ? str[0].toLowerCase() + str.slice(1) : "") as Uncapitalize; } export function pascalCase(): ""; export function pascalCase( - string_: T, + str: T, ): PascalCase; -export function pascalCase(string_?: T) { - return string_ - ? ((Array.isArray(string_) ? string_ : splitByCase(string_ as string)) +export function pascalCase(str?: T) { + return str + ? ((Array.isArray(str) ? str : splitByCase(str as string)) .map((p) => upperFirst(p)) .join("") as PascalCase) : ""; @@ -103,26 +99,26 @@ export function pascalCase(string_?: T) { export function camelCase(): ""; export function camelCase( - string_: T, + str: T, ): CamelCase; -export function camelCase(string_?: T) { - return lowerFirst(pascalCase(string_ || "")) as CamelCase; +export function camelCase(str?: T) { + return lowerFirst(pascalCase(str || "")) as CamelCase; } export function kebabCase(): ""; export function kebabCase( - string_: T, + str: T, ): KebabCase; export function kebabCase< T extends string | readonly string[], Joiner extends string, ->(string_: T, joiner: Joiner): KebabCase; +>(str: T, joiner: Joiner): KebabCase; 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) : ""; @@ -130,10 +126,10 @@ export function kebabCase< export function snakeCase(): ""; export function snakeCase( - string_: T, + str: T, ): SnakeCase; -export function snakeCase(string_?: T) { - return kebabCase(string_ || "", "_") as SnakeCase; +export function snakeCase(str?: T) { + return kebabCase(str || "", "_") as SnakeCase; } export * from "./types";