-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
251 lines (224 loc) · 8.14 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* Unsupported and legacy banks:
* ?? - Pepper Pay
* 1 - Euro Trade
* 2 - Poalei Agudat Israel
* 6 - Adanim (merged with Mizrahi Tefahot)
* 7 - Bank Lepituah Ataasiya
* 8 - Poalim / Asfanot
* 19 - Bank Ahaklaut Leisrael
* 23 - HSBC
* 24 - Poalim
* 25 - BNP Paribas
* 26 - Yobank (merged with 31)
* 27 - Barclays Bank PLC
* 28 - Poalim
* 30 - Bank Lemishar
* 33 - Discount
* 37 - Of Jordan
* 38 - Commercial Bank of Palestine
* 39 - SBI State Bank of India
* 43 - Jordan National Bank PLC Aman
* 48 - Otsar Ahayal / Aoved Aleumi
* 49 - Arav Bank
* 50 - Bank Clearing Center
* 59 - Automatic Bank Services
* 65 - Hasah Kupot Hisahon Lehinuh
* 66 - Kahir Aman
* 67 - Arab Land
* 68 - Bank Dexia
* 71 - Commercial Jordan
* 73 - Arav Islamic
* 74 - British Bank of Middle East
* 76 - Palestine Investments
* 77 - Leumi Mashkantaot
* 82 - אל-קודס לפיתוח והשקעות
* 83 - Union Bank
* 84 - האוזינג
* 89 - Palestine
* 90 - Discount Mashkantaot
* 93 - ג'ורדן כווית
* 99 - Bank Israel
*/
'use strict';
module.exports = function(bankNumber, branchNumber, accountNumber) {
// Input validation:
if(bankNumber.constructor === String) {
bankNumber = Number(bankNumber);
}
if(branchNumber.constructor === String) {
branchNumber = Number(branchNumber);
}
if(accountNumber.constructor === String) {
accountNumber = Number(accountNumber);
}
if(!isNonNegativeInteger(bankNumber)) {
return false;
}
if(!isNonNegativeInteger(branchNumber)) {
return false;
}
if(!isNonNegativeInteger(accountNumber)) {
return false;
}
var SUPPORTED_BANKS = {
YAHAV: 4, // Government workers
POST: 9,
LEUMI: 10,
DISCOUNT: 11,
HAPOALIM: 12,
IGUD: 13,
OTSAR_AHAYAL: 14,
MERCANTILE: 17,
MIZRAHI_TEFAHOT: 20,
CITIBANK: 22,
BEINLEUMI: 31,
ARAVEI_ISRAELI: 34, // Merged with Leumi
MASAD: 46,
POALEI_AGUDAT_ISRAEL: 52 // merged with Beinleumi
}
if(bankNumber === SUPPORTED_BANKS.MIZRAHI_TEFAHOT) {
if(branchNumber > 400) {
branchNumber -= 400;
}
}
var accountNumberDigits = numberDigitsToArr(accountNumber, 9);
var branchNumberDigits = numberDigitsToArr(branchNumber, 3);
// Account number validation
var sum = 0;
var remainder = 0;
switch(bankNumber) {
case(SUPPORTED_BANKS.LEUMI):
case(SUPPORTED_BANKS.IGUD):
case(SUPPORTED_BANKS.ARAVEI_ISRAELI):
sum = scalarProduct(accountNumberDigits.slice(0, 8), [1, 10, 2, 3, 4, 5, 6, 7]);
sum += scalarProduct(branchNumberDigits.slice(0, 4), [8, 9, 10]);
remainder = sum % 100;
return arrIncludes([90, 72, 70, 60, 20], remainder);
case(SUPPORTED_BANKS.YAHAV):
case(SUPPORTED_BANKS.MIZRAHI_TEFAHOT):
case(SUPPORTED_BANKS.HAPOALIM):
sum = scalarProduct(accountNumberDigits.slice(0, 6), [1, 2, 3, 4, 5, 6]);
sum += scalarProduct(branchNumberDigits.slice(0, 4), [7, 8, 9]);
remainder = sum % 11;
switch (bankNumber) {
case (SUPPORTED_BANKS.YAHAV):
return arrIncludes([0, 2], remainder);
case (SUPPORTED_BANKS.MIZRAHI_TEFAHOT):
return arrIncludes([0, 2, 4], remainder);
case (SUPPORTED_BANKS.HAPOALIM):
return arrIncludes([0, 2, 4, 6], remainder);
}
return false;
case(SUPPORTED_BANKS.DISCOUNT):
case(SUPPORTED_BANKS.MERCANTILE):
case(SUPPORTED_BANKS.BEINLEUMI):
case(SUPPORTED_BANKS.POALEI_AGUDAT_ISRAEL):
sum = scalarProduct(accountNumberDigits.slice(0, 9), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
remainder = sum % 11;
switch (bankNumber) {
case(SUPPORTED_BANKS.DISCOUNT):
case(SUPPORTED_BANKS.MERCANTILE):
return arrIncludes([0, 2, 4], remainder);
case(SUPPORTED_BANKS.BEINLEUMI):
case(SUPPORTED_BANKS.POALEI_AGUDAT_ISRAEL):
if(arrIncludes([0, 6], remainder)) {
return true;
} else {
sum = scalarProduct(accountNumberDigits.slice(0, 6), [1, 2, 3, 4, 5, 6]);
remainder = sum % 11;
return arrIncludes([0, 6], remainder);
}
}
return false;
case(SUPPORTED_BANKS.POST):
sum = scalarProduct(accountNumberDigits.slice(0, 9), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
remainder = sum % 10;
return remainder === 0;
case(54): // Jerusalem
return true; // wtf?
case(SUPPORTED_BANKS.CITIBANK):
sum = scalarProduct(accountNumberDigits.slice(1, 9), [2, 3, 4, 5, 6, 7, 2, 3]);
return (11 - sum % 11) === accountNumberDigits[0];
case(SUPPORTED_BANKS.OTSAR_AHAYAL):
case(SUPPORTED_BANKS.MASAD):
sum = scalarProduct(accountNumberDigits.slice(0, 6), [1, 2, 3, 4, 5, 6]);
sum += scalarProduct(branchNumberDigits.slice(0, 4), [7, 8, 9]);
remainder = sum % 11;
if(remainder === 0) {
return true;
}
if(bankNumber === SUPPORTED_BANKS.MASAD) {
if (remainder === 2 && arrIncludes([154, 166, 178, 181, 183, 191, 192, 503, 505, 507, 515, 516, 527, 539], branchNumber)) {
return true;
}
sum = scalarProduct(accountNumberDigits.slice(0, 9), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
remainder = sum % 11;
if(remainder === 0) {
return true;
} else {
sum = scalarProduct(accountNumberDigits.slice(0, 6), [1, 2, 3, 4, 5, 6]);
remainder = sum % 11;
return remainder === 0;
}
}
if(bankNumber === SUPPORTED_BANKS.OTSAR_AHAYAL) {
if (arrIncludes([0, 2], remainder) && arrIncludes([385, 384, 365, 347, 363, 362, 361], branchNumber)) {
return true;
} else if(remainder === 4 && arrIncludes([363, 362, 361], branchNumber)) {
return true;
} else {
sum = scalarProduct(accountNumberDigits.slice(0, 9), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
remainder = sum % 11;
if(remainder === 0) {
return true;
} else {
sum = scalarProduct(accountNumberDigits.slice(0, 6), [1, 2, 3, 4, 5, 6]);
remainder = sum % 11;
return remainder === 0;
}
}
}
return false;
}
return false;
/**
* Calculates scalar product of two arrays of the same length.
* https://en.wikipedia.org/wiki/Dot_product
* @param {Array} arr1
* @param {Array} arr2
*/
function scalarProduct(arr1, arr2) {
var product = 0;
for (var i = 0; i < arr1.length && i < arr2.length; ++i) {
product += arr1[i] * arr2[i];
}
return product;
}
/**
* Check if `val` is an element of `arr` using strict compare by reference
* A bit like arr.includes(val), but made in-house for legacy browser support
* @param {Array} arr
* @param {*} val
*/
function arrIncludes(arr, val) {
if (arr) {
for (var i = 0; i < arr.length; ++i) {
if (arr[i] === val) {
return true;
}
}
}
return false;
}
function numberDigitsToArr(num, length) {
var digitsArray = [];
for (var i = 0; i < length; ++i) {
digitsArray.push(num % 10);
num = Math.floor(num / 10);
}
return digitsArray;
}
function isNonNegativeInteger(num) {
return num.constructor == Number && !isNaN(num) && num >= 0 && num == Math.floor(num);
}
};