voca
Version:
The ultimate JavaScript string library
41 lines (34 loc) • 1.15 kB
JavaScript
import './internal/is_nil.js';
import { c as coerceToBoolean } from './internal/coerce_to_boolean.js';
import './is_string.js';
import { c as coerceToString } from './internal/coerce_to_string.js';
/**
* Converts the first character of `subject` to upper case. If `restToLower` is `true`, convert the rest of
* `subject` to lower case.
*
* @function capitalize
* @static
* @since 1.0.0
* @memberOf Case
* @param {string} [subject=''] The string to capitalize.
* @param {boolean} [restToLower=false] Convert the rest of `subject` to lower case.
* @return {string} Returns the capitalized string.
* @example
* v.capitalize('apple');
* // => 'Apple'
*
* v.capitalize('aPPle', true);
* // => 'Apple'
*/
function capitalize(subject, restToLower) {
var subjectString = coerceToString(subject);
var restToLowerCaseBoolean = coerceToBoolean(restToLower);
if (subjectString === '') {
return '';
}
if (restToLowerCaseBoolean) {
subjectString = subjectString.toLowerCase();
}
return subjectString.substr(0, 1).toUpperCase() + subjectString.substr(1);
}
export default capitalize;