-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
109 lines (90 loc) · 2.68 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
'use strict';
// Prime Number used to generate the control number.
var MOD = 97;
// 10 digits is the length required before the control number
var LENGTH = 10;
function padWithLeadingZeros(numbers){
var diffLength = LENGTH - numbers.length;
if(diffLength === 0){
return numbers;
}
// in ES6: new Array(len).fill(0);
var padding = new Array(diffLength + 1).join('0');
return padding + numbers;
}
/**
* Calculates the control number based on the given numbers
*
* control = numbers % 97, unless numbers % 97 === 0, then
* 97 is used as control.
*
* When the control is a single digit, a leading zero is added.
*
* @param {String|Number} numbers number to calculate control for.
* @return {String} The control number.
*/
function calculateCheckDigits(numbers){
numbers = parseInt(numbers, 10);
var control = numbers % MOD;
control = control === 0 ? 97 : control;
control = control < 10 ? "0" + control : control.toString();
return control;
}
/**
* Checks whether the given OGM is valid. Returns true if valid,
* false otherwise.
*
* @param {String} ogm The ogm to check
* @return {Boolean} truth value of validity.
*/
function isValidOGM(structuredMessage){
if(!/\+{3}[0-9]{3}\/[0-9]{4}\/[0-9]{5}\+{3}/.test(structuredMessage)){
return false;
}
var numbers = structuredMessage.match(/\d+/g).join('');
var firstTen = numbers.substring(0, 10);
var control = numbers.substring(10, 12);
var result = calculateCheckDigits(firstTen);
return result === control;
}
/**
* Generates a valig ogm based on the given numbers.
* If the type of numbers is a Number, DO NOT ADD LEADING ZEROES,
* OTHERWISE THE RESULT IS INTERPRETED AS OCTAL.
*
* @param {String| Number} Numbers to generate ogm from.
* @return {String} Structured message
*/
function generateOGM(numbers){
var control = calculateCheckDigits(numbers);
numbers = numbers.toString();
numbers = padWithLeadingZeros(numbers);
// built the string representation of the ogm.
return '+++' + numbers.substring(0, 3)
+ '/' + numbers.substring(3,7)
+ '/' + numbers.substring(7,10)
+ control + '+++';
}
/**
* Generates a random OGM.
* @return {String} The final OGM.
*/
function generateRandomOGM(){
// possible because Math.random does not include 1.
var numbers = Math.floor(Math.random() * 10000000000);
return generateOGM(numbers);
}
/**
* Returns a stripped version of the OGM. Removes the + and /'s.
* only digits remain.
*/
function getStrippedOGM(ogm) {
return ogm.match(/\d+/g).join('');
}
module.exports = {
isValidOGM: isValidOGM,
generateOGM: generateOGM,
calculateCheckDigits: calculateCheckDigits,
generateRandomOGM: generateRandomOGM,
getStrippedOGM: getStrippedOGM
}