-
-
Notifications
You must be signed in to change notification settings - Fork 248
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: Introduce optional enhancers to casing function #884
Conversation
@philsturgeon @P0lip This is an early push. Would you mind taking a look at the proposed interface to see it that would fit the need? |
target: unknown, | ||
type: CasingType, | ||
disallowDigits?: boolean, | ||
separatorForCamelAndPascalCasing?: string, |
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.
Maybe it's less about making specific "seperators for camel and pascal", and more... "I would like to be able to enforce kebab casing for this URL, but ofc the URL will have slashes as well as kebab."
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've came up with a different approach where a new separator can be optionally defined to separate "words".
ie. in camel
, with separator /
, allowing digits
valid:
- helloThere/iAm007
- h/elloThere/iAm007
- helloThere/iAm007
invalid:
- helloThere/iAm0/07
- hello/There/iAm007
- helloTherei/Am007
Thoughts?
); | ||
}); | ||
|
||
test('allows advanced scenarios', () => { |
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.
@philsturgeon test cases from initial issue
5375e91
to
862a0a1
Compare
@philsturgeon @P0lip I've cleaned up the history, rebased and updated the doco. Would you please take another pass at this proposal? |
src/functions/casing.ts
Outdated
} | ||
|
||
return; | ||
if (opts.allowLeadingSeparator !== undefined && opts.separator === undefined) { | ||
throw new AssertionError({ message: "'allowLeadingSeparator' can only be valued when 'separator' is defined." }); |
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.
Typings could be tweaked then. See my suggestion.
This is optional though.
src/functions/casing.ts
Outdated
|
||
interface ICasingOptionsOverrides { | ||
disallowDigits?: boolean; | ||
separator?: string; |
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.
separator?: string; | |
interface ICasingOptionsOverridesWithSeperator { | |
disallowDigits?: boolean; | |
separator: string; | |
allowLeadingSeparator: boolean; |
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.
LGTM
macro: /^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$/, | ||
export interface ICasingOptions extends ICasingOptionsOverrides { | ||
type: CasingType; | ||
} |
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.
} | |
} | |
export type CasingOptions = ICasingOptions & ICasingOptionsOverrides | ICasingOptions & ICasingOptionsOverridesWithSeperator; |
862a0a1
to
b72136b
Compare
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.
This looks good once @P0lip's type tweaks are done. Great work!
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.
👍
Just need to have conflicts resolved and could also tweak typings a bit.
@P0lip I may need some help with regards to the proposed type tweakings. Implementing the proposal makes indeed the interface cleaner. However, the TypeScript compiler prevents me from writing code like if (opts.allowLeadingSeparator !== undefined && opts.separator === undefined) {
throw new AssertionError({ message: "'allowLeadingSeparator' can only be valued when 'separator' is defined." });
} because such code doesn't satisfy And although I could tweak it by throwing some The current code is indeed much simpler but covers all the code execution branches. As usual, iIf you feel those type tweakings should really be implemented, I'll work my way through them so that all the current tests keep on passing. Thoughts? Edit: FWIW, you initially mentioned they were optional and I didn't implement the suggestion for the reason described above. |
6cc7b2a
to
a631e26
Compare
@P0lip I gave some thought about it and I'm going to try and tweak the interface into something like
which I believe might actually convey more meaning than the initial interface I came out with (and might not need some elaborate type tweaking ;-) ). |
a631e26
to
03028c1
Compare
@P0lip The new proposal has been pushed. What do you think about it? |
03028c1
to
df13c46
Compare
df13c46
to
56d9e10
Compare
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.
👍
Fixes #788
Checklist
Does this PR introduce a breaking change?