-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (37 loc) · 1.01 KB
/
index.js
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
(function() {
'use strict';
var Phone = function(rawNumber) {
this.cleaned = cleanNumber(rawNumber);
};
Phone.prototype.number = function(){
return this.cleaned;
};
Phone.prototype.areaCode = function() {
return this.cleaned.slice(0, 3);
};
Phone.prototype.numPrefix = function() {
return this.cleaned.slice(3, 6);
};
Phone.prototype.numSuffix = function() {
return this.cleaned.slice(6);
};
Phone.prototype.toString = function() {
return "(" + this.areaCode() + ") " +
this.numPrefix() + "-" +
this.numSuffix();
};
function lengthCorrectedNumber( cleanedNumber ) {
if (cleanedNumber.length === 10) {
return cleanedNumber;
} else if ( cleanedNumber[0] === '1' && cleanedNumber.length === 11 ) {
return cleanedNumber.slice(1);
} else {
return "0000000000";
}
};
function cleanNumber(rawNumber) {
var cleaned = rawNumber.replace(/\D+/g, "");
return lengthCorrectedNumber( cleaned );
};
module.exports = Phone;
})();