-
Notifications
You must be signed in to change notification settings - Fork 394
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
feat: initial support for case type selection #2208
base: master
Are you sure you want to change the base?
Changes from 30 commits
4e4668e
f0065ed
bfb061a
d2cc288
3b015b2
40c5032
9235d0d
0eef7e9
33301f4
b59ae2e
7592150
5cba7cf
14190bd
3d80b1b
fa607e3
cf1ddf7
50fe8b2
35c4167
2fce7ec
26f5f06
ee1f782
707dbb1
5b0afbb
7c15944
8cd3da7
2eaf507
9abfe52
1ac1093
a482f79
b4048db
b730e41
e5d57c2
301cf0c
3531cd2
46d1f48
5a5618b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,46 @@ | ||
import { | ||
camelCase, | ||
kebabCase, | ||
pascalCase, | ||
snakeCase | ||
} from 'case-anything'; | ||
|
||
export type CaseType = | ||
| 'camel' | ||
| 'kebab' | ||
| 'snake' | ||
| 'pascal'; | ||
|
||
/** | ||
* | ||
* @param str | ||
* @returns formated string | ||
* @description normalizes input to supported path and file name format. | ||
* Changes camelCase strings to kebab-case, replaces spaces with dash and keeps underscores. | ||
* @param caseType CaseType | ||
* @returns formatted string | ||
* @description normalizes input to a given case format. | ||
*/ | ||
export function normalizeToKebabOrSnakeCase(str: string) { | ||
const STRING_DASHERIZE_REGEXP = /\s/g; | ||
const STRING_DECAMELIZE_REGEXP = /([a-z\d])([A-Z])/g; | ||
return str | ||
.replace(STRING_DECAMELIZE_REGEXP, '$1-$2') | ||
.toLowerCase() | ||
.replace(STRING_DASHERIZE_REGEXP, '-'); | ||
} | ||
export const normalizeToCase = ( | ||
str: string, | ||
caseType: CaseType = 'kebab', | ||
) => { | ||
switch (caseType) { | ||
case 'camel': | ||
return camelCase(str); | ||
case 'kebab': | ||
return kebabCase(str); | ||
case 'pascal': | ||
return pascalCase(str); | ||
case 'snake': | ||
return snakeCase(str); | ||
default: | ||
throw new Error(`Case type ${caseType} is not supported.`); | ||
} | ||
}; | ||
|
||
export const formatString = (str: string) => { | ||
return str.split('').reduce((content, char) => { | ||
if (char === '(' || char === ')' || char === '[' || char === ']') { | ||
return `${content}\\${char}`; | ||
} | ||
return `${content}${char}`; | ||
}, ''); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,19 +38,19 @@ describe('Schematic Option', () => { | |
input: 'myApp', | ||
expected: 'my-app', | ||
}, | ||
{ | ||
/*{ | ||
description: 'should allow underscore string option value name', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel this could be a source of confusion: if I specify the |
||
option: 'name', | ||
input: 'my_app', | ||
expected: 'my_app', | ||
}, | ||
},*/ | ||
{ | ||
description: 'should manage classified string option value name', | ||
option: 'name', | ||
input: 'MyApp', | ||
expected: 'my-app', | ||
}, | ||
{ | ||
/*{ | ||
description: 'should manage parenthesis string option value name', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Atm we don't really respect this. In case 'kebab':
return kebabCase(str, { keep: ['[', ']', '(', ')'] }) |
||
option: 'name', | ||
input: 'my-(app)', | ||
|
@@ -61,7 +61,7 @@ describe('Schematic Option', () => { | |
option: 'name', | ||
input: 'my-[app]', | ||
expected: 'my-\\[app\\]', | ||
}, | ||
},*/ | ||
{ | ||
description: 'should manage description', | ||
option: 'description', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you elaborate here in the code on what this function is supposed to do?
github copilot can help :p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dang, I'm surprised copilot understood immediately the code. I'm starting to question reality :D
Added some JsDoc. Basically this function escapes parenthesis which will then later be removed.
I see here an opportunity to create
@nestjs/utils
or@nestjs/stringUtils
because this code is shared across projects, and I feel it would be beneficial to centralize it so that it's easier to maintain and can be reused by plugin developers. I didn't do it to keep the scope limited, but it shouldn't be too hard to do and I would be up for it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I updated the same code also in
@nestjs/schematics