Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/string.test.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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()
}