forked from exercism/wren
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproof.ci.wren
47 lines (43 loc) · 1.31 KB
/
proof.ci.wren
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class PhoneNumber {
static clean(input) {
// remove valid non-digits
var ok = ["+", "(", ")", " ", "-", "."]
var cleaned = ok.reduce(input) {|s, char| s.replace(char, "")}
// check for invalid characters
// anything other than digits and lettes is "punctuation"
var cps = cleaned.codePoints
if (cps.any {|cp| (65 <= cp && cp <= 90) || (97 <= cp && cp <= 122)}) {
Fiber.abort("letters not permitted")
}
if (cps.any {|cp| cp < 48 || cp > 57}) {
Fiber.abort("punctuations not permitted")
}
// validate length
if (cleaned.count < 10) {
Fiber.abort("must not be fewer than 10 digits")
}
if (cleaned.count > 11) {
Fiber.abort("must not be greater than 11 digits")
}
if (cleaned.count == 11) {
if (cleaned[0] != "1") {
Fiber.abort("11 digits must start with 1")
}
cleaned = cleaned[1..-1]
}
// validate area code and exchange code
if (cleaned[0] == "0") {
Fiber.abort("area code cannot start with zero")
}
if (cleaned[0] == "1") {
Fiber.abort("area code cannot start with one")
}
if (cleaned[3] == "0") {
Fiber.abort("exchange code cannot start with zero")
}
if (cleaned[3] == "1") {
Fiber.abort("exchange code cannot start with one")
}
return cleaned
}
}