Dial code normalizer
EasyOnline Assessment
Clean up phone numbers typed into a sign-up form by converting each one to international E.164 form (+ followed by the country calling code and the national number), or saying why it cannot be converted.
Three countries are supported:
| country | calling code | national number | trunk prefix |
|---|---|---|---|
US | 1 | exactly 10 digits, the first digit 2 to 9 | 1 |
GB | 44 | exactly 10 digits | 0 |
FR | 33 | exactly 9 digits | 0 |
Process each entry in this order:
- Remove every space,
-,.,(and). What remains must be digits, optionally preceded by one+. Any other character (a letter, a+anywhere but the first position, ...) givesreject:bad-chars. - International form: the entry starts with
+, or with00(which acts like+). The digits after that marker must begin with one of the three calling codes, otherwisereject:unknown-country(this includes nothing following the marker). The digits after the calling code are the national number; for GB and FR a single0at its front is a trunk prefix typed by mistake, as in+44 (0) 20 ..., and is dropped. For US nothing is dropped. - National form (anything else): the number belongs to the
default_country. For US, if there are exactly 11 digits and the first is1, that1is dropped. For GB and FR, a single leading0is dropped if present. - The national number must now have the exact length from the table, otherwise
reject:bad-length. Then, for US numbers only, its first digit must be 2 to 9, otherwisereject:bad-area.
A valid entry becomes "+", the calling code, and the national number. Only the first failing check is reported.
Input: default_country, one of "US", "GB", "FR"; and numbers, a list of strings.
Output: a list with one string per entry of numbers, in the same order: the E.164 number or the reject:... reason.
Example 1
- Input:
- ["US",["(415) 555-2671","1-415-555-2671","+1 415 555 2671","0044 (0)20 7946 0958","020 7946 0958"]]
- Output:
- ["+14155552671","+14155552671","+14155552671","+442079460958","reject:bad-length"]
- Explanation
- With US as the default, the first three entries are all
+14155552671: separators are removed, and the 11-digit1-415-...loses its trunk1. The0044 (0)20 ...entry is international (00acts like+), the calling code 44 is GB, and the stray trunk0after it is dropped, giving+442079460958.020 7946 0958is treated as a US national number: it has 11 digits but does not start with1, so nothing is dropped and the length is wrong.
Example 2
- Input:
- ["GB",["020 7946 0958","(020) 7946-0958","7946 0958","+33 1 23 45 67 89","+33 (0)1 23 45 67 89","07700 900123","+49 30 1234"]]
- Output:
- ["+442079460958","+442079460958","reject:bad-length","+33123456789","+33123456789","+447700900123","reject:unknown-country"]
- Explanation
- With GB as the default, the leading
0of020 7946 0958is the trunk prefix and is dropped:+442079460958.7946 0958has only 8 digits. The French numbers are international, and the(0)after+33is dropped, so both give+33123456789.07700 900123becomes+447700900123.+49 ...starts with no supported calling code.
Example 3
- Input:
- ["US",["212-555-0147 ext 9","(155) 555-0147","+1 (055) 555 0147","1 800 555 0199","555-0147","+",""]]
- Output:
- ["reject:bad-chars","reject:bad-area","reject:bad-area","+18005550199","reject:bad-length","reject:unknown-country","reject:bad-length"]
- Explanation
212-555-0147 ext 9contains letters, so it isbad-charsbefore any length question.(155) 555-0147is 10 digits and starts with 1, so there is no 11-digit trunk1to drop; its length is right but a US area digit must be 2 to 9.+1 (055) ...fails the same check.1 800 555 0199loses the trunk1.555-0147has 7 digits. A lone+has no calling code after it, and an empty entry has no digits at all.
Constraints
default_countryis"US","GB"or"FR"0 <= numbers.length <= 500- Each entry has 0 to 30 printable ASCII characters (it may be empty)