forked from liamja/modcheck.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modcheck.js
108 lines (84 loc) · 2.82 KB
/
modcheck.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
let weightTable = require('./valacdos').table;
export default class Modcheck {
constructor(accountNumber, sortCode) {
this.accountNumber = accountNumber;
this.sortCode = sortCode;
this.weightTable = weightTable;
this.currentCheck = null;
if (typeof this.sortCode === 'string' || this.sortCode instanceof String) {
this.sortCode = this.sortCode.replace(/-/g, '');
}
}
check() {
if (parseInt(this.accountNumber) === 0 || parseInt(this.sortCode) === 0
) {
return false;
}
let results = [];
let checks = this.weightTable.filter(function (check) {
let sortCode = parseInt(this.sortCode)
, start = parseInt(check.sortCodeRange.start)
, end = parseInt(check.sortCodeRange.end);
return (sortCode >= start && sortCode <= end);
}, this);
checks.forEach(function (check) {
this.currentCheck = check;
if (check.exception == 8) {
this.sortCode = '090126';
}
this.weight = Object.keys(check.weight).reduce(function (prev, cur) {
return prev + check.weight[cur].toString();
}, '');
switch (check.algorithm) {
case 'DBLAL':
if (this.currentCheck.exception == 3 && (String(this.accountNumber).charAt(2) != 6 && String(this.accountNumber).charAt(2) != 9)) {
results.push(this.dblAlCheck());
}
break;
case 'MOD10':
results.push(this.mod10Check());
break;
case 'MOD11':
results.push(this.mod11Check());
break;
}
}, this);
// One fail causes entire check to fail
let passed = results.reduce(function (prev, cur) {
return cur !== true ? false : prev;
}, true);
return passed;
}
dblAlCheck() {
let account = this.sortCode + this.accountNumber;
let weightedAccount = [];
for (let i = 0; i < 14; i++) {
weightedAccount[i] = parseInt(account[i]) * parseInt(this.weight[i]);
}
weightedAccount = weightedAccount.join('').split('');
let sum = weightedAccount.reduce((a, b) => parseInt(a) + parseInt(b));
if (this.currentCheck.exception == 1)
{
sum += 27;
}
return sum % 10 === 0;
}
mod10Check() {
let account = this.sortCode + this.accountNumber;
let weightedAccount = [];
for (let i = 0; i < 14; i++) {
weightedAccount[i] = parseInt(account[i]) * parseInt(this.weight[i]);
}
let sum = weightedAccount.reduce((a, b) => parseInt(a) + parseInt(b));
return sum % 10 === 0;
}
mod11Check() {
let account = this.sortCode + this.accountNumber;
let weightedAccount = [];
for (let i = 0; i < 14; i++) {
weightedAccount[i] = parseInt(account[i]) * parseInt(this.weight[i]);
}
let sum = weightedAccount.reduce((a, b) => parseInt(a) + parseInt(b));
return sum % 11 === 0;
}
}