Skip to content

Handling of Abbreviations and Acronyms #7

@Fleshgrinder

Description

@Fleshgrinder

Abbreviations and acronyms are dirty business with both lower and upper camel case. Currently the library is doing a perfect job at dealing with them and is too strict in the transformation logic. Here are a few examples:

transform actual expected
"HTTPException".toLowerCamelCase() httpexception httpException
"HTTPException".toUpperCamelCase() Httpexception HttpException
"HTTPException".toLowerDashCase() httpexception http-exception
"HTTPException".toUpperDashCase() HTTPEXCEPTION HTTP-EXCEPTION
"HTTPException".toLowerSnakeCase() httpexception http_exception
"HTTPException".toUpperSnakeCase() HTTPEXCEPTION HTTP_EXCEPTION

There are obviously cases where it becomes impossible to translate these things, this is exactly the reason why people should always treat any abbreviation or acronym like any normal word. This parseDBMXMLFromIPAddress is from Wikipedia and is always going to be transformed incorrectly. However, the following should work as expected.

transform actual expected
"NonlinearXWave".toLowerCamelCase() nonlinearXwave nonlinearXWave
"NonlinearXWave".toUpperCamelCase() NonlinearXwave NonlinearXWave
"NonlinearXWave".toLowerDashCase() nonlinear-xwave nonlinear-x-wave
"NonlinearXWave".toUpperDashCase() NONLINEAR-XWAVE NONLINEAR-X-WAVE
"NonlinearXWave".toLowerSnakeCase() nonlinear_xwave nonlinear_x_wave
"NonlinearXWave".toUpperSnakeCase() NONLINEAR_XWAVE NONLINEAR_X_WAVE

I think that this is doable by treating the last uppercase letter as the beginning of a new word. This is most definitely going to break some other inputs, but it should produce the desired output at least for those inputs I can think of. I am not sure yet how this can be done in any non-camel case format, simply because they usually ignore case altogether. Converting to camel first to then convert to the target seems very wasteful …

It definitely makes sense to look at some other case format libraries, not only JVM, but also other languages. Maybe someone already had a good idea on how to deal with this. Or maybe it's just a fool's errand?

Metadata

Metadata

Assignees

Labels

🐛 bugSomething isn't working

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions