-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelephoneNumberValidator.js
136 lines (110 loc) · 3.29 KB
/
telephoneNumberValidator.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"use strict";
/*
Used a stack instead of JS own iterable types or methods, as a standard from other strongly typed languages.
*/
class Stack {
constructor(array) {
this.stackArray = new Array(...array);
}
lookup = (index) => this.stackArray[index];
pop = () => (this.size() > 0 ? this.stackArray.pop() : null);
push = () => this.stackArray.push();
print = () => console.log(this.stackArray.toString());
size = () => this.stackArray.length;
isEmpty = () => this.stackArray.length == 0;
}
// Enumerated types and Constants
const MAX_DIGITS = 11;
const MIN_DIGITS = 10;
const COUNTRY_CODE = "1";
const nonNumberCharactersRegex = new RegExp("[^a-z0-9()-s]");
const numericCharactersRegex = new RegExp("^[0-9]$");
const removeWhiteSpaces = new RegExp("\\s", "g");
const onlyNumericsRegex = new RegExp("[^a-z0-9]", "g");
const SAPERATOR_TYPE = {
OPEN_PAREN: "(",
CLOSE_PAREN: ")",
HYPHEN: "-",
WHITE_SPACE: " ",
};
const SAPERATORS = Object.values(SAPERATOR_TYPE);
// Helpers
function validCountryCode(num, stack) {
let cleanedNum = num.replace(onlyNumericsRegex, "");
let code = stack.pop();
if (SAPERATORS.includes(code)) return false;
if (cleanedNum.length == MAX_DIGITS) return code === COUNTRY_CODE ? true : false;
if (cleanedNum.length == MIN_DIGITS) return true;
if (cleanedNum.length > MAX_DIGITS || cleanedNum.length < MIN_DIGITS) return false;
return false;
}
function nonAplhaNumericCheck(str) {
return nonNumberCharactersRegex.test(str) ? true : false;
}
function patternMatcher(stack, patternLength) {
let saperator = "";
let saperatorTwo = "";
let saperatorStart = false;
let saperatorEnd = false;
let poppedChar = "";
let length = stack.size();
if (SAPERATORS.includes(stack.lookup(length - 1))) {
saperatorStart = true;
saperator = stack.pop();
}
for (let i = length; i > length - patternLength; i--) {
poppedChar = stack.pop();
if (!numericCharactersRegex.test(poppedChar)) {
return false;
}
}
if (saperatorStart && SAPERATORS.includes(stack.lookup(stack.size() - 1))) {
if (stack.lookup(stack.size() - 1) !== saperator) {
if (
saperator == SAPERATOR_TYPE.CLOSE_PAREN &&
stack.lookup(stack.size() - 1) == SAPERATOR_TYPE.OPEN_PAREN
) {
saperatorEnd = true;
saperatorTwo = stack.pop();
return true;
}
saperatorStart = false;
saperatorEnd = false;
saperator = "";
} else {
saperatorEnd = true;
saperatorTwo = stack.pop();
}
}
if (saperator !== saperatorTwo) return false;
return true;
}
// Main
function telephoneCheck(str) {
str = str.replace(removeWhiteSpaces, "");
let firstPattern = false;
let secondPattern = false;
let thirdPattern = false;
let stack = new Stack(str);
// First Pattern from the end
firstPattern = patternMatcher(stack, 4);
stack.print();
secondPattern = patternMatcher(stack, 3);
stack.print();
thirdPattern = patternMatcher(stack, 3);
stack.print();
if (stack.size() > 0) {
if (
validCountryCode(str, stack) &&
stack.isEmpty() &&
firstPattern &&
secondPattern &&
thirdPattern
) {
return true;
} else return false;
}
return firstPattern && secondPattern && thirdPattern;
}
let result = telephoneCheck("(555-555-5555");
console.log(result);