From d3bd2c869c5dcecbe4dc11fb9fe4481e908d93e1 Mon Sep 17 00:00:00 2001 From: Vojko Drev Date: Fri, 16 Aug 2019 09:19:01 +0200 Subject: [PATCH] Added getIbanCountryCode and changed iban.js to use it. --- iban.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/iban.js b/iban.js index 8399833..77391a3 100644 --- a/iban.js +++ b/iban.js @@ -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; }; @@ -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. * @@ -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); }; @@ -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); };