Files
lodash/.internal/createCaseFirst.js
Luiz Américo e51a424513 Fix string methods to handle empty values (#4442)
* Enable strings category methods tests

* Ensure escape, pad, padEnd, padStart, trim, trimEnd, trimStart, unescape return an empty string for falsey values

* Coerce value to string using toString in truncate, capitalize and case methods

* Ensure createCaseFirst returns an empty string for falsey values
2019-08-26 06:13:55 -07:00

35 lines
761 B
JavaScript

import castSlice from './castSlice.js'
import hasUnicode from './hasUnicode.js'
import stringToArray from './stringToArray.js'
/**
* Creates a function like `lowerFirst`.
*
* @private
* @param {string} methodName The name of the `String` case method to use.
* @returns {Function} Returns the new case function.
*/
function createCaseFirst(methodName) {
return (string) => {
if (!string) {
return ''
}
const strSymbols = hasUnicode(string)
? stringToArray(string)
: undefined
const chr = strSymbols
? strSymbols[0]
: string[0]
const trailing = strSymbols
? castSlice(strSymbols, 1).join('')
: string.slice(1)
return chr[methodName]() + trailing
}
}
export default createCaseFirst