Skip to content

Commit

Permalink
feat(string): add capitalize and test case (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wing-9527 committed Nov 8, 2022
1 parent 5403529 commit d8baab2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/string.test.ts
@@ -1,5 +1,5 @@
import { expect, it } from 'vitest'
import { ensurePrefix, ensureSuffix, slash, template } from './string'
import { capitalize, ensurePrefix, ensureSuffix, slash, template } from './string'

it('template', () => {
expect(
Expand Down Expand Up @@ -49,3 +49,11 @@ it('ensureSuffix', () => {
expect(ensureSuffix('world', 'hello ')).toEqual('hello world')
expect(ensureSuffix('123', 'abc123')).toEqual('abc123')
})

it('capitalize', () => {
expect(capitalize('hello World')).toEqual('Hello world')
expect(capitalize('123')).toEqual('123')
expect(capitalize('中国')).toEqual('中国')
expect(capitalize('āÁĂÀ')).toEqual('Āáăà')
expect(capitalize('\a')).toEqual('A')
})
12 changes: 12 additions & 0 deletions src/string.ts
Expand Up @@ -66,3 +66,15 @@ export function randomStr(size = 16, dict = urlAlphabet) {
id += dict[(Math.random() * len) | 0]
return id
}

/**
* First letter uppercase, other lowercase
* @category string
* @example
* ```
* capitalize('hello') => 'Hello'
* ```
*/
export function capitalize(str: string): string {
return str[0].toUpperCase() + str.slice(1).toLowerCase()
}

0 comments on commit d8baab2

Please sign in to comment.