-
Notifications
You must be signed in to change notification settings - Fork 0
feat: name casing #2
base: master
Are you sure you want to change the base?
Changes from all commits
3609e4d
4e2c8bf
423a3dc
45dc6ad
a97b619
4b3b32c
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 |
---|---|---|
|
@@ -48,6 +48,10 @@ export class GenerateCommand extends AbstractCommand { | |
'-c, --collection [collectionName]', | ||
'Schematics collection to use.', | ||
) | ||
.option( | ||
'--case [case]', | ||
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.
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. --caseNaming |
||
'Case for variable naming. Options are \'kebab\' | \'snake\' | \'camel\' | \'pascal\' | \'upper\'.', | ||
) | ||
.action( | ||
async ( | ||
schematic: string, | ||
|
@@ -93,6 +97,11 @@ export class GenerateCommand extends AbstractCommand { | |
value: command.skipImport, | ||
}); | ||
|
||
options.push({ | ||
name: 'case', | ||
value: command.case, | ||
}); | ||
|
||
const inputs: Input[] = []; | ||
inputs.push({ name: 'schematic', value: schematic }); | ||
inputs.push({ name: 'name', value: name }); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { normalizeToKebabOrSnakeCase } from '../utils/formatting'; | ||
import { normalizeToCase, formatString, CaseType } from '../utils/formatting'; | ||
|
||
export class SchematicOption { | ||
constructor(private name: string, private value: boolean | string) {} | ||
constructor(private name: string, private value: boolean | string, private caseType: CaseType) {} | ||
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 decided to pass |
||
|
||
get normalizedName() { | ||
return normalizeToKebabOrSnakeCase(this.name); | ||
return normalizeToCase(this.name, 'kebab'); | ||
} | ||
|
||
public toCommandString(): string { | ||
|
@@ -25,13 +25,14 @@ export class SchematicOption { | |
} | ||
|
||
private format() { | ||
return normalizeToKebabOrSnakeCase(this.value as string) | ||
.split('') | ||
.reduce((content, char) => { | ||
if (char === '(' || char === ')' || char === '[' || char === ']') { | ||
return `${content}\\${char}`; | ||
} | ||
return `${content}${char}`; | ||
}, ''); | ||
|
||
return formatString( | ||
normalizeToCase( | ||
this.value as string, | ||
this.caseType | ||
) | ||
); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,48 @@ | ||
import { CaseToCase } from 'case-to-case' | ||
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'm not sure this is the best package to do this |
||
|
||
const formatter = new CaseToCase() | ||
|
||
export type CaseType = 'kebab' | 'snake' | 'camel' | 'pascal' | 'upper'; | ||
|
||
/** | ||
* | ||
* @param str | ||
* @param caseType | ||
* @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. | ||
*/ | ||
export const normalizeToCase = (str: string, caseType: CaseType = 'kebab') => { | ||
switch (caseType) { | ||
case 'kebab': | ||
return normalizeToKebabOrSnakeCase(str); | ||
case 'snake': | ||
return normalizeToKebabOrSnakeCase(str); | ||
case 'camel': | ||
return formatter.toCamelCase(str); | ||
case 'pascal': | ||
return formatter.toPascalCase(str); | ||
case 'upper': | ||
return formatter.toUpperCase(str); | ||
default: | ||
console.log(`Error! case type ${caseType} is not supported.`) | ||
return str; | ||
|
||
} | ||
} | ||
|
||
export const formatString = (str: string) => { | ||
return str.split('') | ||
.reduce((content, char) => { | ||
if (char === '(' || char === ')' || char === '[' || char === ']') { | ||
return `${content}\\${char}`; | ||
} | ||
return `${content}${char}`; | ||
}, '') | ||
} | ||
|
||
|
||
|
||
/** | ||
* | ||
* @param str | ||
|
@@ -13,3 +58,5 @@ export function normalizeToKebabOrSnakeCase(str: string) { | |
.toLowerCase() | ||
.replace(STRING_DASHERIZE_REGEXP, '-'); | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { normalizeToCase, formatString, CaseType } from '../../../lib/utils/formatting'; | ||
import {SchematicOption} from "../../../lib/schematics"; | ||
|
||
type TestSuite = { | ||
description: string; | ||
input: string; | ||
caseType: CaseType; | ||
expected: string; | ||
}; | ||
|
||
describe('Format strings', () => { | ||
|
||
const tests: TestSuite[] = [ | ||
{ | ||
description: 'From kebap to camel', | ||
input: 'my-app', | ||
caseType: 'camel', | ||
expected: 'myApp', | ||
}, | ||
]; | ||
|
||
tests.forEach((test) => { | ||
it(test.description, () => { | ||
|
||
expect( | ||
normalizeToCase(test.input, test.caseType) | ||
).toEqual( | ||
test.expected | ||
); | ||
}); | ||
}); | ||
|
||
}); |
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.
I would introduce a key-value options here since it has 3 arguments.
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.
You mean adding something like
SchmaticOption('naming', 'camelCase')
?That's the first thing I tried but I couldn't make it work because when we launch the command
nest generate controller emails --naming=camelCase
we have two schematic options:SchmaticOption('name', 'emails')
SchmaticOption('suffix', 'controller')
We need to apply the
camelCase
transform to both, hence why I used a 3 way option.If I misunderstood you please let me know, or maybe you can see something I'm not seeing.
One option could be to open a draft PR and ask the
nestjs
authors for advice.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.
Proposal: nameCase becomes an extensible object (additionalParameters = { caseNaming:
hello
})