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
Showing
8 changed files
with
102 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
jest.config.js | ||
*.json |
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,2 +1,3 @@ | ||
## [1.0.0] - 2020-08-26 | ||
### First util version | ||
* Add first validator version for Spain bank account |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const validate = require('./../src/validator.js'); | ||
|
||
test('it should return TRUE if the passed value is a valid CCC', () => { | ||
expect(validate('14650100971716610913')).toBe(true); | ||
expect(validate('14650100 97 1716610913')).toBe(true); | ||
expect(validate('14650100-97-1716610913')).toBe(true); | ||
expect(validate('14650100- 97-17166 10913')).toBe(true); | ||
}); | ||
|
||
test('should return FALSE if the passed value is not a valid CCC', () => { | ||
expect(validate('1465010097171661913')).toBe(false); | ||
expect(validate('0049-1500-05-1234567890')).toBe(false); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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 validate = (ccc) => { | ||
const cccSanitized = formatString(ccc); | ||
const entityOfficeNumber = getEntityAndOfficeNumbers(cccSanitized); | ||
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; | ||
}; | ||
|
||
module.exports = validate; |