Skip to content

Commit

Permalink
Merge d1e712c into f7b0bd9
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanMaRuiz committed Sep 13, 2020
2 parents f7b0bd9 + d1e712c commit 5d7bb29
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
@@ -0,0 +1,2 @@
jest.config.js
*.json
6 changes: 5 additions & 1 deletion .eslintrc.json
Expand Up @@ -26,6 +26,10 @@
2
],
"curly": "error",
"max-len": ["error", { "code": 140 }]
"max-len": ["error", { "code": 140 }],
"quotes": [
"error",
"single"
]
}
}
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1,2 +1,3 @@
## [1.0.0] - 2020-08-26
### First util version
* Add first validator version for Spain bank account
27 changes: 26 additions & 1 deletion README.md
Expand Up @@ -3,7 +3,21 @@

# CCC Util

This module check if a CCC is valid or not and also provide a method to generate a valid random CCC.
This module provide a `validate` method to check if the given CCC is valid or not.

**Note**

Due the different logic to validate bank account depending on the Country. This validator is only valid for **Spain** bank accounts. If you are interested in this validator and you want to add a new country, you can:

**Open an issue like this:**

```
Title: Add support for new Country
* **Country**: _New country_
* **Validation**: _Link to info about how to validade the account_
**Open a Pull request** adding your own validation.
## Install
Expand All @@ -14,6 +28,17 @@ npm i ccc-util --save-dev
## Usage

```bash
const { validate } = require('ccc-util');

validate('14650100971716610913'); //=> true
validate('14650100-97-1716610913'); //=> true
validate('14650100 97 1716610913'); //=> true

validate('0137081073'); //=> false

validate('0049-1500-05-1234567890); //=> false
```
### Reference
* [How to validate an Spanish CCC](https://es.wikipedia.org/wiki/Código_cuenta_cliente#D%C3%ADgitos_de_control)
13 changes: 13 additions & 0 deletions __test__/validator.test.js
@@ -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);
});
2 changes: 1 addition & 1 deletion index.js
@@ -1,4 +1,4 @@
const validate = require('./src/validate.js');
const validate = require('./src/validator.js');


module.exports = {
Expand Down
10 changes: 10 additions & 0 deletions jest.config.js
Expand Up @@ -28,4 +28,14 @@ module.exports = {

// The test environment that will be used for testing
testEnvironment: "node",

// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
"global": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": -10
}
}
};
44 changes: 44 additions & 0 deletions src/validator.js
@@ -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;

0 comments on commit 5d7bb29

Please sign in to comment.