DNI validation fixes

This commit is contained in:
Juan 2018-03-13 12:14:06 +01:00
parent f3044644b0
commit 9b153aec5a
3 changed files with 15 additions and 13 deletions

View File

@ -1,39 +1,39 @@
const validateDni = require('../validateDni'); const validateDni = require('../validateDni');
describe('DNI validation', () => { describe('DNI validation', () => {
it('should return false for invented DNI', () => { it('should return true for any DNI when no country is passed', () => {
let isValid = validateDni('Pepinillos'); let isValid = validateDni('Pepinillos');
expect(isValid).toBeFalsy(); expect(isValid).toBeTruthy();
}); });
describe('Spanish', () => { describe('Spanish', () => {
it('should return true for valid spanish DNI', () => { it('should return true for valid spanish DNI', () => {
let isValid = validateDni('20849756A'); let isValid = validateDni('20849756A', 'es');
expect(isValid).toBeTruthy(); expect(isValid).toBeTruthy();
}); });
it('should return false for spanish DNI with exceeded digits', () => { it('should return false for spanish DNI with exceeded digits', () => {
let isValid = validateDni('208497563239A'); let isValid = validateDni('208497563239A', 'es');
expect(isValid).toBeFalsy(); expect(isValid).toBeFalsy();
}); });
it('should return false for spanish DNI with invalid letter', () => { it('should return false for spanish DNI with invalid letter', () => {
let isValid = validateDni('20243746E'); let isValid = validateDni('20243746E', 'es');
expect(isValid).toBeFalsy(); expect(isValid).toBeFalsy();
}); });
it('should return true for valid spanish CIF', () => { it('should return true for valid spanish CIF', () => {
let isValid = validateDni('B97367486'); let isValid = validateDni('B97367486', 'es');
expect(isValid).toBeTruthy(); expect(isValid).toBeTruthy();
}); });
it('should return false for spanish CIF with invalid letter', () => { it('should return false for spanish CIF with invalid letter', () => {
let isValid = validateDni('A97527786'); let isValid = validateDni('A97527786', 'es');
expect(isValid).toBeFalsy(); expect(isValid).toBeFalsy();
}); });

View File

@ -1,9 +1,11 @@
module.exports = function(fi, country) { module.exports = function(fi, country) {
if (fi == null) return true; if (fi == null || country == null)
if (typeof fi != 'string') return false; return true;
if (typeof fi != 'string' || typeof country != 'string')
return false;
fi = fi.toUpperCase(); fi = fi.toUpperCase();
country = country ? country.toLowerCase() : 'es'; country = country.toLowerCase();
let len = fi.length; let len = fi.length;

View File

@ -4,7 +4,7 @@ module.exports = function(iban) {
iban = iban.toUpperCase(); iban = iban.toUpperCase();
iban = trim(iban); iban = trim(iban);
iban = iban.replace(/\s/g, ""); iban = iban.replace(/\s/g, '');
if (iban.length != 24) { if (iban.length != 24) {
return false; return false;
@ -33,7 +33,7 @@ module.exports = function(iban) {
function module97(iban) { function module97(iban) {
var parts = Math.ceil(iban.length / 7); var parts = Math.ceil(iban.length / 7);
var remainer = ""; var remainer = '';
for (var i = 1; i <= parts; i++) { for (var i = 1; i <= parts; i++) {
remainer = String(parseFloat(remainer + iban.substr((i - 1) * 7, 7)) % 97); remainer = String(parseFloat(remainer + iban.substr((i - 1) * 7, 7)) % 97);
@ -48,6 +48,6 @@ module.exports = function(iban) {
} }
function trim(text) { function trim(text) {
return (text || "").replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g, "" ); return (text || '').replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g, '');
} }
}; };