Skip to content

Commit

Permalink
fix: standardize string utility function formatting and improve reada…
Browse files Browse the repository at this point in the history
…bility
  • Loading branch information
rsaz committed Nov 15, 2024
1 parent aec5d24 commit a95accc
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/generate/utils/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* @returns The converted string in camelCase.
*/
export function anyCaseToCamelCase(str: string): string {
return str
.replace(/[-_]+(.)?/g, (_, char) => (char ? char.toUpperCase() : ''))
.replace(/^[A-Z]/, (char) => char.toLowerCase());
return str
.replace(/[-_]+(.)?/g, (_, char) => (char ? char.toUpperCase() : ""))
.replace(/^[A-Z]/, (char) => char.toLowerCase());
}

/**
Expand All @@ -15,10 +15,10 @@ export function anyCaseToCamelCase(str: string): string {
* @returns The converted string in kebab-case.
*/
export function anyCaseToKebabCase(str: string): string {
return str
.replace(/([a-z0-9])([A-Z])/g, '$1-$2') // Convert camelCase and PascalCase to kebab-case
.replace(/_/g, '-') // Convert snake_case to kebab-case
.toLowerCase(); // Ensure all characters are lowercase
return str
.replace(/([a-z0-9])([A-Z])/g, "$1-$2") // Convert camelCase and PascalCase to kebab-case
.replace(/_/g, "-") // Convert snake_case to kebab-case
.toLowerCase(); // Ensure all characters are lowercase
}

/**
Expand All @@ -27,9 +27,9 @@ export function anyCaseToKebabCase(str: string): string {
* @returns The converted string in PascalCase.
*/
export function anyCaseToPascalCase(str: string): string {
return str
.replace(/[-_]+(.)?/g, (_, char) => (char ? char.toUpperCase() : ''))
.replace(/^[a-z]/, (char) => char.toUpperCase());
return str
.replace(/[-_]+(.)?/g, (_, char) => (char ? char.toUpperCase() : ""))
.replace(/^[a-z]/, (char) => char.toUpperCase());
}

/**
Expand All @@ -38,10 +38,10 @@ export function anyCaseToPascalCase(str: string): string {
* @returns The converted string in snake_case.
*/
export function anyCaseToSnakeCase(str: string): string {
return str
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
.replace(/[-]+/g, '_')
.toLowerCase();
return str
.replace(/([a-z0-9])([A-Z])/g, "$1_$2")
.replace(/[-]+/g, "_")
.toLowerCase();
}

/**
Expand All @@ -50,7 +50,9 @@ export function anyCaseToSnakeCase(str: string): string {
* @returns The converted string in UPPER CASE.
*/
export function anyCaseToUpperCase(str: string): string {
return str.replace(/[-_]+(.)?/g, (_, char) => (char ? char.toUpperCase() : '')).toUpperCase();
return str
.replace(/[-_]+(.)?/g, (_, char) => (char ? char.toUpperCase() : ""))
.toUpperCase();
}

/**
Expand All @@ -59,5 +61,7 @@ export function anyCaseToUpperCase(str: string): string {
* @returns The converted string in lower case.
*/
export function anyCaseToLowerCase(str: string): string {
return str.replace(/[-_]+(.)?/g, (_, char) => (char ? char.toLowerCase() : '')).toLowerCase();
}
return str
.replace(/[-_]+(.)?/g, (_, char) => (char ? char.toLowerCase() : ""))
.toLowerCase();
}

0 comments on commit a95accc

Please sign in to comment.