Skip to content

Commit

Permalink
feat: add support pascalCase
Browse files Browse the repository at this point in the history
  • Loading branch information
Bajdzis committed Sep 20, 2019
1 parent 0804201 commit f543a45
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/textCase/domain.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type TextCase = 'camelCase' | 'snakeCase' | 'lowerCase' | 'kebabCase' | 'capitalize' | 'upperKebabCase' | 'upperSnakeCase'| 'other';
export type TextCase = 'camelCase' | 'snakeCase' | 'lowerCase' | 'kebabCase' | 'pascalCase' | 'upperKebabCase' | 'upperSnakeCase' | 'other';
6 changes: 3 additions & 3 deletions src/textCase/getFormatedText.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {camelCase, snakeCase, lowerCase , kebabCase, capitalize} from 'lodash';
import {camelCase, snakeCase, lowerCase , kebabCase, upperFirst} from 'lodash';
import { TextCase } from './domain';

export function getFormatedText(value: string, format: TextCase) {
if('capitalize' === format) {
return capitalize(value);
if('pascalCase' === format) {
return upperFirst(camelCase(value));
} else if ('lowerCase' === format) {
return lowerCase(value);
} else if ('camelCase' === format) {
Expand Down
22 changes: 19 additions & 3 deletions src/textCase/getTextCase.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { camelCase, snakeCase, lowerCase , kebabCase, capitalize } from 'lodash';
import { camelCase, snakeCase, lowerCase , kebabCase, upperFirst } from 'lodash';
import { TextCase } from './domain';

export function getTextCase(value: string): TextCase {
if(capitalize(value) === value){
return 'capitalize';
if(upperFirst(camelCase(value)) === value){
return 'pascalCase';
} else if (lowerCase(value) === value) {
return 'lowerCase';
} else if (camelCase(value) === value) {
Expand All @@ -20,3 +20,19 @@ export function getTextCase(value: string): TextCase {

return 'other';
}

export function getTextCaseSingleWord(singleWord: string): TextCase {
if (singleWord.match(/^[a-z]*$/i) === null) {
return 'other';
}

if (upperFirst(lowerCase(singleWord)) === singleWord) {
return 'pascalCase';
} else if (lowerCase(singleWord) === singleWord) {
return 'lowerCase';
} else if (singleWord.toUpperCase() === singleWord) {
return 'upperKebabCase';
}

return 'other';
}
29 changes: 22 additions & 7 deletions src/textCase/textCase.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getTextCase } from './getTextCase';
import { getTextCase, getTextCaseSingleWord } from './getTextCase';
import { getFormatedText } from './getFormatedText';

describe('textCase / getTextCase',() => {
describe('textCase',() => {

it('should return correct text case', () => {
expect(getTextCase('Some text')).toEqual('capitalize');
it('getTextCase should return correct text case', () => {
expect(getTextCase('SomeText')).toEqual('pascalCase');
expect(getTextCase('some text')).toEqual('lowerCase');
expect(getTextCase('someText')).toEqual('camelCase');
expect(getTextCase('some-text')).toEqual('kebabCase');
Expand All @@ -13,10 +13,25 @@ describe('textCase / getTextCase',() => {
expect(getTextCase('SOME_TEXT')).toEqual('upperSnakeCase');
});

it('should return text with correct format', () => {
const someText = 'some text';
it('getTextCaseSingleWord should return correct text case', () => {
expect(getTextCaseSingleWord('Word')).toEqual('pascalCase');
expect(getTextCaseSingleWord('word')).toEqual('lowerCase');
expect(getTextCaseSingleWord('WORD')).toEqual('upperKebabCase');
expect(getTextCaseSingleWord('WoRd')).toEqual('other');

expect(getFormatedText(someText, 'capitalize')).toEqual('Some text');
expect(getTextCaseSingleWord('SomeText')).toEqual('other');
expect(getTextCaseSingleWord('some text')).toEqual('other');
expect(getTextCaseSingleWord('someText')).toEqual('other');
expect(getTextCaseSingleWord('some-text')).toEqual('other');
expect(getTextCaseSingleWord('SOME-TEXT')).toEqual('other');
expect(getTextCaseSingleWord('some_text')).toEqual('other');
expect(getTextCaseSingleWord('SOME_TEXT')).toEqual('other');
});

it('getFormatedText should return text with correct format', () => {
const someText = 'Some text';

expect(getFormatedText(someText, 'pascalCase')).toEqual('SomeText');
expect(getFormatedText(someText, 'lowerCase')).toEqual('some text');
expect(getFormatedText(someText, 'camelCase')).toEqual('someText');
expect(getFormatedText(someText, 'kebabCase')).toEqual('some-text');
Expand Down
4 changes: 2 additions & 2 deletions src/variableTemplate/createVariableTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PathInfo } from '../fileInfo/getInfoAboutPath';
import { addSlashes } from '../helpers/addSlashes';
import { AwesomeTreeError } from '../errors/AwesomeTreeError';
import { getTextCase } from '../textCase/getTextCase';
import { getTextCaseSingleWord } from '../textCase/getTextCase';


export function createVariableTemplate(search:string, information: PathInfo[], maxIterate: number = 500) {
Expand All @@ -28,7 +28,7 @@ export function createVariableTemplate(search:string, information: PathInfo[], m

while ((regExpResult = regExp.exec(result)) !== null) {
const word = regExpResult[0];
const textCase = getTextCase(word);
const textCase = getTextCaseSingleWord(word);
const allCurrentWord = new RegExp(`(?<=^([^\\$\{]|\\$\\{[^"]*\\})*)(?<varName>${word})`, 'g');
if (textCase === 'other') {
result = result.replace( allCurrentWord, `\${:${word}:}` );
Expand Down
8 changes: 4 additions & 4 deletions src/variableTemplate/variableTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createVariableTemplate } from './createVariableTemplate';
import { PathInfo } from '../fileInfo/getInfoAboutPath';
import { renderVariableTemplate } from './renderVariableTemplate';

const mockPathInfo: PathInfo[] =[
const mockPathInfo: PathInfo[] = [
{
extension: '',
isFile: false,
Expand Down Expand Up @@ -30,7 +30,7 @@ describe('fileInfo / createVariableTemplate',() => {
it('should return template string', () => {
const template = createVariableTemplate('/tests/uri/mockedUri.test.ts', mockPathInfo);

expect(decodeURIComponent(template)).toEqual('/${lowerCase(variable[0][2][0])}/${lowerCase(variable[0][1][0])}/mocked${capitalize(variable[0][1][0])}.test.ts');
expect(decodeURIComponent(template)).toEqual('/${lowerCase(variable[0][2][0])}/${lowerCase(variable[0][1][0])}/mocked${pascalCase(variable[0][1][0])}.test.ts');
});

it('should works with wired text case', () => {
Expand All @@ -54,7 +54,7 @@ describe('fileInfo / createVariableTemplate',() => {
path: '/allClasses/uriComponent/tests/',
pathParts: [
{
parts:[ '1', '0', 'variable', 'capitalize', 'uri', 'mocked', 'tests', '1', '0', 'variable', 'capitalize'],
parts:[ '1', '0', 'variable', 'pascalCase', 'uri', 'mocked', 'tests', '1', '0', 'variable', 'pascalCase'],
textCase:'camelCase',
value: 'allClasses'
}
Expand All @@ -63,7 +63,7 @@ describe('fileInfo / createVariableTemplate',() => {
];

const template = createVariableTemplate('/tests/uri/mockedUri.test.ts', specialCharacters);
expect(decodeURIComponent(template)).toEqual('/${lowerCase(variable[0][0][6])}/${lowerCase(variable[0][0][4])}/${lowerCase(variable[0][0][5])}${capitalize(variable[0][0][4])}.test.ts');
expect(decodeURIComponent(template)).toEqual('/${lowerCase(variable[0][0][6])}/${lowerCase(variable[0][0][4])}/${lowerCase(variable[0][0][5])}${pascalCase(variable[0][0][4])}.test.ts');
});

});

0 comments on commit f543a45

Please sign in to comment.