From f9df3521f676a5bffed63da501c96f78bc25463a Mon Sep 17 00:00:00 2001 From: popovkov57 Date: Thu, 3 Nov 2016 21:58:47 +0100 Subject: [PATCH] feat(random): Add ability to get an array of dikkenek quotes 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 | 15 ++++++++++++++- src/index.test.js | 9 +++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 27e18cc..8a4cfab 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,20 @@ var uniqueRandomArray = require('unique-random-array'); var dikkenekQuotes = require('./dikkenek-quotes.json'); +var getRandomItem = uniqueRandomArray(dikkenekQuotes); module.exports = { all: dikkenekQuotes, - random: uniqueRandomArray(dikkenekQuotes) + random: random }; + +function random(number){ + if (number === undefined) { + return getRandomItem(); + } else { + var randomItem = []; + for (var i = 0; i < number; i++) { + randomItem.push(getRandomItem()); + } + return randomItem; + } +} diff --git a/src/index.test.js b/src/index.test.js index ff70391..b0e8264 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -16,6 +16,7 @@ describe('dikkenek-quotes', function(){ it('it should contain `C’est excessivement énervant !`', function(){ expect(dikkenek.all).to.include('C’est excessivement énervant !'); }); + }); describe('random', function(){ @@ -23,5 +24,13 @@ describe('dikkenek-quotes', function(){ var randomItem = dikkenek.random(); expect(dikkenek.all).to.include(randomItem); }); + + it('should return an array of random items if passed a number', function(){ + var randomItem = dikkenek.random(3); + expect(randomItem).to.have.length(3); + randomItem.forEach(function(item){ + expect(dikkenek.all).to.include(item); + }) + }); }); });