Skip to content

GetIbanCountryCode #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions iban.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
*/
Specification.prototype.isValid = function(iban){
return this.length == iban.length
&& this.countryCode === iban.slice(0,2)
&& this.countryCode === getIbanCountryCode(iban)
&& this._regex().test(iban.slice(4))
&& iso7064Mod97_10(iso13616Prepare(iban)) == 1;
};
Expand Down Expand Up @@ -335,6 +335,18 @@
return (typeof v == 'string' || v instanceof String);
}

/**
* Returns IBAN's country code.
*
* @param {String} iban the IBAN.
* @returns {String} Country code.
*/
function getIbanCountryCode(iban) {
return iban.slice(0,2);
}

exports.getIbanCountryCode = getIbanCountryCode;

/**
* Check if an IBAN is valid.
*
Expand All @@ -346,7 +358,7 @@
return false;
}
iban = electronicFormat(iban);
var countryStructure = countries[iban.slice(0,2)];
var countryStructure = countries[getIbanCountryCode(iban)];
return !!countryStructure && countryStructure.isValid(iban);
};

Expand All @@ -362,9 +374,9 @@
separator = ' ';
}
iban = electronicFormat(iban);
var countryStructure = countries[iban.slice(0,2)];
var countryStructure = countries[getIbanCountryCode(iban)];
if (!countryStructure) {
throw new Error('No country with code ' + iban.slice(0,2));
throw new Error('No country with code ' + getIbanCountryCode(iban));
}
return countryStructure.toBBAN(iban, separator);
};
Expand Down