mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-24 03:32:58 +03:00
feat: Support pascalcase (#364)
This commit is contained in:
parent
4d46377d9b
commit
6d84ac35a4
@ -4,3 +4,4 @@ export { kebabCase } from './kebabCase.ts';
|
||||
export { lowerCase } from './lowerCase.ts';
|
||||
export { startCase } from './startCase.ts';
|
||||
export { capitalize } from './capitalize.ts';
|
||||
export { pascalCase } from './pascalCase.ts';
|
||||
|
36
src/string/pascalCase.spec.ts
Normal file
36
src/string/pascalCase.spec.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { pascalCase } from './pascalCase';
|
||||
|
||||
describe('PascalCase', () => {
|
||||
it('should change space to pascal case', () => {
|
||||
expect(pascalCase('some whitespace')).toEqual('SomeWhitespace');
|
||||
});
|
||||
|
||||
it('should change hyphen to pascal case', () => {
|
||||
expect(pascalCase('hyphen-text')).toEqual('HyphenText');
|
||||
});
|
||||
|
||||
it('should change Acronyms to small letter', () => {
|
||||
expect(pascalCase('HTTPRequest')).toEqual('HttpRequest');
|
||||
});
|
||||
|
||||
it('should handle leading and trailing whitespace', () => {
|
||||
expect(pascalCase(' leading and trailing whitespace')).toEqual('LeadingAndTrailingWhitespace');
|
||||
});
|
||||
|
||||
it('should handle special characters correctly', () => {
|
||||
expect(pascalCase('special@characters!')).toEqual('SpecialCharacters');
|
||||
});
|
||||
|
||||
it('should handle strings that are already in PascalCase', () => {
|
||||
expect(pascalCase('PascalCase')).toEqual('PascalCase');
|
||||
});
|
||||
|
||||
it('should work with an empty string', () => {
|
||||
expect(pascalCase('')).toEqual('');
|
||||
});
|
||||
|
||||
it('should work with screaming camel case', () => {
|
||||
expect(pascalCase('FOO_BAR')).toEqual('FooBar');
|
||||
});
|
||||
});
|
21
src/string/pascalCase.ts
Normal file
21
src/string/pascalCase.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { getWords } from './_internal/getWords';
|
||||
import { capitalize } from './capitalize';
|
||||
|
||||
/**
|
||||
* Converts a string to Pascal case.
|
||||
*
|
||||
* Pascal case is the naming convention in which each word is capitalized and concatenated without any separator characters.
|
||||
*
|
||||
* @param {string} str - The string that is to be changed to pascal case.
|
||||
* @returns {string} - The converted string to Pascal case.
|
||||
*
|
||||
* @example
|
||||
* const convertedStr1 = pascalCase('pascalCase') // returns 'PascalCase'
|
||||
* const convertedStr2 = pascalCase('some whitespace') // returns 'SomeWhitespace'
|
||||
* const convertedStr3 = pascalCase('hyphen-text') // returns 'HyphenText'
|
||||
* const convertedStr4 = pascalCase('HTTPRequest') // returns 'HttpRequest'
|
||||
*/
|
||||
export const pascalCase = (str: string): string => {
|
||||
const words = getWords(str);
|
||||
return words.map(word => capitalize(word)).join('');
|
||||
};
|
Loading…
Reference in New Issue
Block a user