File size: 1,185 Bytes
c27e67a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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
def pretty_list(list) do
list
|> Enum.map(&String.replace("#{&1}", "_", " "))
|> pretty_join()
end
def format_date_range(date_range) do
"#{format_date(date_range.first)} - #{format_date(date_range.last)}"
end
def format_date(date) do
Calendar.strftime(date, "%b %-d, %Y")
end
def number_format(number) when is_integer(number) do
Cldr.Number.to_string!(number)
end
def number_format(other), do: other
end
|