-
Notifications
You must be signed in to change notification settings - Fork 291
/
index.js
63 lines (53 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
let array = require("./data/data.json");
const randomNum = () => {
return Math.floor(Math.random() * array.length);
};
const getQuoteByYear = (start, end) => {
return array
.filter(i => i.year >= start && i.year <= end)
.sort((a, b) => (a.year > b.year ? 1 : b.year > a.year ? -1 : 0));
};
const getRandomQuote = () => {
let randNum = randomNum();
return array[randNum].quote;
};
const getSomeRandom = count => {
let randomQuotesArray = [];
let randomQuotesSet = new Set(); // to prevent duplicate quotes
while (randomQuotesArray.length < count) {
let quote = array[randomNum()];
if (!randomQuotesSet.has(quote)) {
randomQuotesArray.push(quote);
}
}
return randomQuotesArray;
};
const getQuotesByObject = (quote, obj) => {
let resultArray = [];
quote = quote.toLowerCase();
object = obj;
array.forEach(item => {
item[object] = item[object].toLowerCase();
if (item[object] && item[object].includes(quote)) {
resultArray.push(item);
}
});
return resultArray;
};
const getQuotesByMovie = quote => {
return getQuotesByObject(quote, "movie");
};
const getQuotesByType = quote => {
return getQuotesByObject(quote, "type");
};
const getAll = () => {
return array;
};
module.exports = {
getAll,
getRandomQuote,
getSomeRandom,
getQuoteByYear,
getQuotesByMovie,
getQuotesByType
};