import Enum
defmodule Acronym do
  @doc """
  Generate an acronym from a string.
  "This is a string" => "TIAS"
  """
  @spec abbreviate(String.t()) :: String.t()
  def abbreviate(string) do
      # Splits sentence into words
      tokenized_sentence = String.split(string, ~r{(\W)}, trim: true)
      # Splits words if there are caps in the middle of them
      letters = Enum.map(tokenized_sentence, (fn x -> grab_acronym(x) end))
      Enum.reduce(List.flatten(letters), "", (fn(x, acc) -> acc <> x end))
  end

  def grab_acronym(string) do
      cond do
          String.match?(string, ~r{[[:upper:]]}) == true ->
              List.flatten(String.split(string, ~r{[^[:upper:]]}, trim: true))
          true ->
              [String.upcase(String.first(string))]
      end
  end
end
