2023-10-26 18:20:38 +03:00
|
|
|
defmodule PlausibleWeb.TextHelpers do
|
|
|
|
@moduledoc false
|
|
|
|
|
|
|
|
@spec pretty_join([String.t()]) :: String.t()
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Turns a list of strings into a string and replaces the last comma
|
|
|
|
with the word "and".
|
|
|
|
|
|
|
|
### Examples:
|
|
|
|
|
|
|
|
iex> ["one"] |> PlausibleWeb.TextHelpers.pretty_join()
|
|
|
|
"one"
|
|
|
|
|
|
|
|
iex> ["one", "two"] |> PlausibleWeb.TextHelpers.pretty_join()
|
|
|
|
"one and two"
|
|
|
|
|
|
|
|
iex> ["one", "two", "three"] |> PlausibleWeb.TextHelpers.pretty_join()
|
|
|
|
"one, two and three"
|
|
|
|
"""
|
|
|
|
def pretty_join([str]), do: str
|
|
|
|
|
|
|
|
def pretty_join(list) do
|
|
|
|
[last_string | rest] = Enum.reverse(list)
|
|
|
|
|
|
|
|
rest_string =
|
|
|
|
rest
|
|
|
|
|> Enum.reverse()
|
|
|
|
|> Enum.join(", ")
|
|
|
|
|
|
|
|
"#{rest_string} and #{last_string}"
|
|
|
|
end
|
2023-11-30 15:30:04 +03:00
|
|
|
|
2023-12-20 17:56:49 +03:00
|
|
|
def pretty_list(list) do
|
|
|
|
list
|
|
|
|
|> Enum.map(&String.replace("#{&1}", "_", " "))
|
|
|
|
|> pretty_join()
|
|
|
|
end
|
|
|
|
|
2023-11-30 15:30:04 +03:00
|
|
|
def format_date_range(date_range) do
|
|
|
|
"#{format_date(date_range.first)} - #{format_date(date_range.last)}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_date(date) do
|
|
|
|
Timex.format!(date, "{Mshort} {D}, {YYYY}")
|
|
|
|
end
|
2023-10-26 18:20:38 +03:00
|
|
|
end
|