iancoleman / strcase

A golang package for converting to snake_case or CamelCase
MIT License
1.01k stars 106 forks source link

Number word boundary bug #22

Open jpotterm opened 4 years ago

jpotterm commented 4 years ago

Numbers usually act as word boundaries, but not always (which I believe is a bug). Here are inconsistent test cases:

strcase.ToSnake("1A2") // 1a_2
strcase.ToSnake("A1B") // a_1_b
nasermirzaei89 commented 4 years ago

I also have this problem in this case:

strcase.ToSnake("ID3") // returns id_3 but I think it should return id3
strcase.ToSnake("ID3v2_3") // returns id_3_v2_3 but I think it should return id3_v2_3

version commit I use: 16388991a33441046539eb716cff4d294d556c70

iancoleman commented 4 years ago

24 introduces the change for @jpotterm with these tests:

https://github.com/iancoleman/strcase/blob/23e9d4e5c09d4767bb6a4d9fdc49a9d548b70898/snake_test.go#L51-L52

@nasermirzaei89 I'm not sure what the particular rule would be to differentiate those particular cases, can you describe the intention please? It looks like maybe you mean "any numbers following the token 'ID' or 'v' should be considered part of that token and not split from it"? Is it specifically for <ID|V>+?

The current rule is that numbers act as word boundaries, so ToSnake("ID3v2_3") gives "id_3_v_2_3".

NathanBaulch commented 4 years ago

I think the intention is to only introduce delimiters after numbers, not before them. This is a super easy change with my recent contributions, see here. I'd also prefer this behavior but didn't include it in #24 since the current behavior isn't necessarily wrong so it feels a little more like a breaking change.

nasermirzaei89 commented 4 years ago

@iancoleman I think the solution of @NathanBaulch is good. You should check if it doesn't break any other cases. Thanks for the follow up 🎉

rami-dabain commented 3 years ago

24 is looks merged, however it doesn't solve the numbering issue:

ID3v2_3 should become id3_v2_3 but currently it gives id_3_v_2_3 or is there a config/param that i can't find to switch between the 2 methods?

wichert commented 2 years ago

There are some many possibilities here, that I doubt you can make this work for everyone without introducing a bunch of configuration options. In my case I am looking at Position3D being changed to position_3_d, which I would prefer to be position_3d.

amanenk commented 2 years ago

I have faced the same issue. I want Int8Value -> int8_value but strcase generates int_8_value. I have tried to strcase.ConfigureAcronym("Int8", "int8") but it didn't help. That would be awesome to use numbers in acronyms (the issue above can be solved via adding 3D to acronyms) and adding a config parameter to attach trailing numbers would be another convenient way.