Last active
June 15, 2020 19:38
-
-
Save ideaMarcos/096f415e8c42b4db72d4251aaede78bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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