feat: Support pascalcase (#364)

This commit is contained in:
Joris Gallot 2024-08-10 03:18:30 +02:00 committed by GitHub
parent 4d46377d9b
commit 6d84ac35a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 58 additions and 0 deletions

View File

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

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