-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.js
70 lines (61 loc) · 1.78 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
64
65
66
67
68
69
70
const puns = require("./puns.json");
/**
* Get a list of puns.
* @returns {Object[]} puns - The puns.
* @returns {string} puns[].pun - The pun.
* @returns {string} puns[].punchline - The punchline.
*/
exports.all = () => puns;
/**
* Get a random pun.
* @returns {Object} pun - The pun.
* @returns {string} pun.pun - The pun.
* @returns {string} pun.punchline - The punchline.
*/
exports.random = () => {
const randomId = Math.floor(Math.random() * Math.floor(puns.length))
puns[randomId].id = randomId
return puns[randomId]
};
/**
* Get specific pun.
* @param {string} [id] Id of pun; returns random pun otherwise
* @returns {Object} pun - The pun.
* @returns {string} pun.pun - The pun.
* @returns {string} pun.punchline - The punchline.
*/
exports.get = (id) => {
if (id !== undefined) {
if (isNaN(id)) {
throw `${id} is not a number!`;
} else {
if (id < 0 || id >= puns.length) throw `${id} is not in the 0 - ${puns.length - 1} range!`;
return puns[id];
}
} else {
return this.random()
}
}
/**
* Search for a specific pun based on provided keyword or keywords.
* @param {string[]} [keywords] Keyword or List of keywords.
* @returns {Object[]} puns - The puns.
* @returns {string} puns[].pun - The pun.
* @returns {string} puns[].punchline - The punchline.
*/
exports.search = (keywords) => {
if (keywords === undefined) {
return puns;
}
if (!Array.isArray(keywords)) {
keywords = [keywords];
}
let searchResults = [];
keywords.forEach(keyword => {
let perKeywordRelevantPuns = puns.filter(aPun => {
return (aPun.pun.toLowerCase().includes(keyword.toLowerCase()) || aPun.punchline.toLowerCase().includes(keyword.toLowerCase()))
})
searchResults.push(...perKeywordRelevantPuns);
});
return searchResults;
}