From 52839f32118faee512420879335402b0de2ca5a9 Mon Sep 17 00:00:00 2001 From: Gonzalo Manrique Date: Tue, 11 Jul 2017 00:52:12 -0300 Subject: [PATCH] feat(random): Add ability to get an array of random Harry Potter names If you pass a number to the random function, you will receive an array with that number of random items closes #2 --- src/index.js | 17 +++++++++++++++-- src/index.test.js | 8 ++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index d982e90..0329214 100644 --- a/src/index.js +++ b/src/index.js @@ -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; + } +} diff --git a/src/index.test.js b/src/index.test.js index 225fff2..94353d1 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -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); + }); + }); }); });