Skip to content

Commit d8baab2

Browse files
authored
feat(string): add capitalize and test case (#23)
1 parent 5403529 commit d8baab2

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/string.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, it } from 'vitest'
2-
import { ensurePrefix, ensureSuffix, slash, template } from './string'
2+
import { capitalize, ensurePrefix, ensureSuffix, slash, template } from './string'
33

44
it('template', () => {
55
expect(
@@ -49,3 +49,11 @@ it('ensureSuffix', () => {
4949
expect(ensureSuffix('world', 'hello ')).toEqual('hello world')
5050
expect(ensureSuffix('123', 'abc123')).toEqual('abc123')
5151
})
52+
53+
it('capitalize', () => {
54+
expect(capitalize('hello World')).toEqual('Hello world')
55+
expect(capitalize('123')).toEqual('123')
56+
expect(capitalize('中国')).toEqual('中国')
57+
expect(capitalize('āÁĂÀ')).toEqual('Āáăà')
58+
expect(capitalize('\a')).toEqual('A')
59+
})

src/string.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,15 @@ export function randomStr(size = 16, dict = urlAlphabet) {
6666
id += dict[(Math.random() * len) | 0]
6767
return id
6868
}
69+
70+
/**
71+
* First letter uppercase, other lowercase
72+
* @category string
73+
* @example
74+
* ```
75+
* capitalize('hello') => 'Hello'
76+
* ```
77+
*/
78+
export function capitalize(str: string): string {
79+
return str[0].toUpperCase() + str.slice(1).toLowerCase()
80+
}

0 commit comments

Comments
 (0)