-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
186 lines (153 loc) · 5.26 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
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
174
175
176
177
178
179
180
181
182
183
184
185
const validChars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'P', 'Q', 'R', 'S'];
const validChars32 = [...validChars, 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const VALID_SELCAL_REGEX = /^[A-HJ-NP-S]{2}-[A-HJ-NP-S]{2}$/;
const VALID_SELCAL_32_REGEX = /^[A-HJ-NP-Z1-9]{2}-[A-HJ-NP-Z1-9]{2}$/;
function getValidCharPair(excludeChars = [], selcal32 = false) {
const localArray = (selcal32 ? validChars32 : validChars).filter(char => !excludeChars.includes(char));
const workingArray = [];
for (let i = 0; i < 2; i++) {
const randomIndex = Math.floor(Math.random() * localArray.length);
const element = localArray[randomIndex];
localArray.splice(randomIndex, 1);
workingArray.push(element);
}
return workingArray.sort();
}
export function getSingleSelcal(selcal32 = false) {
if (typeof selcal32 !== 'boolean') {
throw new Error('Invalid argument: selcal32 must be a boolean');
}
const firstPair = getValidCharPair([], selcal32);
let secondPair;
// If selcal32 is true and the first pair doesn't contain a SELCAL32 character,
// generate the second pair with the condition that it must contain a SELCAL32 character
if (selcal32 && !firstPair.some(char => validChars32.includes(char) && !validChars.includes(char))) {
do {
secondPair = getValidCharPair(firstPair, true);
} while (!secondPair.some(char => validChars32.includes(char) && !validChars.includes(char)));
} else {
secondPair = getValidCharPair(firstPair, selcal32);
}
return `${firstPair.join('')}-${secondPair.join('')}`;
}
export function generateBatch(numCodes, isSelcal32 = false) {
if (typeof numCodes !== 'number') {
throw new TypeError('numCodes must be a number');
}
if (typeof isSelcal32 !== 'boolean') {
throw new TypeError('isSelcal32 must be a boolean');
}
const codes = [];
for (let i = 0; i < numCodes; i++) {
codes.push(getSingleSelcal(isSelcal32));
}
return codes;
}
/* SELCAL Check */
/**
* Returns an object indicating that the code is invalid.
* @param {string} code - The SELCAL code that is being checked
* @param {string} note - The reason the code is invalid
* @returns {Object} An object with properties for the SELCAL code, its validity and a note.
*/
function setInvalid(code, note) {
return {
code,
isValid: false,
note
};
}
/**
* Checks if the character pairs in the code are valid.
* @param {string} code - The SELCAL code that is being checked
* @returns {Object} - An object indicating if the character pairs are valid, and if not, the reason.
*/
function checkValidCharPairs(code) {
const pairsArray = code.split('-');
for (let pair of pairsArray) {
const pairArray = pair.split('');
const letters = pairArray.filter(char => isNaN(char));
const numbers = pairArray.filter(char => !isNaN(char));
const letterIndexes = letters.map(char => validChars32.indexOf(char));
const numberIndexes = numbers.map(char => validChars32.indexOf(char));
if (letters.length > 1 && letterIndexes[1] <= letterIndexes[0]) {
return {
isValid: false,
reason: 'Letters not sequential'
};
}
if (numbers.length > 1 && numberIndexes[1] <= numberIndexes[0]) {
return {
isValid: false,
reason: 'Numbers not sequential'
};
}
}
return {
isValid: true
};
}
/**
* Checks if the code contains repeated characters.
* @param {string} code - The SELCAL code that is being checked
* @returns {Object} - An object indicating whether or not the code contains repeated characters
*/
function checkRepeatedCharacters(code) {
const charArray = code.replace('-', '').split('');
const charSet = new Set(charArray);
return {
isValid: charSet.size === charArray.length
};
}
/**
* Checks if a SELCAL code is valid.
* @param {string} code - The SELCAL code to check
* @returns {Object} - An object with properties for the code, its validity, the type of SELCAL code, and a note if the code is invalid.
*/
export function isValidSelcalCode(code) {
if (typeof code !== 'string') {
return setInvalid(code, 'code is not a string');
}
const validationResult = {
code,
isValid: null,
selcalCodeType: null,
note: null
}
if (VALID_SELCAL_REGEX.test(code)) {
validationResult.selcalCodeType = 'selcal';
} else if (VALID_SELCAL_32_REGEX.test(code)) {
validationResult.selcalCodeType = 'selcal32';
} else {
return setInvalid(code, 'Invalid Character');
}
const repeatedCharactersCheck = checkRepeatedCharacters(code);
if (!repeatedCharactersCheck.isValid) {
return setInvalid(code, 'Characters are repeated');
}
const validCharPairsCheck = checkValidCharPairs(code);
if (!validCharPairsCheck.isValid) {
return {
...validationResult,
isValid: false,
note: validCharPairsCheck.reason
};
}
return {
...validationResult,
isValid: true
};
}
// console.log(isValidSelcalCode(6));
// console.log(isValidSelcalCode('AB-CC'));
// console.log(isValidSelcalCode('AB-CD'));
// console.log(isValidSelcalCode('A9-BC'));
// console.log(isValidSelcalCode('AI-BT'));
// console.log(isValidSelcalCode('AB-DC'));
// console.log(isValidSelcalCode('12-AB'));
// console.log(isValidSelcalCode('21-AB'));
//
//
//
//
//