diff --git a/benchmarks/lowerCase.bench.ts b/benchmarks/lowerCase.bench.ts new file mode 100644 index 00000000..d87c8501 --- /dev/null +++ b/benchmarks/lowerCase.bench.ts @@ -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); + }); +}); diff --git a/docs/.vitepress/en.mts b/docs/.vitepress/en.mts index cc58c0d9..7b7a5438 100644 --- a/docs/.vitepress/en.mts +++ b/docs/.vitepress/en.mts @@ -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' }, ], }, diff --git a/docs/.vitepress/ko.mts b/docs/.vitepress/ko.mts index dcb4e2e6..679eefa9 100644 --- a/docs/.vitepress/ko.mts +++ b/docs/.vitepress/ko.mts @@ -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' }, ], }, diff --git a/docs/ko/reference/string/lowerCase.md b/docs/ko/reference/string/lowerCase.md new file mode 100644 index 00000000..4e2fec42 --- /dev/null +++ b/docs/ko/reference/string/lowerCase.md @@ -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' +``` diff --git a/docs/reference/string/lowerCase.md b/docs/reference/string/lowerCase.md new file mode 100644 index 00000000..31b1f72d --- /dev/null +++ b/docs/reference/string/lowerCase.md @@ -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' +``` diff --git a/src/string/index.ts b/src/string/index.ts index 3e29206f..a3f8c9c4 100644 --- a/src/string/index.ts +++ b/src/string/index.ts @@ -1,3 +1,4 @@ export { snakeCase } from './snakeCase.ts'; export { kebabCase } from './kebabCase.ts'; +export { lowerCase } from './lowerCase.ts'; export { capitalize } from './capitalize.ts'; diff --git a/src/string/lowerCase.spec.ts b/src/string/lowerCase.spec.ts new file mode 100644 index 00000000..1f3ed364 --- /dev/null +++ b/src/string/lowerCase.spec.ts @@ -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'); + }); +}); diff --git a/src/string/lowerCase.ts b/src/string/lowerCase.ts new file mode 100644 index 00000000..20681481 --- /dev/null +++ b/src/string/lowerCase.ts @@ -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(' '); +};