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

feat: Introduce optional enhancers to casing function #884

Merged
merged 1 commit into from
Jan 13, 2020

Conversation

nulltoken
Copy link
Contributor

@nulltoken nulltoken commented Jan 3, 2020

Fixes #788
Checklist

  • Tests added / updated
  • Docs added / updated

Does this PR introduce a breaking change?

  • Yes
  • No

src/functions/casing.ts Outdated Show resolved Hide resolved
@nulltoken
Copy link
Contributor Author

@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,
Copy link
Contributor

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."

Copy link
Contributor Author

@nulltoken nulltoken Jan 5, 2020

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?

src/types/rule.ts Outdated Show resolved Hide resolved
src/functions/casing.ts Outdated Show resolved Hide resolved
);
});

test('allows advanced scenarios', () => {
Copy link
Contributor Author

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

@nulltoken nulltoken requested a review from P0lip January 6, 2020 22:35
src/functions/casing.ts Outdated Show resolved Hide resolved
src/functions/casing.ts Outdated Show resolved Hide resolved
src/functions/casing.ts Outdated Show resolved Hide resolved
src/functions/casing.ts Outdated Show resolved Hide resolved
src/types/rule.ts Outdated Show resolved Hide resolved
@nulltoken nulltoken force-pushed the ntk/casing_separators branch from 5375e91 to 862a0a1 Compare January 10, 2020 08:01
@nulltoken nulltoken changed the title [WIP] Introduce optional enhancers to casing function feat: Introduce optional enhancers to casing function Jan 10, 2020
@nulltoken
Copy link
Contributor Author

@philsturgeon @P0lip I've cleaned up the history, rebased and updated the doco. Would you please take another pass at this proposal?

@P0lip P0lip added the enhancement New feature or request label Jan 10, 2020
P0lip
P0lip previously approved these changes Jan 10, 2020
src/functions/casing.ts Outdated Show resolved Hide resolved
}

return;
if (opts.allowLeadingSeparator !== undefined && opts.separator === undefined) {
throw new AssertionError({ message: "'allowLeadingSeparator' can only be valued when 'separator' is defined." });
Copy link
Contributor

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.


interface ICasingOptionsOverrides {
disallowDigits?: boolean;
separator?: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
separator?: string;
interface ICasingOptionsOverridesWithSeperator {
disallowDigits?: boolean;
separator: string;
allowLeadingSeparator: boolean;

Copy link
Contributor

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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}
export type CasingOptions = ICasingOptions & ICasingOptionsOverrides | ICasingOptions & ICasingOptionsOverridesWithSeperator;

Copy link
Contributor

@philsturgeon philsturgeon left a 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!

Copy link
Contributor

@P0lip P0lip left a 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.

src/functions/__tests__/casing.test.ts Outdated Show resolved Hide resolved
@nulltoken
Copy link
Contributor Author

nulltoken commented Jan 13, 2020

@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 CasingOptions type declarations.

And although I could tweak it by throwing some anys at it or type guards, I'm not really sure it's really worth it. As this type declaration will not offer any real run time protection with regards to what the users could inject in their rulesets.

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.

@nulltoken nulltoken force-pushed the ntk/casing_separators branch from 6cc7b2a to a631e26 Compare January 13, 2020 21:19
@nulltoken
Copy link
Contributor Author

nulltoken commented Jan 13, 2020

@P0lip I gave some thought about it and I'm going to try and tweak the interface into something like

export interface ICasingOptions {
  type: CasingType;
  disallowDigits?: boolean;
  separator?: {
    char: string;
    allowLeading?: boolean;
  }
}

which I believe might actually convey more meaning than the initial interface I came out with (and might not need some elaborate type tweaking ;-) ).

@nulltoken nulltoken force-pushed the ntk/casing_separators branch from a631e26 to 03028c1 Compare January 13, 2020 22:04
@nulltoken
Copy link
Contributor Author

@P0lip The new proposal has been pushed. What do you think about it?

@nulltoken nulltoken force-pushed the ntk/casing_separators branch from 03028c1 to df13c46 Compare January 13, 2020 22:07
@nulltoken nulltoken force-pushed the ntk/casing_separators branch from df13c46 to 56d9e10 Compare January 13, 2020 22:08
Copy link
Contributor

@P0lip P0lip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@P0lip P0lip merged commit 4bd5614 into develop Jan 13, 2020
@P0lip P0lip deleted the ntk/casing_separators branch January 13, 2020 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Casing function: allow hyphens or slashes
3 participants