Last active
June 3, 2018 10:26
-
-
Save nevadajames/d84fb17acc3a0c6db8890c1bbf7f8b42 to your computer and use it in GitHub Desktop.
elixir lessons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule AreaCode do | |
| @moduledoc """ | |
| Returns map of provinces with corresponding area codes as a list | |
| """ | |
| def codes_by_province do | |
| %{ | |
| marbella: [822], badajoz: [ 824, 924], guipuzcoa: [843,943], | |
| navarra: [848, 948], granada: [858, 958], murcia: [868, 968], baleares: [871, 971], | |
| gerona: [872, 972], lerida: [ 873, 973], zaragoza: [876, 976], | |
| tarragona: [877, 977], pontevedra: [886, 986], madrid: [911, 912, 913, 914, 915, 916, 917, 918], | |
| segovia: [921], salamanca: [923], toledo: [925], ciudad_real: [926], caceres: [927], | |
| barcelona: [931, 932, 933, 934, 935, 936, 937, 938], la_rioja: [941], cantabria: [942], | |
| vizcaya: [944, 946], alava: [945], burgos: [947], guadalajara: [949], almeria: [950], | |
| malaga: [951, 952], jaen: [953], sevilla: [954, 955], cordoba: [957], huelva: [959], | |
| valencia: [960, 961, 962, 963], alicante: [965, 966], albacete: [967], cuenca: [969], | |
| huesca: [974], soria: [975], teruel: [978], palencia: [979], zamora: [980], lugo: [982], | |
| valladolid: [983], asturias: [984, 985], orense: [988] | |
| } | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule PhoneValidator do | |
| @moduledoc """ | |
| Module for validating Spanish Phone Numbers, and also | |
| returns information about phone number type and can generate | |
| random valid phone numbers, | |
| """ | |
| def digit_extension(n) do | |
| Enum.map(0..n, fn _i -> :rand.uniform(9) end) | |
| end | |
| def join_digits(list), do: Enum.join(list) | |
| def valid?(n), do: Regex.match?(~r/[6789]\d{8}/, n) | |
| def generate, do: 8 |> digit_extension() |> join_digits() | |
| def prefix(n), do: String.slice(n,0..2) | |
| def phone_type(n) do | |
| cond do | |
| Regex.match?(~r/^80[1-9]|^90[1-9]/, prefix n) -> | |
| "premium" | |
| Regex.match?(~r/^800|^900/, prefix n) -> | |
| "toll-free" | |
| Regex.match?(~r/^[67]/, prefix n) -> | |
| "mobile" | |
| true -> | |
| "landline" | |
| end | |
| end | |
| def find_province(n) do | |
| require AreaCode | |
| area_code = Enum.filter AreaCode.codes_by_province, fn {key, value} -> | |
| Enum.member?(value, String.to_integer(prefix n)) | |
| end | |
| area_code |> Keyword.keys | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment