-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from JuanMaRuiz/feature/add-generator
Add isbn generator
- Loading branch information
Showing
2 changed files
with
25 additions
and
0 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,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); | ||
}); |
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,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; |