Files
revenue-fe/src/utils/String.ts
2025-02-01 16:44:51 +07:00

23 lines
759 B
TypeScript

export const camelToSnakeCase = (str: string) => {
return str.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();
};
export const snakeToCamelCase = (str: string) => {
return str.replace(/(_\w)/g, (match) => match[1].toUpperCase());
};
export const toCamelCase = (str: string | undefined | null): string => {
if (!str) return '';
return str
.toLowerCase()
.replace(/(?:^|[^a-zA-Z0-9])([a-zA-Z0-9])/g, (match, group1) => group1.toUpperCase())
.replace(/[^a-zA-Z0-9]/g, '');
};
export const snakeToTitleCase = (str: string) => {
return String(str)
.split('_') // Split the string by underscores
.map((word) => word.charAt(0).toUpperCase() + word.slice(1)) // Capitalize each word
.join(' '); // Join the words with spaces
};