sindresorhus / camelcase

Convert a dash/dot/underscore/space separated string to camelCase: foo-bar → fooBar
MIT License
675 stars 95 forks source link

Converting either volume_3d or volume3d results in volume3D making it impossible to decamelize #77

Closed fc closed 1 year ago

fc commented 4 years ago

How to reproduce:

console.log(camelcase('volume_3d')); // volume3D
console.log(camelcase('volume3d')); // volume3D

this operates under the assumption that decamelizing should return the string to its original decamelized version but I'm not sure how that would be possible for strings in this format.

sindresorhus commented 4 years ago

What would expect the second case to camelize to?

fc commented 4 years ago

This is what I was thinking: volume_3d -> volume3d volume3d -> volume3D That's the only way I can imagine it working while still allowing a way to reverse it to its original form but certainly this seems like it could break something but can't think of other possiblities.

Qix- commented 4 years ago

Seems inconsistent. I'm :-1:.

In your two examples, you have two different conventions: snake_case and lowercase. That's like expecting myvariable to somehow automatically camel-case to myVariable.

Seems to reverse correctly to me.

fc commented 4 years ago

Some context... using decamelize right now converts volume3D to volume3_d. Am I right to assume that calling decamelize should revert the string to its original camelcased form?

My specific use case is: I get an API result that contains an object with a property volume_3d but on the frontend I want all keys of the objects to be volume3d (or volume3D), and if I am submitting back to the backend which expects the snakecase format, I want to just decamelize that volume3dto volume_3d but right now that does not work.

sindresorhus commented 4 years ago

The conversion to/from camel-case is not stable and I don't think it's feasible or even ideal to do so. I think the solution here is to document in both readme's that you cannot assume stability both ways.

dangreen commented 3 years ago

Hi. I'v got same problems:

Expected: b2b_registration_request -> b2bRegistrationRequest Received: b2b_registration_request -> b2BRegistrationRequest

Most other same libs works as expected, but we use camelcase-keys package which use this package.

dangreen commented 3 years ago

Libs examples:

> require('to-camel-case')('b2b_registration_request')
'b2bRegistrationRequest'
> require('to-snake-case')('b2bRegistrationRequest')
'b2b_registration_request'
> require('decamelize')('b2bRegistrationRequest')
'b2b_registration_request'
emmanuelsio commented 2 years ago

I have the same issue

var camelcaseKeys = require("camelcase")
camelcaseKeys('uses_log4j'); 
//=> usesLog4J

Why after number makes upper case the following character? The result migth be usesLog4j

sindresorhus commented 2 years ago

Expected: b2b_registration_request -> b2bRegistrationRequest

I agree. That should be fixed. PR welcome.

JCrandall101 commented 1 year ago

Hey @sindresorhus, I open PR-112 to be able to handle the b2b_registration_request issue. Is this what you were looking for?