blakeembrey / change-case

Convert strings between camelCase, PascalCase, Capital Case, snake_case and more
MIT License
2.21k stars 92 forks source link

Bug with case conversions for snake case with numbers #313

Closed bminer closed 7 months ago

bminer commented 8 months ago

Here's a new bug in v5.

Whenever a number is prefixed by a _ in snake case, change-case does not properly convert to the appropriate case.

Example:

changeCase.pascalCase("email_1_address", {separateNumbers: false})
changeCase.pascalCase("email_1_address", {separateNumbers: true})

Output: 'Email_1Address' in both cases Expected: Email1Address, I think in both cases

This even breaks when converting cases back and forth:

changeCase.camelCase(
  changeCase.snakeCase("email1Address", {separateNumbers: true}
), {separateNumbers: true})

Output: 'email_1Address' Expected: email1Address

bminer commented 8 months ago

This was working in v4 when using the camelCaseTransformMerge function.

changeCase.camelCase(str, {
  transform: changeCase.camelCaseTransformMerge,
})
blakeembrey commented 8 months ago

Have you tried disabling the separateNumbers option? I think that should do what you’re asking for, you just have to be aware that it’s not reversible (same as v4 though).

Edit: Sorry, was on mobile/vacation, I see you tried it. I’ll have to look when I’m back at my computer.

blakeembrey commented 7 months ago

I have published a new release with a mergeAmbiguousCharacters option for camelCase and pascalCase.

For the sake of this issue, I do want to clarify that this isn't a bug but expected behavior (without the option enabled). It works the same way in v4.

The behavior wasn't related to separateNumbers (which splits V1 into V_1), but instead the standard formatting of these two methods that prefixes an _ before numbers which can be unclear. Using your example, it's impossible to reverse email_1_address if it turns into Email1Address because it may be intentional to do email1 vs email_1.

Anyway, hopefully mergeAmbiguousCharacters supports your use-case in v5.2.

bminer commented 7 months ago

Looks great! Thanks, Blake!