Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Camel case from/to underscore conversion #1

Closed
huandu opened this issue Jan 6, 2015 · 4 comments
Closed

Feature: Camel case from/to underscore conversion #1

huandu opened this issue Jan 6, 2015 · 4 comments

Comments

@huandu
Copy link
Owner

huandu commented Jan 6, 2015

Camel case to underscore.

  • SomeWords => some_words
  • HTTPServer => http_server
  • NoHTTPS => no_https

Underscore to camel case.

  • some_words => SomeWords
  • http_server => HttpServer
  • no_https => NoHttps
  • _complex__case_ => _Complex_Case_
@huandu
Copy link
Owner Author

huandu commented Aug 25, 2018

Add a new rule in camel case to underscore (snake) case.

  • Add _ before a number if there is a character other than a number, blank, underscore or hyphen.

Some cases.

  • HTTP2XX => http_2xx
  • http2xx => http_2xx
  • HTTP20xOK => http_20x_ok

@huandu
Copy link
Owner Author

huandu commented Jan 12, 2020

As snake/kebab case conversion is aware of space, ToCamelCase should be aware of it too.

To make the conversion rule clear, we define it as following.

Camel/snake/kebab case conversion is to convert words separated by space, underscore and hyphen to expected case. For snake or kebab case conversion, when an upper case rune is converted to lower case, a new underscore or hyphen is inserted before the rune.

For the string FROM CamelCase to snake/kebab-case, following are the results after conversion.

  • Camel case: FromCamelcaseToSnake/kebabCase.
  • Snake case: from_camel_case_to_snake/kebab_case.
  • Kebab case: from-camel-case-to-snake/kebab-case.

@huandu
Copy link
Owner Author

huandu commented Apr 7, 2020

Add a new rule to make string starting with number more readable.

If there are lower case characters immediately following numbers, e.g. 20x in HTTP20x or 2m3s in Duration2m3s, all these numbers and lower case characters will be considered as one word during conversion. Otherwise, numbers will be considered as a part of previous word, e.g. Bld4 in Bld4Floor3rd.

Following are some samples.

  • HTTP20x => http_20x
  • Duration2m3s => duration_2m3s
  • Bld4Floor3rd => bld4_floor_3rd

@huandu
Copy link
Owner Author

huandu commented Jun 6, 2024

BREAKING CHANGE

Per discussion in #56 and the history around PascalCase and CamelCase, ToCamelCase should return first letter in lowercase. Instead, add a new api ToPascalCase to cover the old test cases.

For the string FROM CamelCase to snake/kebab-case, following are the results after conversion.

  • Camel case: fromCamelcaseToSnake/kebabCase.
  • Pascal case: FromCamelcaseToSnake/kebabCase.
  • Snake case: from_camel_case_to_snake/kebab_case.
  • Kebab case: from-camel-case-to-snake/kebab-case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant