summary refs log tree commit diff stats
path: root/raku/phone-number
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-08-25 20:06:50 +0530
committerAndinus <andinus@nand.sh>2021-08-25 20:06:50 +0530
commitb5c455ddef1b9f3c112be5db50c0a63d588a67cf (patch)
treee51950fd8180f581683adcb0c31202a39a84189f /raku/phone-number
parentc1cec7e46fead306a5576b2731a00d0dd86ae6e0 (diff)
downloadexercism-b5c455ddef1b9f3c112be5db50c0a63d588a67cf.tar.gz
Raku: Solution for phone-number
Diffstat (limited to 'raku/phone-number')
-rw-r--r--raku/phone-number/Phone.rakumod36
1 files changed, 35 insertions, 1 deletions
diff --git a/raku/phone-number/Phone.rakumod b/raku/phone-number/Phone.rakumod
index f2c828c..ef214b1 100644
--- a/raku/phone-number/Phone.rakumod
+++ b/raku/phone-number/Phone.rakumod
@@ -12,5 +12,39 @@ constant @errors = (
    'exchange code cannot start with one',
 );
 
-sub clean-number ($number) is export {
+sub clean-number(Str $number --> Str) is export {
+    die @errors[3] if $number.contains: /<alpha>/;
+
+    # <punct> matches "(";
+    die @errors[4] if $number.contains: /<[!:]>/;
+
+    given $number.comb.grep(/\d/)>>.Int {
+        when .elems == 11 {
+            die @errors[0] if .[0] !== 1;
+
+            # Area code.
+            die @errors[5] if .[1] == 0;
+            die @errors[6] if .[1] == 1;
+
+            # Exchange code.
+            die @errors[7] if .[4] == 0;
+            die @errors[8] if .[4] == 1;
+
+            return .skip.join;
+        }
+        when .elems == 10 {
+            # Area code.
+            die @errors[5] if .[0] == 0;
+            die @errors[6] if .[0] == 1;
+
+            # Exchange code.
+            die @errors[7] if .[3] == 0;
+            die @errors[8] if .[3] == 1;
+
+            return .join;
+        }
+        when .elems > 11 { die @errors[1] }
+        when .elems < 10 { die @errors[2] }
+        default { return .join }
+    }
 }