diff --git a/lib/faker/phone/ke.ex b/lib/faker/phone/ke.ex new file mode 100644 index 000000000..5e93af1f3 --- /dev/null +++ b/lib/faker/phone/ke.ex @@ -0,0 +1,104 @@ +defmodule Faker.Phone.Ke do + import Faker, only: [samplerp: 2] + + @moduledoc """ + Functions for generating phone related data for Kenya + """ + + @doc """ + Returns a random phone number. + + ## Examples + + iex> Faker.Phone.Ke.number() + "+254115628209" + iex> Faker.Phone.Ke.number() + "+254162562860" + iex> Faker.Phone.Ke.number() + "+254709999999" + iex> Faker.Phone.Ke.number() + "+257362091134" + """ + + @spec number() :: String.t() + def number do + if Faker.random_between(0, 1) == 0 do + landline_number() + else + cell_number() + end + end + + samplerp(:landline_number_format, [ + "#######", + "#######" + ]) + + @doc """ + Returns a 2 digit random area code + + ## Examples + + iex> Faker.Phone.Ke.area_code() + "20" + iex> Faker.Phone.Ke.area_code() + "42" + iex> Faker.Phone.Ke.area_code() + "52" + iex> Faker.Phone.Ke.area_code() + "34" + """ + @spec area_code() :: String.t() + def area_code do + [digit(0, 9), digit(0, 9)] |> Enum.join() + end + + @doc """ + Returns a random landline phone number. + + ## Examples + + iex> Faker.Phone.Ke.landline_number() + "+254362091134" + iex> Faker.Phone.Ke.landline_number() + "+254451151075" + iex> Faker.Phone.Ke.landline_number() + "+254567096040" + iex> Faker.Phone.Ke.landline_number() + "+254317703793" + """ + + @spec landline_number() :: String.t() + def landline_number do + "+254" <> area_code() <> Faker.format(landline_number_format()) + end + + samplerp(:cell_number_format, [ + "########", + "########", + "########", + "########" + ]) + + @doc """ + Returns a random cell phone number + + ## Examples + + iex> Faker.Phone.Ke.cell_number() + "919999999" + iex> Faker.Phone.Ke.cell_number() + "929999999" + iex> Faker.Phone.Ke.cell_number() + "939999999" + iex> Faker.Phone.Ke.cell_number() + "969999999" + """ + + @spec cell_number() :: String.t() + def cell_number do + "+254" <> Enum.random(["7","1"]) <> Faker.format(cell_number_format()) + end + + defp digit(min, max), do: Faker.random_between(min, max) +end diff --git a/test/faker/phone/ke_test.exs b/test/faker/phone/ke_test.exs new file mode 100644 index 000000000..c7513cbcf --- /dev/null +++ b/test/faker/phone/ke_test.exs @@ -0,0 +1,41 @@ +defmodule Faker.Phone.KeTest do + use ExUnit.Case, async: true + + import Faker.Phone.Ke + + @iterations 10_000 + + test "test area_code/0. Test if area code is a valid length" do + Stream.repeatedly(&area_code/0) + |> Enum.take(@iterations) + |> Enum.each(fn generated_value -> + assert String.length(generated_value) == 2 + end) + end + + + test "cell_number/0. Test if cell number is a valid length" do + Stream.repeatedly(&cell_number/0) + |> Enum.take(@iterations) + |> Enum.each(fn generated_value -> + assert String.length(generated_value) == 13 + end) + end + + test "cell_number/0 return starts with +2541 or +2547 followed by 8 digits" do + Stream.repeatedly(&cell_number/0) + |> Enum.take(@iterations) + |> Enum.each(fn generated_value -> + assert String.match?(generated_value, ~r/^\+254(1|7)[0-9]{8}$/) + end) + end + + + test "landline_number/0 return starts with +254 or +254 followed by 9 digits" do + Stream.repeatedly(&landline_number/0) + |> Enum.take(@iterations) + |> Enum.each(fn generated_value -> + assert String.match?(generated_value, ~r/^\+254[0-9]{9}$/) + end) + end +end