feat: add upperCase function (#402)
Some checks are pending
CI / codecov (push) Waiting to run
Release / release (push) Waiting to run

* feat(upperCase): Add upperCase function

* Add upperCase benchmark

* Added english docs
This commit is contained in:
mattiacoll 2024-09-13 18:34:16 +12:00 committed by GitHub
parent c5d0254552
commit 7f2e19dc31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 119 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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

20
src/string/upperCase.ts Normal file
View File

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