Skip to content

Instantly share code, notes, and snippets.

@ideaMarcos
Last active June 15, 2020 19:38
Show Gist options
  • Save ideaMarcos/096f415e8c42b4db72d4251aaede78bd to your computer and use it in GitHub Desktop.
Save ideaMarcos/096f415e8c42b4db72d4251aaede78bd to your computer and use it in GitHub Desktop.
defmodule ExMachinaHelper do
@doc """
Converts all characters in the given string to a random case according to `:default` mode.
Useful for writing tests where sorting is of importance.
## Examples
iex> ExMachinaHelper.random_case("abcdef")
"AbCdEf"
iex> ExMachinaHelper.random_case("abcdef")
"abcdef"
"""
def random_case(string) when is_binary(string) do
[:upcase, :downcase, :alternate, :none]
|> Enum.random()
|> case do
:upcase ->
String.upcase(string)
:downcase ->
String.downcase(string)
:alternate ->
string
|> String.upcase()
|> String.split("")
|> Enum.map_every(2, &String.downcase/1)
|> Enum.join()
_ ->
string
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment