Skip to content

Instantly share code, notes, and snippets.

@LostKobrakai
Created May 4, 2022 12:31
Show Gist options
  • Save LostKobrakai/e6d7f562d1194b2f7083f6d8adf0bfa4 to your computer and use it in GitHub Desktop.
Save LostKobrakai/e6d7f562d1194b2f7083f6d8adf0bfa4 to your computer and use it in GitHub Desktop.
Phoenix.HTML.Safe implementation(s)
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