39948-vm/frontend/src/helpers/textFormatters.ts

54 lines
1.1 KiB
TypeScript

/**
* Text Formatting Helpers
*
* Common text transformation utilities.
*/
/**
* Convert a plural title to singular by removing the last character.
* Simple approach for entity names like "roles" -> "role".
*
* @example
* singularize('View roles') // 'View role'
* singularize('assets') // 'asset'
*/
export function singularize(pluralTitle: string): string {
return pluralTitle.slice(0, -1);
}
/**
* Capitalize the first letter of a string.
*
* @example
* capitalize('hello') // 'Hello'
*/
export function capitalize(str: string): string {
if (!str) return '';
return str.charAt(0).toUpperCase() + str.slice(1);
}
/**
* Convert snake_case to Title Case.
*
* @example
* snakeToTitle('tour_pages') // 'Tour Pages'
*/
export function snakeToTitle(str: string): string {
return str.split('_').map(capitalize).join(' ');
}
/**
* Convert camelCase to Title Case.
*
* @example
* camelToTitle('tourPages') // 'Tour Pages'
*/
export function camelToTitle(str: string): string {
return str
.replace(/([A-Z])/g, ' $1')
.trim()
.split(' ')
.map(capitalize)
.join(' ');
}