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

Feature Request: CamelCase to behave like lodash.camelCase and camelcase #224

Closed
gCardinal opened this issue Jul 1, 2021 · 4 comments · Fixed by #501
Closed

Feature Request: CamelCase to behave like lodash.camelCase and camelcase #224

gCardinal opened this issue Jul 1, 2021 · 4 comments · Fixed by #501
Assignees
Labels
bug Something isn't working component:casing Relates to CamelCase, SnakeCase and similar types help wanted Extra attention is needed

Comments

@gCardinal
Copy link

gCardinal commented Jul 1, 2021

Right now, CamelCase (and, obviously, CamelCasedProperties and CamelCasedPropertiesDeep) behaves weirdly when handling two character strings.

When it's a 2 character string (like ID, for example), it will transform the string as expected (ID to id), but strings like HostIDPlayer will leave the ID part untouched (HostIDPlayer to hostIDPlayer).

const a: CamelCase<'ID'> = 'id';  // Works, no TS error 
const b: CamelCase<'HostIDPlayer'> = 'hostIdPlayer';  // Error, TS expects 'hostIDPlayer'

Libraries like lodash.camelCase and camelcase will transform the strings as expected as can be seen in this codesandbox.

I can't say I know enough about TS' type system, but is there a way to make CamelCase behave like lodash.camelCase and camelcase? That was, to me, what I expected the behavior to be.

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
Copy link
Owner

I agree with you. It's how I prefer doing camel-case. But there are definitely some people that prefer the existing behavior...

For example, the camelcase package has an option to enable the current behavior: https://github.com/sindresorhus/camelcase#preserveconsecutiveuppercase

@wwwzbwcom Thoughts on this?

@wwwzbwcom
Copy link
Contributor

Maybe add a option to toggle it?

@voxpelli
Copy link
Collaborator

Right, as CamelCase<> is currently implemented, it:

  • Splits words separated by -, _ or a white space
  • It lower cases the first character always
  • If all characters in the input are upper case, then prior to doing anything, they are all lower cased

It does not split on uppercase characters in any way, unlike the DelimiterCase based ones.

Though the DelimiterCase ones would probably also not handle this very well, eg. KebabCase<'HostIDPlayer'> would probably give a string literal that's 'host-i-d-player'.

Currently CamelCase does look at the previous character when it decides what to do, but currently DelimiterCase doesn't.

To support this use case I think we essentially would have to merge the functionality of the two. I can't remember why I didn't do that initially.

@voxpelli
Copy link
Collaborator

voxpelli commented Oct 13, 2022

There is one major difference between the current implementation in type-fest and the implementation in the camelcase module:

const camelFromPascal: CamelCase<'FooBar'> = 'fooBar';
expectType<'fooBar'>(camelFromPascal);

type-fest supports conversion from pascal case to camel case.

One could argue that this should be dropped because one can just as well use the TS Uncapitalize<> helper instead, but it would still require a change in logic here.

Also, as a reference, CamelCase<'ID'> was added in: #220

And support for conversion from screaming kebab/snake/space cases was added in: #234

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

Successfully merging a pull request may close this issue.

5 participants