You can use Macro.underscore/1 , but this is not the right way to do this. Since the Macro module itself states :
This feature was designed to emphasize language identifiers / tokens, so it belongs to the Macro module. Do not use it as a general underline mechanism, as it does not support Unicode or characters that are not allowed in Elixir identifiers.
So itβs better to use a different library. I would recommend using recase . It can convert a string anyway, not just camelCase .
Since this is a third-party library, you need to install it.
- add this line to
mix.exs in deps : {:recase, "~> 0.6"} Be sure to use the latest version! - run
mix deps.get
Here's how you use it:
Recase.to_camel("some-value") # => "someValue" Recase.to_camel("Some Value") # => "someValue"
You can find the documents here: https://hexdocs.pm/recase/readme.html
And the repo is here: https://github.com/sobolevn/recase
source share