Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(capitalize): add capitalize #161

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions benchmarks/capitalize.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { bench, describe } from 'vitest';
import { capitalize as capitalizeToolkit } from 'es-toolkit';
import { capitalize as capitalizeLodash } from 'lodash';

describe('capitalize', () => {
bench('es-toolkit/capitalize', () => {
const str = 'camelCase';
capitalizeToolkit(str);
});

bench('lodash/capitalize', () => {
const str = 'camelCase';
capitalizeLodash(str);
});
});
5 changes: 4 additions & 1 deletion docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ function sidebar(): DefaultTheme.Sidebar {
},
{
text: 'String Utilities',
items: [{ text: 'snakeCase', link: '/reference/string/snakeCase' }],
items: [
{ text: 'snakeCase', link: '/reference/string/snakeCase' },
{ text: 'capitalize', link: '/reference/string/capitalize' },
],
},
],
},
Expand Down
5 changes: 4 additions & 1 deletion docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ function sidebar(): DefaultTheme.Sidebar {
},
{
text: '문자열',
items: [{ text: 'snakeCase', link: '/ko/reference/string/snakeCase' }],
items: [
{ text: 'snakeCase', link: '/ko/reference/string/snakeCase' },
{ text: 'capitalize', link: '/ko/reference/string/capitalize' },
],
},
],
},
Expand Down
26 changes: 26 additions & 0 deletions docs/ko/reference/string/capitalize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# capitalize

문자열의 첫 번째 문자를 대문자로 변환하고 나머지 문자는 소문자로 변환합니다.
raon0211 marked this conversation as resolved.
Show resolved Hide resolved

## 인터페이스

```typescript
function capitalize<T extends string>(str: T): Capitalize<T>;
```

### 파라미터

`str` (`T`): 대문자로 변환할 문자열.

### 반환 값

(`Capitalize<string>`): 대문자로 변환된 문자열.

## 예시

```typescript
import { capitalize } from 'es-toolkit/string';

capitalize('fred'); // returns 'Fred'
capitalize('FRED'); // returns 'Fred'
```
26 changes: 26 additions & 0 deletions docs/reference/string/capitalize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# capitalize

Converts the first character of string to upper case and the remaining to lower case.

## Signature

```typescript
function capitalize<T extends string>(str: T): Capitalize<T>;
```

### Parameters

`str` (`T`): The string to be converted to uppercase.

### Returns

(`Capitalize<string>`): The capitalized string.

## Examples

```typescript
import { capitalize } from 'es-toolkit/string';

capitalize('fred'); // returns 'Fred'
capitalize('FRED'); // returns 'Fred'
```
32 changes: 32 additions & 0 deletions src/string/capitalize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, it, expect } from 'vitest';
import { capitalize } from './capitalize';

describe('capitalize', () => {
it('should converts the first character of string to upper case', async () => {
expect(capitalize('fred')).toEqual('Fred');
});

it('should converts the first character of string to upper case and the remaining to lower case.', async () => {
expect(capitalize('FRED')).toEqual('Fred');
});

it('should handle special characters correctly', async () => {
expect(capitalize('special@characters!')).toEqual('Special@characters!');
});

it('should handle hyphen correctly', async () => {
expect(capitalize('hyphen-text')).toEqual('Hyphen-text');
});

it('should handle leading whitepspace', async () => {
expect(capitalize(' fred')).toEqual(' fred');
});

it('should handle strings that are already in capitalize', async () => {
expect(capitalize('Fred')).toEqual('Fred');
});

it('should work with an empty string', async () => {
expect(capitalize('')).toEqual('');
});
});
15 changes: 15 additions & 0 deletions src/string/capitalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Converts the first character of string to upper case and the remaining to lower case.
*
* @template T - Literal type of the string.
* @param {T} str - The string to be converted to uppercase.
* @returns {Capitalize<T>} - The capitalized string.
*
* @example
* const result = capitalize('fred') // returns 'Fred'
* const result2 = capitalize('FRED') // returns 'Fred'
*/

export const capitalize = <T extends string>(str: T): Capitalize<T> => {
return (str.charAt(0).toUpperCase() + str.slice(1).toLowerCase()) as Capitalize<T>;
};
1 change: 1 addition & 0 deletions src/string/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { snakeCase } from './snakeCase.ts';
export { capitalize } from './capitalize.ts';