Skip to content

Commit

Permalink
Added option to use random themes
Browse files Browse the repository at this point in the history
  • Loading branch information
DentFuse committed Oct 20, 2020
1 parent b0355ee commit 389dad9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion api/index.js
@@ -1,5 +1,5 @@
let jokes = require('../src/jokes.json');
const { CONSTANTS } = require('../src/utils');
const { CONSTANTS, getRandomArrayElement } = require('../src/utils');
const { qnaCard, quoteCard } = require('../src/renderJokesCard');
const themes = require('../src/themes');

Expand All @@ -21,6 +21,10 @@ module.exports = async (req, res) => {
theme,
} = req.query;

theme = theme.toLoweCase();

if(theme === 'random') theme = getRandomArrayElement(Object.keys(themes));

if(!themes[theme]) theme = 'default';
const colorTheme = themes[theme];
borderColor = borderColor ? borderColor : colorTheme.borderColor;
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Expand Up @@ -13,3 +13,9 @@ exports.CONSTANTS = {
FOUR_HOURS: 14400,
ONE_DAY: 86400,
};

exports.getRandomArrayElement = (arr) => {
min = 0;
max = arr.length;
return arr[Math.floor(Math.random() * (max - min) + min)]; //The maximum is inclusive and the minimum is inclusive
}

0 comments on commit 389dad9

Please sign in to comment.