string/capitalize.jsjavascript
/**
* 将字符串首字符转为大写,其余字符转为小写。
* @function capitalize
* @memberof module:string
* @param {string} value 原字符串
* @returns {string} 首字母大写的新字符串
* @throws {TypeError} 参数不是字符串时抛出
*/
function capitalize(value) {
if (typeof value !== 'string') throw new TypeError('value must be a string')
if (value.length === 0) return ''
const [first, ...rest] = Array.from(value.toLowerCase())
return first.toUpperCase() + rest.join('')
}
export default capitalize