-
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 17 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,7 +1,61 @@ | ||
import { | ||
camelCase, | ||
kebabCase, | ||
pascalCase, | ||
snakeCase, | ||
capitalCase, | ||
} from 'case-anything'; | ||
|
||
export type CaseType = | ||
| 'kebab' | ||
| 'snake' | ||
| 'camel' | ||
| 'pascal' | ||
| 'capital' | ||
| 'kebab-or-snake'; | ||
|
||
/** | ||
* | ||
* @param str | ||
* @returns formated string | ||
* @param caseType CaseType | ||
* @returns formatted string | ||
* @description normalizes input to a given case format. | ||
*/ | ||
export const normalizeToCase = ( | ||
str: string, | ||
caseType: CaseType, | ||
) => { | ||
switch (caseType) { | ||
case 'kebab': | ||
return kebabCase(str); | ||
case 'snake': | ||
return snakeCase(str); | ||
case 'camel': | ||
return camelCase(str); | ||
case 'pascal': | ||
return pascalCase(str); | ||
case 'capital': | ||
return capitalCase(str); | ||
// For legacy purposes | ||
case 'kebab-or-snake': | ||
default: | ||
return normalizeToKebabOrSnakeCase(str); | ||
} | ||
}; | ||
|
||
export const formatString = (str: string) => { | ||
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. 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 commentThe 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 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. ok I updated the same code also in |
||
return str.split('').reduce((content, char) => { | ||
if (char === '(' || char === ')' || char === '[' || char === ']') { | ||
return `${content}\\${char}`; | ||
} | ||
return `${content}${char}`; | ||
}, ''); | ||
}; | ||
|
||
/** | ||
* | ||
* @param str | ||
* @returns formatted string | ||
* @description normalizes input to supported path and file name format. | ||
* Changes camelCase strings to kebab-case, replaces spaces with dash and keeps underscores. | ||
*/ | ||
|
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,25 @@ | ||
import { normalizeToCase, CaseType } from '../../../lib/utils/formatting'; | ||
|
||
type TestSuite = { | ||
description: string; | ||
input: string; | ||
caseType: CaseType; | ||
expected: string; | ||
}; | ||
|
||
describe('Format strings', () => { | ||
const tests: TestSuite[] = [ | ||
espoal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
description: 'From kebab 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 didn't follow what is the kebab-or-snake type 🤔
Also,
since this will cover file names only, as discussed in the issue
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.
About the
kebab-or-snake
case:In the original code there is a case normalization function that is a mix of kebap and snake case. I kept it for historical purposes, but I also introduced separated kebap and snake normalization functions. I would more than willing to remove it.