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(lowerCase): Add lowerCase function #166

Merged
merged 5 commits into from
Jul 12, 2024
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
15 changes: 15 additions & 0 deletions benchmarks/lowerCase.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { bench, describe } from 'vitest';
import { lowerCase as lowerCaseToolkit } from 'es-toolkit';
import { lowerCase as lowerCaseLodash } from 'lodash';

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

bench('lodash/lowerCase', () => {
const str = 'camelCase';
lowerCaseLodash(str);
});
});
1 change: 1 addition & 0 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ function sidebar(): DefaultTheme.Sidebar {
items: [
{ text: 'snakeCase', link: '/reference/string/snakeCase' },
{ text: 'kebabCase', link: '/reference/string/kebabCase' },
{ text: 'lowerCase', link: '/reference/string/lowerCase' },
{ text: 'capitalize', link: '/reference/string/capitalize' },
],
},
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ function sidebar(): DefaultTheme.Sidebar {
items: [
{ text: 'snakeCase', link: '/ko/reference/string/snakeCase' },
{ text: 'kebabCase', link: '/ko/reference/string/kebabCase' },
{ text: 'lowerCase', link: '/ko/reference/string/lowerCase' },
{ text: 'capitalize', link: '/ko/reference/string/capitalize' },
],
},
Expand Down
30 changes: 30 additions & 0 deletions docs/ko/reference/string/lowerCase.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# lowerCase

문자열을 소문자 표기법으로 변환해요.

소문자 표기법은 여러 단어로 구성된 식별자의 각 단어를 소문자로 쓰고, 단어를 공백( )으로 연결하는 명명 규칙입니다. 예를 들어 `lower case`처럼 써요.

## 인터페이스

```typescript
function lowerCase(str: string): string;
```

### 파라미터

- `str` (`string`): 소문자로 변환할 문자열이에요.

### 반환 값

(`string`) 소문자로 변환된 문자열이에요.

## 예시

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

lowerCase('camelCase'); // returns 'camel case'
lowerCase('some whitespace'); // returns 'some whitespace'
lowerCase('hyphen-text'); // returns 'hyphen text'
lowerCase('HTTPRequest'); // returns 'http request'
```
30 changes: 30 additions & 0 deletions docs/reference/string/lowerCase.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# lowerCase

Converts a string to lower case.

Lower case is the naming convention in which each word is written in lowercase and separated by an space ( ) character. For example, `lower case`.

## Signature

```typescript
function lowerCase(str: string): string;
```

### Parameters

- `str` (`string`): The string that is to be changed to lower case.

### Returns

(`string`) The converted string to lower case.

## Examples

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

lowerCase('camelCase'); // returns 'camel case'
lowerCase('some whitespace'); // returns 'some whitespace'
lowerCase('hyphen-text'); // returns 'hyphen text'
lowerCase('HTTPRequest'); // returns 'http request'
```
1 change: 1 addition & 0 deletions src/string/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { snakeCase } from './snakeCase.ts';
export { kebabCase } from './kebabCase.ts';
export { lowerCase } from './lowerCase.ts';
export { capitalize } from './capitalize.ts';
40 changes: 40 additions & 0 deletions src/string/lowerCase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, it, expect } from 'vitest';
import { lowerCase } from './lowerCase';

describe('lowerCase', () => {
it('should change camel case to lower case', async () => {
expect(lowerCase('camelCase')).toEqual('camel case');
});

it('should change space to space', async () => {
expect(lowerCase('some whitespace')).toEqual('some whitespace');
});

it('should change hyphen to space', async () => {
expect(lowerCase('hyphen-text')).toEqual('hyphen text');
});

it('should change Acronyms to small letter', async () => {
expect(lowerCase('HTTPRequest')).toEqual('http request');
});

it('should handle leading and trailing whitespace', async () => {
expect(lowerCase(' leading and trailing whitespace')).toEqual('leading and trailing whitespace');
});

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

it('should handle strings that are already in lower case', async () => {
expect(lowerCase('lower_case')).toEqual('lower case');
});

it('should work with an empty string', async () => {
expect(lowerCase('')).toEqual('');
});

it('should work with screaming snake case', async () => {
expect(lowerCase('FOO_BAR')).toEqual('foo bar');
});
});
21 changes: 21 additions & 0 deletions src/string/lowerCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { CASE_SPLIT_PATTERN } from '../constants';

/**
* Converts a string to lower case.
*
* Lower case is the naming convention in which each word is written in lowercase and separated by an space ( ) character.
*
* @param {string} str - The string that is to be changed to lower case.
* @returns {string} - The converted string to lower case.
*
* @example
* const convertedStr1 = lowerCase('camelCase') // returns 'camel case'
* const convertedStr2 = lowerCase('some whitespace') // returns 'some whitespace'
* const convertedStr3 = lowerCase('hyphen-text') // returns 'hyphen text'
* const convertedStr4 = lowerCase('HTTPRequest') // returns 'http request'
*/

export const lowerCase = (str: string): string => {
const splitWords = str.match(CASE_SPLIT_PATTERN) || [];
return splitWords.map(word => word.toLowerCase()).join(' ');
};