Humanizr / Humanizer

Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities
Other
8.68k stars 965 forks source link

[Bug] `Camelize()` does not respect acronyms #1301

Open iikuzmychov opened 1 year ago

iikuzmychov commented 1 year ago

Issue

I have an issue using Camelize() method. It works not exactly as I expected.

Example

The next code

"IOModule".Camelize()

will return

"iOModule"

while I'm expecting

"ioModule"

I think it's a bug, we do need respect acronyms on camelizing strings.

Proposed solution

I took the original code of Camelize() method and reworked it a bit:

public static string CamelizeRespectingAcronyms(this string input)
{
  var pascalized = input.Pascalize();

  if (pascalized.Length == 0)
  {
    return pascalized;
  }

  var firstUpperSymbolsCount = pascalized.TakeWhile(char.IsUpper).Count();

  if (firstUpperSymbolsCount > 2)
  {
    var acronymLength = firstUpperSymbolsCount - 1;

    return pascalized[..acronymLength].ToLower() + pascalized[acronymLength..];
  }

  return pascalized[..1].ToLower() + pascalized[1..];
}

The update method is returning

"ioModule"

for

"IOModule".Capitilize()
oreze commented 5 months ago

IMO there should also be an option to choose if we want to camelize acronyms such as “some_JSON_data” to > “someJsonData” or “SomeJSONData”. I can prepare some PR later, I am new here and want to look around