diff --git a/benchmarks/performance/upperCase.bench.ts b/benchmarks/performance/upperCase.bench.ts new file mode 100644 index 00000000..29ba1a2d --- /dev/null +++ b/benchmarks/performance/upperCase.bench.ts @@ -0,0 +1,28 @@ +import { bench, describe } from 'vitest'; +import { upperCase as upperCaseToolkit } from 'es-toolkit'; +import { upperCase as upperCaseLodash } from 'lodash'; + +describe('upperCase', () => { + describe('short string', () => { + bench('es-toolkit/upperCase', () => { + const str = 'camelCase'; + upperCaseToolkit(str); + }); + + bench('lodash/upperCase', () => { + const str = 'camelCase'; + upperCaseLodash(str); + }); + }); + + describe('long string', () => { + const LONG_STR = 'camelCaseLongString'.repeat(1000); + bench('es-toolkit/upperCase', () => { + upperCaseToolkit(LONG_STR); + }); + + bench('lodash/upperCase', () => { + upperCaseLodash(LONG_STR); + }); + }); +}); diff --git a/docs/reference/string/upperCase.md b/docs/reference/string/upperCase.md new file mode 100644 index 00000000..f840bcbd --- /dev/null +++ b/docs/reference/string/upperCase.md @@ -0,0 +1,30 @@ +# upperCase + +Converts a string to upper case. + +Upper case is the naming convention in which each word is written in uppercase and separated by an space ( ) character. For example, `UPPER CASE`. + +## Signature + +```typescript +function upperCase(str: string): string; +``` + +### Parameters + +- `str` (`string`): The string that is to be changed to upper case. + +### Returns + +(`string`) The converted string to upper case. + +## Examples + +```typescript +import { upperCase } from 'es-toolkit/string'; + +upperCase('camelCase'); // returns 'CAMEL CASE' +upperCase('some whitespace'); // returns 'SOME WHITESPACE' +upperCase('hyphen-text'); // returns 'HYPHEN TEXT' +upperCase('HTTPRequest'); // returns 'HTTP REQUEST' +``` diff --git a/src/string/index.ts b/src/string/index.ts index 982ccbfa..c2a8e190 100644 --- a/src/string/index.ts +++ b/src/string/index.ts @@ -1,6 +1,7 @@ export { camelCase } from './camelCase.ts'; export { snakeCase } from './snakeCase.ts'; export { kebabCase } from './kebabCase.ts'; +export { upperCase } from './upperCase.ts'; export { lowerCase } from './lowerCase.ts'; export { startCase } from './startCase.ts'; export { capitalize } from './capitalize.ts'; diff --git a/src/string/upperCase.spec.ts b/src/string/upperCase.spec.ts new file mode 100644 index 00000000..0234e85a --- /dev/null +++ b/src/string/upperCase.spec.ts @@ -0,0 +1,40 @@ +import { describe, it, expect } from 'vitest'; +import { upperCase } from './upperCase'; + +describe('upperCase', () => { + it('should change camel case to upper case', async () => { + expect(upperCase('camelCase')).toEqual('CAMEL CASE'); + }); + + it('should change space to space', async () => { + expect(upperCase('some whitespace')).toEqual('SOME WHITESPACE'); + }); + + it('should change hyphen to space', async () => { + expect(upperCase('hyphen-text')).toEqual('HYPHEN TEXT'); + }); + + it('should change Acronyms to small letter', async () => { + expect(upperCase('HTTPRequest')).toEqual('HTTP REQUEST'); + }); + + it('should handle leading and trailing whitespace', async () => { + expect(upperCase(' leading and trailing whitespace')).toEqual('LEADING AND TRAILING WHITESPACE'); + }); + + it('should handle special characters correctly', async () => { + expect(upperCase('special@characters!')).toEqual('SPECIAL CHARACTERS'); + }); + + it('should handle strings that are already in upper case', async () => { + expect(upperCase('upper_case')).toEqual('UPPER CASE'); + }); + + it('should work with an empty string', async () => { + expect(upperCase('')).toEqual(''); + }); + + it('should work with screaming snake case', async () => { + expect(upperCase('FOO_BAR')).toEqual('FOO BAR'); + }); +}); diff --git a/src/string/upperCase.ts b/src/string/upperCase.ts new file mode 100644 index 00000000..d7a1bea0 --- /dev/null +++ b/src/string/upperCase.ts @@ -0,0 +1,20 @@ +import { getWords } from './_internal/getWords.ts'; + +/** + * Converts a string to upper case. + * + * Upper case is the naming convention in which each word is written in uppercase and separated by an space ( ) character. + * + * @param {string} str - The string that is to be changed to upper case. + * @returns {string} - The converted string to upper case. + * + * @example + * const convertedStr1 = upperCase('camelCase') // returns 'CAMEL CASE' + * const convertedStr2 = upperCase('some whitespace') // returns 'SOME WHITESPACE' + * const convertedStr3 = upperCase('hyphen-text') // returns 'HYPHEN TEXT' + * const convertedStr4 = upperCase('HTTPRequest') // returns 'HTTP REQUEST' + */ +export const upperCase = (str: string): string => { + const words = getWords(str); + return words.map(word => word.toUpperCase()).join(' '); +};