Skip to content

Commit 5cad89b

Browse files
committed
fix: undefined string when using ucfirst
1 parent 240ed49 commit 5cad89b

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,19 @@ module.exports.template = function (template, data) {
9494
/**
9595
* Make first char uppercase.
9696
* @param {string} str
97-
* @returns {string}
97+
* @returns {string | undefined}
9898
*/
9999
module.exports.ucfirst = function (str) {
100-
return str.charAt(0).toUpperCase() + str.substr(1)
100+
if (str) return str.charAt(0).toUpperCase() + str.substr(1)
101101
}
102102

103103
/**
104104
* Make first char lowercase.
105105
* @param {string} str
106-
* @returns {string}
106+
* @returns {string | undefined}
107107
*/
108108
module.exports.lcfirst = function (str) {
109-
return str.charAt(0).toLowerCase() + str.substr(1)
109+
if (str) return str.charAt(0).toLowerCase() + str.substr(1)
110110
}
111111

112112
module.exports.chunkArray = function (arr, chunk) {

test/unit/utils_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ describe('utils', () => {
3838
it('should capitalize first letter', () => {
3939
expect(utils.ucfirst('hello')).equal('Hello')
4040
})
41+
42+
it('should handle the undefined', () => {
43+
expect(utils.ucfirst()).to.be.undefined
44+
})
4145
})
4246

4347
describe('#beautify', () => {

0 commit comments

Comments
 (0)