-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathcapitalize.js
More file actions
32 lines (31 loc) · 1.03 KB
/
capitalize.js
File metadata and controls
32 lines (31 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import coerceToBoolean from 'helper/boolean/coerce_to_boolean';
import coerceToString from 'helper/string/coerce_to_string';
/**
* 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'
*/
export default function capitalize(subject, restToLower) {
let subjectString = coerceToString(subject);
const restToLowerCaseBoolean = coerceToBoolean(restToLower);
if (subjectString === '') {
return '';
}
if (restToLowerCaseBoolean) {
subjectString = subjectString.toLowerCase();
}
return subjectString.substr(0, 1).toUpperCase() + subjectString.substr(1);
}