From 029b6432aa14202869d7b4eb6a1e5d4efdf4323e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sas=CC=8Ca=20Jovanic=CC=81?= Date: Wed, 1 Dec 2021 09:45:52 +0100 Subject: [PATCH] Improve test coverage --- ChangeLog | 3 +++ src/IBANTools.ts | 27 +++++++++------------------ test/ibantools_test.js | 9 +++++++++ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index f217327..bdc3636 100755 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2021-12-01 Saša Jovanić + * Improve test coverage + 2021-11-30 Saša Jovanić * Added Estonian (EE) BBAN validation * Added Finland (FI) BBAN validation diff --git a/src/IBANTools.ts b/src/IBANTools.ts index 96f840f..e85ea88 100755 --- a/src/IBANTools.ts +++ b/src/IBANTools.ts @@ -516,18 +516,14 @@ interface CountryMapInternal { const checkNorwayBBAN = (bban: string): boolean => { const weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]; const bbanWithoutSpacesAndPeriods = bban.replace(/[\s.]+/g, ''); - if (bbanWithoutSpacesAndPeriods.length !== 11) { - return false; - } else { - const controlDigit = parseInt(bbanWithoutSpacesAndPeriods.charAt(10), 10); - const bbanWithoutControlDigit = bbanWithoutSpacesAndPeriods.substring(0, 10); - let sum = 0; - for (let index = 0; index < 10; index++) { - sum += parseInt(bbanWithoutControlDigit.charAt(index), 10) * weights[index]; - } - const remainder = sum % 11; - return controlDigit === (remainder === 0 ? 0 : 11 - remainder); + const controlDigit = parseInt(bbanWithoutSpacesAndPeriods.charAt(10), 10); + const bbanWithoutControlDigit = bbanWithoutSpacesAndPeriods.substring(0, 10); + let sum = 0; + for (let index = 0; index < 10; index++) { + sum += parseInt(bbanWithoutControlDigit.charAt(index), 10) * weights[index]; } + const remainder = sum % 11; + return controlDigit === (remainder === 0 ? 0 : 11 - remainder); }; /** @@ -557,11 +553,8 @@ const checkBelgianBBAN = (bban: string): boolean => { const stripped = bban.replace(/[\s.]+/g, ''); const checkingPart = parseInt(stripped.substring(0, stripped.length - 2), 10); const checksum = parseInt(stripped.substring(stripped.length - 2, stripped.length), 10); - let reminder = checkingPart % 97; - if (reminder === 0) { - reminder = 97; - } - return reminder === checksum; + const remainder = checkingPart % 97 === 0 ? 97 : checkingPart % 97; + return remainder === checksum; }; /** @@ -801,8 +794,6 @@ const checkFrenchBBAN = (bban: string): boolean => { case 90: normalized[index] = '9'; break; - default: - break; } } } diff --git a/test/ibantools_test.js b/test/ibantools_test.js index 6191790..53276e6 100644 --- a/test/ibantools_test.js +++ b/test/ibantools_test.js @@ -233,6 +233,9 @@ describe('IBANTools', function() { it('with valid CZ IBAN should return true', function() { expect(iban.isValidIBAN('CZ6508000000192000145399')).to.be.true; }); + it('with invalid CZ IBAN should return false', function() { + expect(iban.isValidIBAN('CZ6508000000182000145399')).to.be.false; + }); it('with valid EE IBAN should return true', function() { expect(iban.isValidIBAN('EE443300338400100007')).to.be.true; }); @@ -257,6 +260,9 @@ describe('IBANTools', function() { it('with valid FR IBAN should return true', function() { expect(iban.isValidIBAN('FR1420041010050500013M02606')).to.be.true; }); + it('with valid FR IBAN should return true', function() { + expect(iban.isValidIBAN('FR22200410100505QZABCMGEF65')).to.be.true; + }); it('with valid MC IBAN should return true', function() { expect(iban.isValidIBAN('MC5811222000010123456789030')).to.be.true; }); @@ -530,6 +536,9 @@ describe('IBANTools', function() { it('with valid BBAN for country code NO should return true', function() { expect(iban.isValidBBAN('12043175449', 'NO')).to.be.true; }); + it('with too short BBAN for country code NO should return false', function() { + expect(iban.isValidBBAN('1204317544', 'NO')).to.be.false; + }); }); describe('When calling composeIBAN()', function() {