diff --git a/src/string.test.ts b/src/string.test.ts index 6182f02..6b5cc64 100644 --- a/src/string.test.ts +++ b/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( @@ -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') +}) diff --git a/src/string.ts b/src/string.ts index a74728a..971a513 100644 --- a/src/string.ts +++ b/src/string.ts @@ -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() +}