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

SnakeCaseProperties doesn't handle PascalCase and UPPERCASE #223

Open
zirkelc opened this issue Jun 26, 2021 · 2 comments
Open

SnakeCaseProperties doesn't handle PascalCase and UPPERCASE #223

zirkelc opened this issue Jun 26, 2021 · 2 comments
Labels
bug Something isn't working component:casing Relates to CamelCase, SnakeCase and similar types help wanted Extra attention is needed

Comments

@zirkelc
Copy link

zirkelc commented Jun 26, 2021

I have a type with PascalCase and a few uppercase properties, e.g.

Identifiers: {
    MarketplaceASIN: {
	MarketplaceId: string;
	ASIN: string;
    },
}

SnakeCasedPropertiesDeep turns this into:

_identifiers: {
    _marketplace_a_s_i_n: {
        _marketplace_id: string;
        _a_s_i_n: string;
    };
};

It doesn't handle the cases where the property starts with an uppercase and where the property contains multiple uppercase letters in a row.

I could overcome the first issue by using CamelCasedPropertiesDeep first.

What I would expect is:

identifiers: {
    marketplace_asin: {
        marketplace_id: string;
        asin: string;
    };
};

During runtime I use lodash's https://lodash.com/docs/4.17.15#snakeCase method. Is there a way to use the same method for the type?

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus sindresorhus added bug Something isn't working help wanted Extra attention is needed labels Jun 26, 2021
@threehams
Copy link

threehams commented Aug 4, 2021

This looks like it might be fairly simple to fix. I've hacked in a userland fix for screaming snake case with this:

type IsScreamingSnakeCase<TString extends string> = Split<TString, ""> extends (
  | UpperCaseCharacters
  | WordSeparators
  | StringDigit
)[]
  ? true
  : false

type SnakeCase = <TString extends string>(
  str: TString,
) => IsScreamingSnakeCase<TString> extends true
  ? SnakeCase<Lowercase<TString>>
  : SnakeCase<TString>;

I'll submit a PR when I have time, hopefully it's as simple as it seems. PascalCase detection could be similar, hopefully, just lowercase the first letter in cases of mixed-case.

edit:

Narrator: It wasn't as simple as it seemed.

@voxpelli
Copy link
Collaborator

voxpelli commented Sep 22, 2021

Best solution would be the one described here I think: #224 (comment)

@voxpelli voxpelli added the component:casing Relates to CamelCase, SnakeCase and similar types label Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working component:casing Relates to CamelCase, SnakeCase and similar types help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants