Skip to content

Commit

Permalink
Merge pull request #1 from JuanMaRuiz/feature/add-generator
Browse files Browse the repository at this point in the history
Add isbn generator
  • Loading branch information
JuanMaRuiz committed Aug 26, 2020
2 parents e04c2ae + 7a0b5ea commit 21bd260
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions __test__/generator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const generator = require('./../src/generator.js');
const validator = require('./../src/validator.js');

test('should return a valid isbn according with the passed param', () => {
const validIsbn = generator('10');
expect(validator(validIsbn)).toBe(true);
});
18 changes: 18 additions & 0 deletions src/generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const validator = require('./validator');
const isbnTypes = {
10: '10',
13: '13'
};

const generator = (type = isbnTypes[10]) => {
const randomNumber = parseInt(Math.random() * parseInt(Math.pow(10,type)));
if ( type === isbnTypes[type] ) {
const tempIsbn = randomNumber.toString();
return validator(tempIsbn) ? tempIsbn : generator(type);
} else {
console.error('Invalid option. Pass "10" for isbn-10 and "13" for isbn-13');
process.exit(1);
}
};

module.exports = generator;

0 comments on commit 21bd260

Please sign in to comment.