Sha256: f9bee4716f03a7d0d82b324df3f74c641c0591e314d3d67ab3c591377056fae4
Contents?: true
Size: 1.09 KB
Versions: 67
Compression:
Stored size: 1.09 KB
Contents
defmodule Phone do @moduledoc """ Utilities to work with phone numbers. """ @bad_result "0000000000" @doc """ Clean up a phone number. Returns 0000000000 if the phone number is bad. """ @spec number(String.t) :: String.t def number(raw) do raw |> to_parts |> to_string end @spec to_parts(String.t) :: [String.t] defp to_parts(raw) do Regex.run(~r/^\D*?1?\D*?(\d{3})\D*(\d{3})\D*(\d{4})$/, raw, [capture: :all_but_first]) || ["000", "000", "0000"] end @doc """ Get the area code of a phone number. The area code is the first three digits of a cleaned up phone number. """ @spec area_code(String.t) :: String.t def area_code(str) do str |> to_parts |> List.first end @spec prefix(String.t) :: String.t def prefix(str) do str |> to_parts |> Enum.at(1) end @spec line(String.t) :: String.t def line(str) do str |> to_parts |> List.last end @doc """ Pretty print a phone number. """ @spec pretty(String.t) :: String.t def pretty(str) do "(#{area_code(str)}) #{prefix(str)}-#{line(str)}" end end
Version data entries
67 entries across 67 versions & 1 rubygems