feat(lowerCase): Add lowerCase function (#166)

* feat(lowerCase): Add lowerCase function

* Update docs/ko/reference/string/lowerCase.md

* Update docs/ko/reference/string/lowerCase.md

* Update docs/reference/string/lowerCase.md

Co-authored-by: Evan Moon <bboydart91@gmail.com>

---------

Co-authored-by: Sojin Park <raon0211@gmail.com>
Co-authored-by: Evan Moon <bboydart91@gmail.com>
Co-authored-by: Sojin Park <raon0211@toss.im>
This commit is contained in:
Bert Verhelst 2024-07-12 03:17:34 +02:00 committed by GitHub
parent 541b89dc0d
commit b1cb93355a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 139 additions and 0 deletions

View File

@ -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);
});
});

View File

@ -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' },
],
},

View File

@ -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' },
],
},

View File

@ -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'
```

View File

@ -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'
```

View File

@ -1,3 +1,4 @@
export { snakeCase } from './snakeCase.ts';
export { kebabCase } from './kebabCase.ts';
export { lowerCase } from './lowerCase.ts';
export { capitalize } from './capitalize.ts';

View File

@ -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
src/string/lowerCase.ts Normal file
View File

@ -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(' ');
};