-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
65 lines (60 loc) · 1.72 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
const data = require('./bankData.js')
const leftPad = require('left-pad')
const bankData = data.bankData
const isValidBankAndBranch = (bankId, branch) => {
if (!bankData.map(o => o.id).includes(+bankId)) return false
const bank = bankData.find(o => o.id === +bankId)
return bank.branches.map(b => +branch >= b.from && +branch <= b.to).some(r => r)
}
const getWeightFactor = (bankId, accountNumber) => {
switch (+bankId) {
case 8:
return '000000076543210000'
case 9:
return '000000000054320001'
case 25:
case 33:
return '000000017317310000'
case 26:
case 28:
case 29:
return '000000013713710371'
case 31:
return '000000000000000000'
default:
if (+accountNumber < 990000) return '00637900A584210000'
else return '00000000A584210000'
}
}
const getModulo = (bankId) => {
switch (+bankId) {
case 25:
case 33:
case 26:
case 28:
case 29:
return 10
case 31:
return 1
default:
return 11
}
}
const isValidNZBankNumber = (bk, brch, acct, suf) => {
const bank = leftPad(bk, 2, '0')
const branch = leftPad(brch, 4, '0')
const account = leftPad(acct, 8, '0')
const suffix = leftPad(suf, 4, '0')
if (+account === 0) return false
if (!isValidBankAndBranch(bank, branch)) return false
const checkString = bank + branch + account + suffix
if (checkString.length !== 18) return false
const weightFactor = getWeightFactor(bank, account)
const result = weightFactor.split('')
.map((v, i) => (v === 'A' ? 10 : +v) * +checkString
.split('')[i])
.reduce((acc, curr) => acc + curr)
const checkNumber = getModulo(bank)
return result % checkNumber === 0
}
exports.isValidNZBankNumber = isValidNZBankNumber