Skip to content

Commit

Permalink
feat: add upperCaseFirst
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Oct 28, 2018
1 parent 0e93f56 commit 5be204a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { default as Disposer } from './Disposer'
export { default as base64Decode } from './base64Decode'
export { default as base64Encode } from './base64Encode'
export { default as base64UrlDecode } from './base64UrlDecode'
Expand All @@ -7,6 +6,7 @@ export { default as bindEvent } from './bindEvent'
export { default as castArray } from './castArray'
export { default as clamp } from './clamp'
export { default as cssTransform } from './cssTransform'
export { default as Disposer } from './Disposer'
export { default as fill } from './fill'
export { default as forOwn } from './forOwn'
export { default as getType } from './getType'
Expand Down Expand Up @@ -41,4 +41,5 @@ export { default as repeat } from './repeat'
export { default as shuffle } from './shuffle'
export { default as stopEventPropagation } from './stopEventPropagation'
export { default as supportPassiveEventListener } from './supportPassiveEventListener'
export { default as upperCaseFirst } from './upperCaseFirst'
export { default as values } from './values'
17 changes: 17 additions & 0 deletions src/upperCaseFirst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const cache: { [key: string]: string } = Object.create(null)

/**
* 转换 str 的首字母为大写。
*
* @param str 要转换的字符串
* @returns 转换后的字符串
*/
export default function upperCaseFirst(str: string): string {
if (!str || typeof str !== 'string') {
return str
}
if (!(str in cache)) {
cache[str] = str.charAt(0).toUpperCase() + str.slice(1)
}
return cache[str]
}
17 changes: 17 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -725,3 +725,20 @@ describe('omit', () => {
expect(vtils.omit(obj, ['x', 'y', 'z'])).toEqual({})
})
})

describe('upperCaseFirst', () => {
test('非字符串原样返回', () => {
[null, undefined, 0, false, true, NaN, {}, [], /ff/, () => {}].forEach(value => {
expect(vtils.upperCaseFirst(value as any)).toBe(value)
})
})
test('字符串首字母大写后返回', () => {
expect(vtils.upperCaseFirst('ok')).toBe('Ok')
expect(vtils.upperCaseFirst('Ok')).toBe('Ok')
expect(vtils.upperCaseFirst('OK')).toBe('OK')
expect(vtils.upperCaseFirst('ok no')).toBe('Ok no')
expect(vtils.upperCaseFirst('1ok')).toBe('1ok')
expect(vtils.upperCaseFirst('_ok')).toBe('_ok')
expect(vtils.upperCaseFirst('')).toBe('')
})
})

0 comments on commit 5be204a

Please sign in to comment.