super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
66 lines (65 loc) • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.capitalize = capitalize;
exports.capitalizeFirst = capitalizeFirst;
exports.titleCase = titleCase;
/**
* Converts the first character of string to upper case and the remaining to lower case.
*
* @param string - The string to capitalize
* @returns The capitalized string
*
* @example
* ```ts
* capitalize('FRED');
* // => 'Fred'
*
* capitalize('fred');
* // => 'Fred'
* ```
*/
function capitalize(string) {
if (!string) {
return '';
}
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
/**
* Converts the first character of string to upper case, leaving the rest unchanged.
*
* @param string - The string to capitalize
* @returns The capitalized string
*
* @example
* ```ts
* capitalizeFirst('fred');
* // => 'Fred'
*
* capitalizeFirst('FRED');
* // => 'FRED'
* ```
*/
function capitalizeFirst(string) {
if (!string) {
return '';
}
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Converts the first character of each word in the string to upper case.
*
* @param string - The string to convert
* @returns The title cased string
*
* @example
* ```ts
* titleCase('fred, barney, and pebbles');
* // => 'Fred, Barney, And Pebbles'
* ```
*/
function titleCase(string) {
if (!string) {
return '';
}
return string.replace(/\\b\\w/g, match => match.toUpperCase());
}