-
Notifications
You must be signed in to change notification settings - Fork 0
/
noTelp.js
177 lines (157 loc) · 3.8 KB
/
noTelp.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const operators = [
{
"name": "kartuHalo",
"operator": "Telkomsel",
"code": [11]
},
{
"name": "simPATI",
"operator": "Telkomsel",
"code": [12,13,21]
},
{
"name": "LOOP",
"operator": "Telkomsel",
"code": [22]
},
{
"name": "KARTU As",
"operator": "Telkomsel",
"code": [21,23,52,53]
},
{
"name": "by.U / Kartu As",
"operator": "Telkomsel",
"code": [51]
},
{
"name": "IndosatM2",
"operator": "Indosat Ooredoo",
"code": [14]
},
{
"name": "Matrix",
"operator": "Indosat Ooredoo",
"code": [55]
},
{
"name": "Mentari",
"operator": "Indosat Ooredoo",
"code": [58]
},
{
"name": "Mentari/Matrix",
"operator": "Indosat Ooredoo",
"code": [15,16]
},
{
"name": "IM3",
"operator": "Indosat Ooredoo",
"code": [56,57]
},
{
"name": "XL",
"operator": "XL Axiata",
"code": [17,18,19,59,77,78,79]
},
{
"name": "Axis",
"operator": "XL Axiata",
"code": [31,32,33,38]
},
{
"name": "3",
"operator": "3",
"code": [95,96,97,98,99],
"validationConfig": {
"maxLength" : 13
}
},
{
"name": "Smartfren",
"operator": "Smartfren",
"code": [81,82,83,84,85,86,87,88,89]
},
{
"name": "Net1",
"operator": "Net1",
"code": [27, 28]
},
{
"name": "ByRU",
"operator": "ByRU",
"code": [68]
},
];
const ValidationMessage = {
"VALID" : "VALID",
"INVALID": "INVALID",
"BELOW_MIN" : "BELOW MINIMUM LENGTH",
"ABOVE_MAX" : "ABOVE MAXIMUM LENGTH",
"NOT_FOUND" : "NOT FOUND"
}
const validationConfig = {
"minLength": 10,
"maxLength": 12
}
const numericOnly = (value) => {
return value.replace(/\D/g,'');
}
const getOperator = (no, validate = false) => {
let code;
let result = {
valid: false,
message: ValidationMessage.INVALID
}
// Null Validation
if(!no){
return result;
}
// + prepend
if(no.substr(0,1) === '+'){
no = no.substr(1);
}
// country code
if(no.substr(0,2) === '62'){
no = '0'+no.substr(2);
}
// convert to numeric only
no = numericOnly(no);
// get code
if(no.substr(0,2) === '08'){
code = no.substr(2,2);
}else {
return result;
}
const found = operators.some(data => {
if(data.code.some (item => item == code)){
result.operator = data.operator;
result.card = data.name;
result.message = ValidationMessage.VALID;
result.valid = true;
if(validate){
// get validationConfig from data
if(data.validationConfig && data.validationConfig.minLength){
validationConfig.minLength = data.validationConfig.minLength;
}
if(data.validationConfig && data.validationConfig.maxLength){
validationConfig.maxLength = data.validationConfig.maxLength;
}
// check validation
if(no.length < validationConfig.minLength){
result.valid = false;
result.message = ValidationMessage.BELOW_MIN;
}else if(no.length > validationConfig.maxLength){
result.valid = false;
result.message = ValidationMessage.ABOVE_MAX;
}
}
return true;
}
});
if(!found){
result.valid = false;
result.message = ValidationMessage.NOT_FOUND;
}
return result;
}