Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Underscores #1

Open
wants to merge 3 commits into
base: feature/batch-generate
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 29 additions & 13 deletions README.md
Expand Up @@ -212,27 +212,43 @@ Here is the string `Lives down BY the River` with each of the converters:
// If you typed in 'Lives down BY the River' for the a Replacer Slot named '__replacerSlot__' and
// used one of the optional Case Converters you would get the following:

__replacerSlot__(noCase); // Lives down BY the River
__replacerSlot__(camelCase); // livesDownByTheRiver
__replacerSlot__(constantCase); // LIVES_DOWN_BY_THE_RIVER
__replacerSlot__(dotCase); // lives.down.by.the.river
__replacerSlot__(kebabCase); // lives-down-by-the-river
__replacerSlot__(lowerCase); // livesdownbytheriver
__replacerSlot__(pascalCase); // LivesDownByTheRiver
__replacerSlot__(pathCase); // lives/down/by/the/river
__replacerSlot__(sentenceCase); // Lives down by the river
__replacerSlot__(snakeCase); // lives_down_by_the_river
__replacerSlot__(titleCase); // Lives Down By The River
'__replacerSlot__(noCase)'; // 'Lives down BY the River';
'__replacerSlot__(camelCase)'; // 'livesDownByTheRiver';
'__replacerSlot__(constantCase)'; // 'LIVES_DOWN_BY_THE_RIVER';
'__replacerSlot__(dotCase)'; // 'lives.down.by.the.river';
'__replacerSlot__(kebabCase)'; // 'lives-down-by-the-river';
'__replacerSlot__(lowerCase)'; // 'livesdownbytheriver';
'__replacerSlot__(pascalCase)'; // 'LivesDownByTheRiver';
'__replacerSlot__(pathCase)'; // 'lives/down/by/the/river';
'__replacerSlot__(sentenceCase)'; // 'Lives down by the river';
'__replacerSlot__(snakeCase)'; // 'lives_down_by_the_river';
'__replacerSlot__(titleCase)'; // 'Lives Down By The River';

