bendrucker / snakecase-keys

Convert an object's keys to snake case
MIT License
182 stars 32 forks source link

Keys ending with numbers fail to snake_case #119

Open andymerskin opened 1 year ago

andymerskin commented 1 year ago

Due to the way the snake-case package handles numbers, keys ending with numbers aren't being properly snake_cased.

Example keys:

fooBar01 -> foo_bar01
foo01 -> foo01
fooBar1 -> foo_bar1
foo1 -> foo1

Whereas lodash's snakeCase utility does properly separate numbers from alphabetical characters:

fooBar01 -> foo_bar_01
foo01 -> foo_01
fooBar1 -> foo_bar_1
foo1 -> foo_1

This may mean this library's underlying case conversion library would need to be swapped, or updated, but unfortunately it hasn't been maintained in several years.

bendrucker commented 1 year ago

This seems like an opinion rather than a correctness issue:

https://stackoverflow.com/questions/58009622/whats-the-correct-way-to-treat-numbers-in-snake-case

In which case I'm not especially inclined to pursue a breaking change.

andymerskin commented 1 year ago

Could this offered as a configurable option, especially since the consensus in that opinion favors what I'm suggesting here? It would also be beneficial for this library to at least offer comparable functionality to popular libraries like lodash, etc. so that case conversion can remain consistent and predictable.

Example with option:

const object = {
  foo01: "value",
};

snakecaseKeys(object, { separateNumbers: true });
// { foo_01: "value" }
bendrucker commented 1 year ago

Probably not, as maintaining variations on what snake case means becomes very complex. And bringing in multiple libraries to handle those is both complex and bloats the package.

I'll certainly accept a PR that makes the snake case function an option so you can pass your own.

andymerskin commented 1 year ago

Fair points — I love that idea better!