Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Faker.Phone.Ke module to generate fake numbers for Kenya #566

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions lib/faker/phone/ke.ex
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions test/faker/phone/ke_test.exs
Original file line number Diff line number Diff line change
@@ -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