Created
May 4, 2022 12:31
-
-
Save LostKobrakai/e6d7f562d1194b2f7083f6d8adf0bfa4 to your computer and use it in GitHub Desktop.
Phoenix.HTML.Safe implementation(s)
This file contains hidden or 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
defimpl Phoenix.HTML.Safe, for: Version do | |
# Based on `to_string` implementation | |
# https://github.com/elixir-lang/elixir/blob/v1.13.4/lib/elixir/lib/version.ex#L629 | |
def to_iodata(%Version{} = version) do | |
[ | |
Integer.to_string(version.major), | |
Integer.to_string(version.minor), | |
[Integer.to_string(version.patch), pre(version.pre), build(version.build)] | |
] | |
|> Enum.intersperse(".") | |
end | |
defp build(nil) do | |
"" | |
end | |
defp build(build) do | |
["+", Phoenix.HTML.Safe.to_iodata(build)] | |
end | |
defp pre([]) do | |
"" | |
end | |
defp pre(pre) do | |
pre | |
|> Enum.map(fn | |
int when is_integer(int) -> Integer.to_string(int) | |
string when is_binary(string) -> Phoenix.HTML.Safe.to_iodata(string) | |
end) | |
|> Enum.intersperse(".") | |
|> List.insert_at(0, "-") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment