Recase: convert strings to any case (from any case!)

Recase helps you to convert a string from any case to any case.
Link: GitHub - wemake-services/recase: ♻ Convert strings to any case.

Why?

One can ask: “Why should I use recase when I can use built-in Macro module?”. But, you have to keep in mind that Macro’s functions are not suitable for general case usage:

Do not use it as a general mechanism for underscoring or camelizing strings as it does not support Unicode or characters that are not valid in Elixir identifiers.

And Recase has even more cases!

Installation

def deps do
  [{:recase, "~> 0.2"}]
end

Examples

Pascal

Pascal (or Upper) case uses mixed case with lower and upper cased characters. Separates words from each other with the upper case letters. Starts with upper case letter.

Recase.to_pascal("some-value") # => "SomeValue"
Recase.to_pascal("Some value") # => "SomeValue"

Camel

Camel case uses mixed case with lower and upper cased characters. Separates words from each other with the upper cased letters. Starts with lower case letter.

Recase.to_camel("some-value") # => "someValue"
Recase.to_camel("Some Value") # => "someValue"

Snake

Snake cases uses all lower case. Uses _ as a separator.

Recase.to_snake("someValue") # => "some_value"
Recase.to_snake("Some Value") # => "some_value"

Kebab

Kebab cases uses all lower case. Uses - as a separator.

Recase.to_kebab("someValue") # => "some-value"
Recase.to_kebab("Some Value") # => "some-value"

Constant

Constant case uses all upper case. Uses _ as a separator.

Recase.to_constant("SomeValue") # => "SOME_VALUE"
Recase.to_constant("some value") # => "SOME_VALUE"

Dot

Dot case uses all lower case similar to snake case. Uses . as a separator.

Recase.to_dot("SomeValue") # => "some.value"
Recase.to_dot("some value") # => "some.value"

Path

Path case preserves case, you can also provide a separator as the second argument.

Recase.to_path("SomeValue") # => "Some/Value"
Recase.to_path("some value", "\\") # => "some\\value"
4 Likes

I like the intention-revealing names!

I’d suggest you might want to add an optional argument or a generic function to pass in any separator so you have the same flexibility as similar libraries like Inflex. That’d cover you for cases where someone is using a less common separator.

Definitely useful for sure!

Thanks! Great idea.

Something like: to_generic/2.
Do you want to contribute?

I’m afraid I won’t have time for a good while. Deadlines approaching. :sweat:

Perhaps to_lower(operator \ _) as I would assume the most common case would be to lowercase?

FWIW: since Recase does not handle different languages properly, I created Case2 → convertion between different casing with full unicode support

Why have not you just fired an issue over recase? That’s sad.

1 Like

Well, I dislike the whole approach with regexps.They should be used as a last resort, binary pattern mathing is faster as you might see in my benchmarks.
I thought about providing the PR but it literally would be drop all your code and use mine. I had a doubt this kind of PR would be appreciated :slight_smile:

I am still fine with you backporting my code so that I could amend my package.

1 Like

Sure, let’s make this PR happen! What help do you need from my side?

4 Likes

Eh. None :slight_smile:

I will do my best to make it till tomorrow noon.

2 Likes