-
Notifications
You must be signed in to change notification settings - Fork 7
/
irdnz.js
60 lines (52 loc) · 1.69 KB
/
irdnz.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
/**
* Checksum validator for New Zealand's IRD/GST number.
* Implementation based on specification available at:
* http://www.ird.govt.nz/resources/9/e/9e408a004e329f16a91bbf8ad6853786/rwt-nrwt-spec-2013+v1.0+.pdf
*
* This module exposes irdnz.isValid method that returns true if IRD/GST number is correct.
*/
(function (exports) {
var PRIMARY_WEIGHTS = [3, 2, 7, 6, 5, 4, 3, 2],
SECONDARY_WEIGHTS = [7, 4, 3, 2, 5, 2, 7, 6];
function isInRange (ird) {
return 10000000 < ird && ird < 150000000;
}
function calculateSum (baseNumber, weights) {
var irdDigits = baseNumber.toString(10).split(""),
/* If IRD number is shorter then 8 digits we need to skip first weight. */
start = weights.length - irdDigits.length,
sum = 0;
for (var i = start; i < weights.length; i++) {
sum += parseInt(irdDigits[i - start], 10) * weights[i];
};
return sum;
}
function calculateCheckDigit (baseNumber, weights) {
var sum = calculateSum(baseNumber, weights);
var rem = sum % 11;
if(rem !== 0){
rem = 11 - rem;
}
return rem;
}
/**
* Checks if New Zealand's IRD/GST number is correct.
*
* @param {string} ird - New Zealand's IRD or GST number.
* @returns {boolean} - true if IRD/GST is valid, false otherwise.
*/
exports.isValid = function (ird) {
/* Make sure IRD is a number. */
var ird = parseInt(ird, 10);
if(isInRange(ird)){
var baseNumber = (ird / 10) >> 0,
remainder = ird % 10;
var checkDigit = calculateCheckDigit(baseNumber, PRIMARY_WEIGHTS);
if(checkDigit === 10){
checkDigit = calculateCheckDigit(baseNumber, SECONDARY_WEIGHTS);
}
return checkDigit === remainder;
}
return false;
};
})(typeof exports === 'undefined' ? window['irdnz']={} : exports);