mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-24 03:32:58 +03:00
feat: add upperCase
function (#402)
* feat(upperCase): Add upperCase function * Add upperCase benchmark * Added english docs
This commit is contained in:
parent
c5d0254552
commit
7f2e19dc31
28
benchmarks/performance/upperCase.bench.ts
Normal file
28
benchmarks/performance/upperCase.bench.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
30
docs/reference/string/upperCase.md
Normal file
30
docs/reference/string/upperCase.md
Normal 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'
|
||||
```
|
@ -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';
|
||||
|
40
src/string/upperCase.spec.ts
Normal file
40
src/string/upperCase.spec.ts
Normal 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
20
src/string/upperCase.ts
Normal 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(' ');
|
||||
};
|
Loading…
Reference in New Issue
Block a user