Skip to content

Commit

Permalink
feat(random): Add ability to get an array of random Harry Potter names
Browse files Browse the repository at this point in the history
If you pass a number to the random function, you will receive an array with that number of random
items

closes #2
  • Loading branch information
Gonzalo Manrique committed Jul 11, 2017
1 parent c6dadb7 commit 52839f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
const
uniqueRandomArray = require('unique-random-array'),
harryPotterNames = require('./harry-potter-names.json');
harryPotterNames = require('./harry-potter-names.json'),
getRandomItem = uniqueRandomArray(harryPotterNames);

module.exports = {
all: harryPotterNames,
random: uniqueRandomArray(harryPotterNames)
random: random
};

function random (number) {
if(number === undefined) {
return getRandomItem();
} else {
let randomItems = [];
for (var i = 0; i < number; i++) {
randomItems.push(getRandomItem());
}
return randomItems;
}
}
8 changes: 8 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,13 @@ describe('Harry Potter names', function() {

expect(harryPotterNames.all).to.include(randomItem);
});

it('should return a random array of random items if passed a number', function() {
let randomItems = harryPotterNames.random(3);
expect(randomItems).to.have.lengthOf(3);
randomItems.forEach(item => {
expect(harryPotterNames.all).to.include(item);
});
});
});
});

0 comments on commit 52839f3

Please sign in to comment.