Skip to content

Commit

Permalink
add convertion string cases
Browse files Browse the repository at this point in the history
  • Loading branch information
devbr-io committed Apr 7, 2023
1 parent ace2016 commit 6226c33
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./match-pattern";
export * from "./optional-pattern";
export * from "./optional-pattern";
export * from "./string";
1 change: 1 addition & 0 deletions src/string/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./naming-convention";
65 changes: 65 additions & 0 deletions src/string/naming-convention.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Converts a string from any case (camelCase, PascalCase, kebab-case, snake_case) to camelCase.
* @param str - The input string to be converted.
* @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());
}

/**
* Converts a string from any case (camelCase, PascalCase, kebab-case, snake_case) to kebab-case.
* @param str - The input string to be converted.
* @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
}

/**
* Converts a string from any case (camelCase, PascalCase, kebab-case, snake_case) to PascalCase.
* @param str - The input string to be converted.
* @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());
}

/**
* Converts a string from any case (camelCase, PascalCase, kebab-case, snake_case) to snake_case.
* @param str - The input string to be converted.
* @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();
}

/**
* Converts a string from any case (camelCase, PascalCase, kebab-case, snake_case) to UPPER CASE.
* @param str - The input string to be converted.
* @returns The converted string in UPPER CASE.
*/
export function anyCaseToUpperCase(str: string): string {
return str.replace(/[-_]+(.)?/g, (_, char) => (char ? char.toUpperCase() : '')).toUpperCase();
}

/**
* Converts a string from any case (camelCase, PascalCase, kebab-case, snake_case) to lower case.
* @param str - The input string to be converted.
* @returns The converted string in lower case.
*/
export function anyCaseToLowerCase(str: string): string {
return str.replace(/[-_]+(.)?/g, (_, char) => (char ? char.toLowerCase() : '')).toLowerCase();
}


0 comments on commit 6226c33

Please sign in to comment.