template-react-app/src/utils/StringTransformationUtils.ts

34 lines
1.1 KiB
TypeScript

/**
* Capitalizes the first letter of a given string.
*
* @param {string} string - The string to capitalize.
* @returns {string} The string with the first letter capitalized.
*/
export function capitalizeFirstLetter(string: string): string {
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Clears multiple consecutive slashes in a path string.
*
* @param {string} path - The path string to be formatted.
* @returns {string} The path with consecutive slashes reduced to a single slash.
* @example clearMultiplePathSlashes("/admin//MAINTENANCE") => "/admin/MAINTENANCE"
*/
export const clearMultiplePathSlashes = (path: string): string => {
return path.replace(/\/{2,}/g, "/");
};
/**
* Trims parameters and queries from a URL path that start with ':' or '?'.
*
* @param {string} path - The URL path to trim.
* @returns {string} The URL path without parameters and queries.
* @example
* // returns "/path"
* trimPathOfParameters("/path/:param1/:param2:param3/?param4")
*/
export const trimPathOfParameters = (path: string): string => {
return path.replace(/\/:[^/]*|\?[^/]*/g, "");
};