|
2 | 2 | * Converts provided underscore format string to camel case |
3 | 3 | * |
4 | 4 | * @param {string} value The value to convert |
5 | | - * @return {boolean} Returns `true` if `value` is a valid iso date, else `false`. |
| 5 | + * @return {string} Returns the title cased string. |
6 | 6 | * @example |
7 | 7 | * ```ts |
8 | 8 | * underscoreToCamelCase("hello_world"); |
9 | 9 | * // => Hello World |
10 | 10 | * ``` |
11 | 11 | */ |
12 | | -export function underscoreToCamelCase(value: string): string { |
13 | | - value = value.charAt(0).toUpperCase() + value.slice(1); |
14 | | - value = value.replace(/_(.)/g, (_match: string, letter) => { |
| 12 | +export function underscoreToTitleCase( |
| 13 | + value: string | null | undefined, |
| 14 | +): string { |
| 15 | + let strValue = _toString(value); |
| 16 | + strValue = strValue.charAt(0).toUpperCase() + strValue.slice(1); |
| 17 | + strValue = strValue.replace(/_(.)/g, (_match: string, letter) => { |
15 | 18 | return ` ${letter.toUpperCase()}`; |
16 | 19 | }); |
17 | | - return value.replace("_", " "); |
| 20 | + return strValue.replace("_", " "); |
| 21 | +} |
| 22 | + |
| 23 | +/** Used as references for various `Number` constants. */ |
| 24 | +const INFINITY = 1 / 0; |
| 25 | + |
| 26 | +function _toString(value: any): string { |
| 27 | + if (value == null) { |
| 28 | + return ""; |
| 29 | + } |
| 30 | + // Exit early for strings to avoid a performance hit in some environments. |
| 31 | + if (typeof value === "string") { |
| 32 | + return value; |
| 33 | + } |
| 34 | + if (Array.isArray(value)) { |
| 35 | + // Recursively convert values (susceptible to call stack limits). |
| 36 | + return `${ |
| 37 | + value.map((other) => (other == null ? other : _toString(other))) |
| 38 | + }`; |
| 39 | + } |
| 40 | + const result = `${value}`; |
| 41 | + return result === "0" && 1 / value === -INFINITY ? "-0" : result; |
18 | 42 | } |
0 commit comments