// Note: you can set a 'defaultCase' converter in IConfigItem so all
// Replacer Slots without a Case Converter will be transformed the same way.
__replacerSlot__; // LivesDownByTheRiver
```

You may also specify the case using an underscores-only syntax:

```js
__replacerSlot__NoCase__; // Lives down BY the River
__replacerSlot__CamelCase__; // livesDownByTheRiver
__replacerSlot__ConstantCase__; // LIVES_DOWN_BY_THE_RIVER
__replacerSlot__DotCase__; // lives.down.by.the.river
__replacerSlot__KebabCase__; // lives-down-by-the-river
__replacerSlot__LowerCase__; // livesdownbytheriver
__replacerSlot__PascalCase__; // LivesDownByTheRiver
__replacerSlot__PathCase__; // lives/down/by/the/river
__replacerSlot__SentenceCase__; // Lives down by the river
__replacerSlot__SnakeCase__; // lives_down_by_the_river
__replacerSlot__TitleCase__; // Lives Down By The River
```

One Rule: no spaces between the [Replacer Slots](#replacer-slots-or-ireplacerslotquestion) and [Case Converters](#case-converters). If there is a space, [Case Converters](#case-converters) will not work.

- :white_check_mark: `__name__(camelCase)`
- :warning: `__name__ (camelCase)`
- :white_check_mark: `__name__(camelCase)` OR `__name__CamelCase__`
- :x: `__name__ (camelCase)` OR `__name__ CamelCase__`

## Batch Usage

Expand Down
29 changes: 7 additions & 22 deletions examples/tools/batchGenerate.js
Expand Up @@ -3,38 +3,23 @@ const {generateTemplateFilesBatch, CaseConverterEnum} = require('../../dist/gene
// Note: In your file it will be like this:
// const {generateTemplateFilesBatch, CaseConverterEnum} = require('generate-template-files');

const componentName = "Example"
const componentScope = "common"
const componentName = "UserProfile"
const componentScope = "example"

generateTemplateFilesBatch([
{
option: 'Component',
option: 'Testing out batch generate of file with mix of underscores and brackets',
defaultCase: CaseConverterEnum.PascalCase,
entry: {
folderPath: './tools/templates/react/component',
folderPath: './tools/templates/misc',
},
dynamicReplacers: [
{ slot: '__name__', slotValue: componentName },
{ slot: '__resourceName__', slotValue: componentName },
{ slot: '__scope__', slotValue: componentScope },
],
output: {
path: `./src/component/__scope__(camelCase)`,
pathAndFileNameDefaultCase: CaseConverterEnum.PascalCase,
},
},
{
option: 'Component Interface',
defaultCase: CaseConverterEnum.PascalCase,
entry: {
folderPath: './tools/templates/react/I__interface__.ts',
},
dynamicReplacers: [
{ slot: '__interface__', slotValue: componentName },
{ slot: '__scope__', slotValue: componentScope },
],
output: {
path: `./src/component/__scope__(camelCase)/I__interface__.ts`,
pathAndFileNameDefaultCase: CaseConverterEnum.PascalCase,
path: `./src/__scope__/`,
pathAndFileNameDefaultCase: CaseConverterEnum.CamelCase,
},
},
]);
Expand Down
28 changes: 28 additions & 0 deletions examples/tools/templates/misc/__resourceName__PascalCase__.ts
@@ -0,0 +1,28 @@
// src/__scope__CamelCase__/__resourceName__(pascalCase).ts
import { __resourceName__SnakeCase___data } from "./__resourceName__PascalCase__Data"

console.assert(__resourceName__SnakeCase___data.key === "__resourceName__(snakeCase)");

export const __resourceName__ConstantCase___KEY = __resourceName__SnakeCase___data.key;
type __resourceName__PascalCase__Data = typeof __resourceName__SnakeCase___data

/**
*
*/
const fetchData = (_key: "__resourceName__(snakeCase)"): __resourceName__PascalCase__Data => {
// Implement fetch logic here
const result: __resourceName__PascalCase__Data["result"] = {}

return {
key: __resourceName__ConstantCase___KEY,
result,
}
};

/**
* The __resourceName__(titleCase) fetcher
*/
export const get__resourceName__ = async (): Promise<__resourceName__PascalCase__Data> => {
const __resourceName__CamelCase__ = fetchData(__resourceName__SnakeCase___data.key);
return __resourceName__CamelCase__;
};
@@ -0,0 +1,7 @@
// src/__scope__CamelCase__/__resourceName__(pascalCase)Data.ts
export const __resourceName__SnakeCase___data = {
key: "__resourceName__(snakeCase)",
result: {
// Add values here
}
} as const
33 changes: 22 additions & 11 deletions src/constants/CaseConverterEnum.ts
Expand Up @@ -5,7 +5,7 @@ enum CaseConverterEnum {
*
* ```
* // If you entered "Lives down BY the River" for __replacerSlot__
* __replacerSlot__(noCase)
* __replacerSlot__(noCase) OR __replacerSlot__NoCase__
*
* // It would output to:
* Lives down BY the River
Expand All @@ -18,12 +18,13 @@ enum CaseConverterEnum {
* ```
*/
None = '(noCase)',
NoneUnderscore = 'NoCase__',
/**
* [Case Converter](../index.html#case-converters) that converts [Replacer Slots](../index.html#replacer-slots) to camel case.
*
* ```
* // If you entered "Lives down BY the River" for __replacerSlot__
* __replacerSlot__(camelCase)
* __replacerSlot__(camelCase) OR __replacerSlot__CamelCase__
*
* // It would output to:
* livesDownByTheRiver
Expand All @@ -36,12 +37,13 @@ enum CaseConverterEnum {
* ```
*/
CamelCase = '(camelCase)',
CamelCaseUnderscore = 'CamelCase__',
/**
* [Case Converter](../index.html#case-converters) that converts [Replacer Slots](../index.html#replacer-slots) to constant case.
*
* ```
* // If you entered "Lives down BY the River" for __replacerSlot__
* __replacerSlot__(constantCase)
* __replacerSlot__(constantCase) OR __replacerSlot__ConstantCase__
*
* // It would output to:
* LIVES_DOWN_BY_THE_RIVER
Expand All @@ -54,12 +56,13 @@ enum CaseConverterEnum {
* ```
*/
ConstantCase = '(constantCase)',
ConstantCaseUnderscore = 'ConstantCase__',
/**
* [Case Converter](../index.html#case-converters) that converts [Replacer Slots](../index.html#replacer-slots) to dot case.
*
* ```
* // If you entered "Lives down BY the River" for __replacerSlot__
* __replacerSlot__(dotCase)
* __replacerSlot__(dotCase) OR __replacerSlot__DotCase__
*
* // It would output to:
* lives.down.by.the.river
Expand All @@ -72,12 +75,13 @@ enum CaseConverterEnum {
* ```
*/
DotCase = '(dotCase)',
DotCaseUnderscore = 'DotCase__',
/**
* [Case Converter](../index.html#case-converters) that converts [Replacer Slots](../index.html#replacer-slots) to kebab case.
*
* ```
* // If you entered "Lives down BY the River" for __replacerSlot__
* __replacerSlot__(kebabCase)
* __replacerSlot__(kebabCase) OR __replacerSlot__KebabCase__
*
* // It would output to:
* lives-down-by-the-river
Expand All @@ -90,12 +94,13 @@ enum CaseConverterEnum {
* ```
*/
KebabCase = '(kebabCase)',
KebabCaseUnderscore = 'KebabCase__',
/**
* [Case Converter](../index.html#case-converters) that converts [Replacer Slots](../index.html#replacer-slots) to all lower case.
*
* ```
* // If you entered "Lives down BY the River" for __replacerSlot__
* __replacerSlot__(lowerCase)
* __replacerSlot__(lowerCase) OR __replacerSlot__LowerCase__
*
* // It would output to:
* livesdownbytheriver
Expand All @@ -108,12 +113,13 @@ enum CaseConverterEnum {
* ```
*/
LowerCase = '(lowerCase)',
LowerCaseUnderscore = 'LowerCase__',
/**
* [Case Converter](../index.html#case-converters) that converts [Replacer Slots](../index.html#replacer-slots) to pacal case.
*
* ```
* // If you entered "Lives down BY the River" for __replacerSlot__
* __replacerSlot__(pascalCase)
* __replacerSlot__(pascalCase) OR __replacerSlot__PascalCase__
*
* // It would output to:
* LivesDownByTheRiver
Expand All @@ -126,12 +132,13 @@ enum CaseConverterEnum {
* ```
*/
PascalCase = '(pascalCase)',
PascalCaseUnderscore = 'PascalCase__',
/**
* [Case Converter](../index.html#case-converters) that converts [Replacer Slots](../index.html#replacer-slots) to path case.
*
* ```
* // If you entered "Lives down BY the River" for __replacerSlot__
* __replacerSlot__(pathCase)
* __replacerSlot__(pathCase) OR __replacerSlot__PathCase__
*
* // It would output to:
* lives/down/by/the/river
Expand All @@ -144,12 +151,13 @@ enum CaseConverterEnum {
* ```
*/
PathCase = '(pathCase)',
PathCaseUnderscore = 'PathCase__',
/**
* [Case Converter](../index.html#case-converters) that converts [Replacer Slots](../index.html#replacer-slots) to sentence case.
*
* ```
* // If you entered "Lives down BY the River" for __replacerSlot__
* __replacerSlot__(sentenceCase)
* __replacerSlot__(sentenceCase) OR __replacerSlot__SentenceCase__
*
* // It would output to:
* Lives down by the river
Expand All @@ -162,12 +170,13 @@ enum CaseConverterEnum {
* ```
*/
SentenceCase = '(sentenceCase)',
SentenceCaseUnderscore = 'SentenceCase__',
/**
* [Case Converter](../index.html#case-converters) that converts [Replacer Slots](../index.html#replacer-slots) to snake case.
*
* ```
* // If you entered "Lives down BY the River" for __replacerSlot__
* __replacerSlot__(snakeCase)
* __replacerSlot__(snakeCase) OR __replacerSlot__SnakeCase__
*
* // It would output to:
* lives_down_by_the_river
Expand All @@ -180,12 +189,13 @@ enum CaseConverterEnum {
* ```
*/
SnakeCase = '(snakeCase)',
SnakeCaseUnderscore = 'SnakeCase__',
/**
* [Case Converter](../index.html#case-converters) that converts [Replacer Slots](../index.html#replacer-slots) to title case.
*
* ```
* // If you entered "Lives down BY the River" for __replacerSlot__
* __replacerSlot__(titleCase)
* __replacerSlot__(titleCase) OR __replacerSlot__TitleCase__
*
* // It would output to:
* Lives Down By The River
Expand All @@ -198,6 +208,7 @@ enum CaseConverterEnum {
* ```
*/
TitleCase = '(titleCase)',
TitleCaseUnderscore = 'TitleCase__',
}

export default CaseConverterEnum;
11 changes: 11 additions & 0 deletions src/utilities/StringUtility.ts
Expand Up @@ -15,26 +15,37 @@ export default class StringUtility {
public static toCase(str: string, caseType: CaseConverterEnum): string {
switch (caseType) {
case CaseConverterEnum.CamelCase:
case CaseConverterEnum.CamelCaseUnderscore:
return StringUtility.toCamelCase(str);
case CaseConverterEnum.ConstantCase:
case CaseConverterEnum.ConstantCaseUnderscore:
return StringUtility.toConstantCase(str);
case CaseConverterEnum.DotCase:
case CaseConverterEnum.DotCaseUnderscore:
return StringUtility.toSentence(str, '.');
case CaseConverterEnum.KebabCase:
case CaseConverterEnum.KebabCaseUnderscore:
return StringUtility.toSentence(str, '-');
case CaseConverterEnum.LowerCase:
case CaseConverterEnum.LowerCaseUnderscore:
return StringUtility.toSentence(str, '');
case CaseConverterEnum.PascalCase:
case CaseConverterEnum.PascalCaseUnderscore:
return StringUtility.toPascalCase(str);
case CaseConverterEnum.PathCase:
case CaseConverterEnum.PathCaseUnderscore:
return StringUtility.toSentence(str, '/');
case CaseConverterEnum.SentenceCase:
case CaseConverterEnum.SentenceCaseUnderscore:
return StringUtility.toSentenceCase(str);
case CaseConverterEnum.SnakeCase:
case CaseConverterEnum.SnakeCaseUnderscore:
return StringUtility.toSentence(str, '_');
case CaseConverterEnum.TitleCase:
case CaseConverterEnum.TitleCaseUnderscore:
return StringUtility.toTitleCase(str);
case CaseConverterEnum.None:
case CaseConverterEnum.NoneUnderscore:
default:
return str;
}
Expand Down