From d8baab2d0d4b1337b7acc09565cc20d7708185e3 Mon Sep 17 00:00:00 2001 From: HaoYang <49954218+Wing-9527@users.noreply.github.com> Date: Tue, 8 Nov 2022 13:58:20 +0800 Subject: [PATCH] feat(string): add `capitalize` and test case (#23) --- src/string.test.ts | 10 +++++++++- src/string.ts | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) 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() +}