| 9 | 9 | * @return {string} - XXXXXXXXXX |
| 10 | 10 | */ |
| 11 | 11 | export const clean = (number) => { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 12 | // Remove all non-digit characters |
| 13 | let cleaned = number.replace(/\D/g, ''); |
| 14 | |
| 15 | // Edge Case: Phone number contains letters |
| 16 | if (/[a-zA-Z]/.test(number)) { |
| 17 | throw new Error('Letters not permitted'); |
| 18 | } |
| 19 | |
| 20 | // Edge Case: Phone number contains invalid punctuation |
| 21 | if (/[!@#$%^&*;:,?]/.test(number)) { |
| 22 | throw new Error('Punctuations not permitted'); |
| 23 | } |
| 24 | |
| 25 | // Edge Case: Phone number has more than 11 digits |
| 26 | if (cleaned.length > 11) { |
| 27 | throw new Error('Must not be greater than 11 digits'); |
| 28 | } |
| 29 | |
| 30 | // Edge Case: Phone number has fewer than 10 digits |
| 31 | if (cleaned.length < 10) { |
| 32 | throw new Error('Must not be fewer than 10 digits'); |
| 33 | } |
| 34 | |
| 35 | // Edge Case: Phone number has 11 digits but doesn't start with 1 |
| 36 | if (cleaned.length === 11 && cleaned[0] !== '1') { |
| 37 | throw new Error('11 digits must start with 1'); |
| 38 | } |
| 39 | |
| 40 | // If 11 digits and starts with 1, remove the country code |
| 41 | if (cleaned.length === 11) { |
| 42 | cleaned = cleaned.substring(1); |
| 43 | } |
| 44 | |
| 45 | // Edge Case: Area code starts with 0 |
| 46 | if (cleaned[0] === '0') { |
| 47 | throw new Error('Area code cannot start with zero'); |
| 48 | } |
| 49 | |
| 50 | // Edge Case: Area code starts with 1 |
| 51 | if (cleaned[0] === '1') { |
| 52 | throw new Error('Area code cannot start with one'); |
| 53 | } |
| 54 | |
| 55 | // Edge Case: Exchange code starts with 0 |
| 56 | if (cleaned[3] === '0') { |
| 57 | throw new Error('Exchange code cannot start with zero'); |
| 58 | } |
| 59 | |
| 60 | // Edge Case: Exchange code starts with 1 |
| 61 | if (cleaned[3] === '1') { |
| 62 | throw new Error('Exchange code cannot start with one'); |
| 63 | } |
| 64 | |
| 65 | return cleaned; |
| 13 | 66 | }; |
| 67 | |
| 68 | // Handled Edge Cases: |
| 69 | // 1. Phone number contains letters |
| 70 | // 2. Phone number contains invalid punctuation |
| 71 | // 3. Phone number has more than 11 digits |
| 72 | // 4. Phone number has fewer than 10 digits |
| 73 | // 5. Phone number has 11 digits but doesn't start with 1 |
| 74 | // 6. Area code starts with 0 |
| 75 | // 7. Area code starts with 1 |
| 76 | // 8. Exchange code starts with 0 |
| 77 | // 9. Exchange code starts with 1 |