Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f366b86
commit e5355c1
Showing
6 changed files
with
116 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,8 @@ | ||
const formatString = string => string.split('-').join('').replace(/\s/g, ''); | ||
|
||
const factors = [1, 2, 4, 8, 5, 10, 9, 7, 3, 6]; | ||
|
||
const getEntityAndOfficeNumbers = (str) => { | ||
return `00${str.substring(0,8)}`; | ||
}; | ||
|
||
const getAccountNumber = (str) => { | ||
return str.substring(10,20); | ||
}; | ||
|
||
const getControlDigit = (str) => { | ||
let controlDigit; | ||
|
||
const sum = Array.from(str).map((el, index) => { | ||
return el * (factors[index]); | ||
}).reduce((acc, curr) => acc + curr); | ||
|
||
const num = 11 - (sum % 11); | ||
|
||
if (num === 11 ) { | ||
controlDigit = 0; | ||
} else if (num === 10) { | ||
controlDigit = 1; | ||
} else { | ||
controlDigit = num; | ||
} | ||
|
||
return controlDigit; | ||
|
||
}; | ||
|
||
const checkInstitution = (institution) => { | ||
if (typeof institution === 'string') { | ||
return true; | ||
} else { | ||
throw 'Institution is not a valid string'; | ||
} | ||
}; | ||
|
||
const validateBankEnity = ({ institutionName, country, entityCode}) => { | ||
const institutionCodes = require(`./${country}-institution-list.js`); | ||
return institutionCodes[`ES${entityCode.substring(2, 6)}`] === institutionName; | ||
}; | ||
|
||
const validate = (ccc, { institution = '', country = 'es' } = {}) => { | ||
const cccSanitized = formatString(ccc); | ||
const entityOfficeNumber = getEntityAndOfficeNumbers(cccSanitized); | ||
let isValidBankEntity = true; | ||
|
||
if (checkInstitution(institution) && institution !== '') { | ||
isValidBankEntity = validateBankEnity({ | ||
institutionName: institution, | ||
country: country, | ||
entityCode: entityOfficeNumber | ||
}); | ||
} | ||
|
||
const accountNumber = getAccountNumber(cccSanitized); | ||
const firstControlDigit = getControlDigit(entityOfficeNumber); | ||
const secondControlDigit = getControlDigit(accountNumber); | ||
|
||
return parseInt(cccSanitized.substring(8, 9)) === firstControlDigit && | ||
parseInt(cccSanitized.substring(9, 10)) === secondControlDigit && | ||
isValidBankEntity; | ||
const validate = require(`./validators/${country}-validator.js`); | ||
return validate(ccc, {institution, country}); | ||
}; | ||
|
||
module.exports = validate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const formatString = string => string.split('-').join('').replace(/\s/g, ''); | ||
|
||
const factors = [1, 2, 4, 8, 5, 10, 9, 7, 3, 6]; | ||
|
||
const getEntityAndOfficeNumbers = (str) => { | ||
return `00${str.substring(0,8)}`; | ||
}; | ||
|
||
const getAccountNumber = (str) => { | ||
return str.substring(10,20); | ||
}; | ||
|
||
const getControlDigit = (str) => { | ||
let controlDigit; | ||
|
||
const sum = Array.from(str).map((el, index) => { | ||
return el * (factors[index]); | ||
}).reduce((acc, curr) => acc + curr); | ||
|
||
const num = 11 - (sum % 11); | ||
|
||
if (num === 11 ) { | ||
controlDigit = 0; | ||
} else if (num === 10) { | ||
controlDigit = 1; | ||
} else { | ||
controlDigit = num; | ||
} | ||
|
||
return controlDigit; | ||
|
||
}; | ||
|
||
const checkInstitution = (institution) => { | ||
if (typeof institution === 'string') { | ||
return true; | ||
} else { | ||
throw 'Institution param is not a valid string'; | ||
} | ||
}; | ||
|
||
const validateBankEnity = ({ institutionName, country, entityCode}) => { | ||
const institutionCodes = require(`./../${country}-institution-list.js`); | ||
return institutionCodes[`ES${entityCode.substring(2, 6)}`] === institutionName; | ||
}; | ||
|
||
const validator = (ccc, { institution = '', country = 'es'} = {}) => { | ||
const cccSanitized = formatString(ccc); | ||
const entityOfficeNumber = getEntityAndOfficeNumbers(cccSanitized); | ||
let isValidBankEntity = true; | ||
|
||
if (checkInstitution(institution) && institution !== '') { | ||
isValidBankEntity = validateBankEnity({ | ||
institutionName: institution, | ||
country: country, | ||
entityCode: entityOfficeNumber | ||
}); | ||
} | ||
|
||
const accountNumber = getAccountNumber(cccSanitized); | ||
const firstControlDigit = getControlDigit(entityOfficeNumber); | ||
const secondControlDigit = getControlDigit(accountNumber); | ||
|
||
return parseInt(cccSanitized.substring(8, 9)) === firstControlDigit && | ||
parseInt(cccSanitized.substring(9, 10)) === secondControlDigit && | ||
isValidBankEntity; | ||
}; | ||
|
||
module.exports = validator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const { isValidNZBankNumber } = require('@fnzc/nz-bank-account-validator'); | ||
|
||
// '00', '0000', '00000000', '000' | ||
const getFormattedAccountNumber = (ccc) => { | ||
const group1 = ccc.substring(0,2); | ||
const group2 = ccc.substring(2,6); | ||
const group3 = ccc.substring(6,14); | ||
const group4 = ccc.substring(14,17); | ||
|
||
return { | ||
group1, | ||
group2, | ||
group3, | ||
group4 | ||
}; | ||
}; | ||
|
||
const validate = (ccc) => { | ||
const { group1, group2, group3, group4 } = getFormattedAccountNumber(ccc); | ||
return isValidNZBankNumber(group1, group2, group3, group4); | ||
} | ||
|
||
module.exports = validate; |