Created
June 27, 2018 12:28
-
-
Save Falci/800abebb780a98edbe58045f386aaeed to your computer and use it in GitHub Desktop.
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
function hasAlphaChars(original, cleaned) { | |
return !(original.match(/[a-z]/gi)) ? cleaned : null; | |
} | |
function hasIncorrectLength(original, cleaned) { | |
return (cleaned && cleaned.length == 10) ? cleaned : null; | |
} | |
function clear(original) { | |
let cleaned = original.replace(/[^0-9]/g, ''); | |
if (cleaned.length === 11 && cleaned[0] === '1') { | |
cleaned = cleaned.substring(1) | |
} | |
return cleaned; | |
} | |
class PhoneNumber { | |
constructor(num, listOfRules = [clear, hasAlphaChars, hasIncorrectLength]){ | |
this.listOfRules = listOfRules; | |
this.num = num; | |
} | |
//do not remove | |
number() { | |
return this.validate(); | |
} | |
validate() { | |
let obj = this.listOfRules.reduce((prev, next) => { | |
const { original, cleaned } = prev; | |
const newCleaned = next(original, cleaned); | |
return { original, cleaned: newCleaned } | |
}, { original: this.num }); | |
return obj.cleaned; | |
} | |
} | |
export default PhoneNumber; |
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
import PhoneNumber from './phone-number'; | |
describe('PhoneNumber()', () => { | |
test('cleans the number', () => { | |
const phone = new PhoneNumber('(123) 456-7890'); | |
expect(phone.number()).toEqual('1234567890'); | |
}); | |
test('cleans numbers with dots', () => { | |
const phone = new PhoneNumber('123.456.7890'); | |
expect(phone.number()).toEqual('1234567890'); | |
}); | |
test('cleans numbers with multiple spaces', () => { | |
const phone = new PhoneNumber('123 456 7890 '); | |
expect(phone.number()).toEqual('1234567890'); | |
}); | |
test('invalid when 9 digits', () => { | |
const phone = new PhoneNumber('123456789'); | |
expect(phone.number()).toEqual(null); | |
}); | |
test('invalid when 11 digits', () => { | |
const phone = new PhoneNumber('21234567890'); | |
expect(phone.number()).toEqual(null); | |
}); | |
test('valid when 11 digits and starting with 1', () => { | |
const phone = new PhoneNumber('11234567890'); | |
expect(phone.number()).toEqual('1234567890'); | |
}); | |
test('invalid when 12 digits', () => { | |
const phone = new PhoneNumber('321234567890'); | |
expect(phone.number()).toEqual(null); | |
}); | |
test('invalid with letters', () => { | |
const phone = new PhoneNumber('123-abc-7890'); | |
expect(phone.number()).toEqual(null); | |
}); | |
test('invalid with punctuations', () => { | |
const phone = new PhoneNumber('123-@:!-7890'); | |
expect(phone.number()).toEqual(null); | |
}); | |
test('invalid with right number of digits but letters mixed in', () => { | |
const phone = new PhoneNumber('1a2b3c4d5e6f7g8h9i0j'); | |
expect(phone.number()).toEqual(null); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment