forked from corbanworks/fake-nino-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (121 loc) · 2.69 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
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
// Create a handy array function
function in_array( arr, obj ) {
if (typeof arr.includes === 'function') {
arr.includes(obj);
}
var len = arr.length;
for ( var x = 0 ; x <= len ; x++ ) {
if ( arr[x] == obj ) return true;
}
return false;
}
// Setup our groups
var group1 = '';
var group2 = '';
var group3 = '';
var group4 = '';
var group5 = '';
var randomArrayIndex = '';
// Some combinations are not allowed
var notAllowed = new Array(
'GB',
'BG',
'NK',
'KN',
'TN',
'NT',
'ZZ'
);
// Some letters can't be first and some can't be second
var firstAllowed = new Array(
'A',
'B',
'C',
'E',
'G',
'H',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'R',
'S',
'T',
'W',
'X',
'Y',
'Z'
);
var secondAllowed = new Array(
'A',
'B',
'C',
'E',
'G',
'H',
'J',
'K',
'L',
'M',
'N',
'P',
'R',
'S',
'T',
'W',
'X',
'Y',
'Z'
);
// Only certain characters can show up last
var lastAllowed = new Array(
'A',
'B',
'C',
'D',
''
);
exports.generate = function (style) {
// Set a default value for style and make sure it is valid
var style = (style == null || (style !== 1 && style !== 2)) ? 1 : style;
// Generate group1
while( group1 == '' || in_array(notAllowed, group1) ) {
// First letter
randomArrayIndex = Math.floor(Math.random() * firstAllowed.length);
group1 = firstAllowed[ randomArrayIndex ];
// Second letter
randomArrayIndex = Math.floor(Math.random() * secondAllowed.length);
group1 = group1 + secondAllowed[ randomArrayIndex ];
}
// Generate group2
group2 = ''+Math.floor(Math.random()*10)+Math.floor(Math.random()*10);
// Generate group3
group3 = ''+Math.floor(Math.random()*10)+Math.floor(Math.random()*10);
// Generate group4
group4 = ''+Math.floor(Math.random()*10)+Math.floor(Math.random()*10);
// Generate group5
randomArrayIndex = Math.floor(Math.random() * lastAllowed.length);
group5 = lastAllowed[ randomArrayIndex ];
if( style == 1 ){
var nino = group1+group2+group3+group4+group5;
} else {
var nino = group1+" "+group2+" "+group3+" "+group4;
if( group5 != '' ){
nino = nino + " " + group5;
}
}
return nino;
}
exports.validate = function (nino) {
var regex = /^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z] ?[0-9]{2} ?[0-9]{2} ?[0-9]{2} ?[ABCD]?/;
if (nino.match(regex)) {
regex = /^(GB)|^(BG)|^(NK)|^(KN)|^(TN)|^(NT)|^(ZZ)/;
if ( !nino.match(regex) ) {
return true;
}
}
return false;
}