#558 validateIban.js Backend unit tests

This commit is contained in:
Carlos Jimenez 2018-11-15 11:55:27 +01:00
parent 5b68b415f7
commit 6206884330
2 changed files with 33 additions and 15 deletions

View File

@ -0,0 +1,21 @@
const validateIban = require('../validateIban');
describe('IBAN validation', () => {
it('should return false for non-IBAN input', () => {
let isValid = validateIban('Pepinillos');
expect(isValid).toBeFalsy();
});
it('should return false for invalid spanish IBAN input', () => {
let isValid = validateIban('ES00 9999 0000 9999 0000 9999');
expect(isValid).toBeFalsy();
});
it('should return true for valid spanish IBAN', () => {
let isValid = validateIban('ES91 2100 0418 4502 0005 1332');
expect(isValid).toBeTruthy();
});
});

View File

@ -6,44 +6,41 @@ module.exports = function(iban) {
iban = trim(iban);
iban = iban.replace(/\s/g, '');
if (iban.length != 24) {
if (iban.length != 24)
return false;
}
// Se coge las primeras dos letras y se pasan a números
var letter1 = iban.substring(0, 1);
var letter2 = iban.substring(1, 2);
var num1 = getIbanNumber(letter1);
var num2 = getIbanNumber(letter2);
let letter1 = iban.substring(0, 1);
let letter2 = iban.substring(1, 2);
let num1 = getIbanNumber(letter1);
let num2 = getIbanNumber(letter2);
// Se sustituye las letras por números.
var isbanaux = String(num1) + String(num2) + iban.substring(2);
let isbanaux = String(num1) + String(num2) + iban.substring(2);
// Se mueve los 6 primeros caracteres al final de la cadena.
isbanaux = isbanaux.substring(6) + isbanaux.substring(0, 6);
// Se calcula el resto, llamando a la función module97, definida más abajo
var resto = module97(isbanaux);
let resto = module97(isbanaux);
if (resto == 1) {
if (resto == 1)
return true;
}
return false;
function module97(iban) {
var parts = Math.ceil(iban.length / 7);
var remainer = '';
let parts = Math.ceil(iban.length / 7);
let remainer = '';
for (var i = 1; i <= parts; i++) {
for (let i = 1; i <= parts; i++)
remainer = String(parseFloat(remainer + iban.substr((i - 1) * 7, 7)) % 97);
}
return remainer;
}
function getIbanNumber(letra) {
var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
return letters.search(letra) + 10;
}