Phoenix.HTML.Safe impl

This commit is contained in:
Sloane Perrault 2023-07-07 13:16:46 -04:00
parent 2c0a468ce2
commit e274f3bf1d
No known key found for this signature in database
5 changed files with 55 additions and 7 deletions

View file

@ -1,8 +1,9 @@
# Changelog
## main
## 0.3.1
- Implements `String.Chars` protocol
- Implements `Phoenix.HTML.Safe` protocol
## 0.3.0

View file

@ -67,6 +67,30 @@ defmodule TypeID do
suffix
end
@doc """
Returns an `t:iodata/0` representation of the given `t:t/0`.
### Examples
iex> tid = TypeID.from_string!("player_01h4rn40ybeqws3gfp073jt81b")
iex> TypeID.to_iodata(tid)
["player", "_", "01h4rn40ybeqws3gfp073jt81b"]
iex> tid = TypeID.from_string!("01h4rn40ybeqws3gfp073jt81b")
iex> TypeID.to_iodata(tid)
"01h4rn40ybeqws3gfp073jt81b"
"""
@spec to_iodata(tid :: t()) :: iodata()
def to_iodata(%__MODULE__{prefix: "", suffix: suffix}) do
suffix
end
def to_iodata(%__MODULE__{prefix: prefix, suffix: suffix}) do
[prefix, "_", suffix]
end
@doc """
Returns a string representation of the given `t:t/0`
@ -78,12 +102,10 @@ defmodule TypeID do
"""
@spec to_string(tid :: t()) :: String.t()
def to_string(%__MODULE__{prefix: "", suffix: suffix}) do
suffix
end
def to_string(%__MODULE__{prefix: prefix, suffix: suffix}) do
prefix <> "_" <> suffix
def to_string(%__MODULE__{} = tid) do
tid
|> to_iodata()
|> IO.iodata_to_binary()
end
@doc """
@ -340,3 +362,9 @@ end
defimpl String.Chars, for: TypeID do
defdelegate to_string(tid), to: TypeID
end
if Code.ensure_loaded?(Phoenix.HTML.Safe) do
defimpl Phoenix.HTML.Safe, for: TypeID do
defdelegate to_iodata(tid), to: TypeID
end
end

View file

@ -43,6 +43,7 @@ defmodule TypeID.MixProject do
defp deps do
[
{:ecto, "~> 3.10", only: [:dev, :test], optional: true},
{:phoenix_html, "~> 3.3", only: [:dev, :test], optional: true},
{:ex_doc, "~> 0.27", only: :dev, runtime: false},
{:yaml_elixir, "~> 2.9", only: [:dev, :test], runtime: false}
]

View file

@ -7,6 +7,7 @@
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
"makeup_erlang": {:hex, :makeup_erlang, "0.1.2", "ad87296a092a46e03b7e9b0be7631ddcf64c790fa68a9ef5323b6cbb36affc72", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f3f5a1ca93ce6e092d92b6d9c049bcda58a3b617a8d888f8e7231c85630e8108"},
"nimble_parsec": {:hex, :nimble_parsec, "1.3.1", "2c54013ecf170e249e9291ed0a62e5832f70a476c61da16f6aac6dca0189f2af", [:mix], [], "hexpm", "2682e3c0b2eb58d90c6375fc0cc30bc7be06f365bf72608804fb9cffa5e1b167"},
"phoenix_html": {:hex, :phoenix_html, "3.3.1", "4788757e804a30baac6b3fc9695bf5562465dd3f1da8eb8460ad5b404d9a2178", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "bed1906edd4906a15fd7b412b85b05e521e1f67c9a85418c55999277e553d0d3"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"yamerl": {:hex, :yamerl, "0.10.0", "4ff81fee2f1f6a46f1700c0d880b24d193ddb74bd14ef42cb0bcf46e81ef2f8e", [:rebar3], [], "hexpm", "346adb2963f1051dc837a2364e4acf6eb7d80097c0f53cbdc3046ec8ec4b4e6e"},
"yaml_elixir": {:hex, :yaml_elixir, "2.9.0", "9a256da867b37b8d2c1ffd5d9de373a4fda77a32a45b452f1708508ba7bbcb53", [:mix], [{:yamerl, "~> 0.10", [hex: :yamerl, repo: "hexpm", optional: false]}], "hexpm", "0cb0e7d4c56f5e99a6253ed1a670ed0e39c13fc45a6da054033928607ac08dfc"},

View file

@ -0,0 +1,17 @@
defmodule TypeID.PhoenixHTMLSafeTest do
use ExUnit.Case
test "to_iodata/1" do
assert {:ok, tid} = TypeID.from_string("test_01h4rm6n03esc96rwqtnq2fr5a")
assert "test_01h4rm6n03esc96rwqtnq2fr5a" ==
tid |> Phoenix.HTML.Safe.to_iodata() |> IO.iodata_to_binary()
end
test "no prefix" do
assert {:ok, tid} = TypeID.from_string("01h4rm6n03esc96rwqtnq2fr5a")
assert "01h4rm6n03esc96rwqtnq2fr5a" ==
tid |> Phoenix.HTML.Safe.to_iodata() |> IO.iodata_to_binary()
end
end