Skip to content

Commit

Permalink
feat: refactor string case utilities and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
rsaz committed Nov 15, 2024
1 parent 6461e50 commit aec5d24
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 5 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
"test:watch": "jest --watch"
},
"dependencies": {
"@expressots/boost-ts": "file:../boost-ts/expressots-boost-ts-1.3.0.tgz",
"axios": "^1.7.3",
"axios": "1.7.7",
"chalk-animation": "2.0.3",
"cli-progress": "3.12.0",
"cli-table3": "0.6.5",
Expand Down
2 changes: 1 addition & 1 deletion src/generate/utils/command-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
anyCaseToKebabCase,
anyCaseToPascalCase,
anyCaseToLowerCase,
} from "@expressots/boost-ts";
} from "./string-utils";

import { printError } from "../../utils/cli-ui";
import { verifyIfFileExists } from "../../utils/verify-file-exists";
Expand Down
2 changes: 1 addition & 1 deletion src/generate/utils/nonopininated-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
anyCaseToCamelCase,
anyCaseToKebabCase,
anyCaseToPascalCase,
} from "@expressots/boost-ts";
} from "./string-utils";
import { ExpressoConfig } from "@expressots/shared";

import { printGenerateSuccess } from "../../utils/cli-ui";
Expand Down
2 changes: 1 addition & 1 deletion src/generate/utils/opinionated-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
anyCaseToCamelCase,
anyCaseToKebabCase,
anyCaseToPascalCase,
} from "@expressots/boost-ts";
} from "./string-utils";
import * as nodePath from "node:path";
import fs from "fs";
import { printGenerateSuccess } from "../../utils/cli-ui";
Expand Down
63 changes: 63 additions & 0 deletions src/generate/utils/string-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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 aec5d24

Please sign in to comment.