-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (94 loc) · 3.48 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { readFileSync } from 'fs'
/**
* Get and return a roast using promises.
* @param {object} [searchArgs] Object that determens the roast.
* If ommitted, it will return a random roast.
* @param {String} [args.type] the type of roast Corresponds with the array name in jokes.json
* If type is ommitted, getRoast() will flatten jokes.json for use with ID.
* @param {String} [args.id] gets specific roast ID Corresponds with position of current selected array.
* If id is omitted, it will return a random joke based on type.
*
* @returns {Promise<string>} Roast based on your args.
*
* @example yourmama.getRoast({type:"fat",id:0}).then(res=>console.log(res))
* "Yo mama is so fat that her bellybutton gets home 15 minutes before she does."
*/
export async function getRoast(args) {
return new Promise((resolve,reject)=>{
if((typeof args == 'undefined')||(typeof args.type == 'undefined'&& typeof args.id == 'undefined')){
return this.getRandom().then(bruh => resolve(bruh))
}
if(typeof args.type == 'string' && typeof args.id == 'undefined'){
return this.getRandomTopic(args.type).then(bruh => resolve(bruh))
}
if(typeof args.id == 'string' && typeof args.type == 'undefined'){
return this.getID(args.id).then(bruh => resolve(bruh))
}
try{
const jokes = JSON.parse(readFileSync(new URL('jokes.json', import.meta.url)));
return resolve(jokes[args.type][args.id])
}catch(e){
return reject(new Error("yo mama so stupid she fucked up syntax, probably\nshould be {type:'typet',id:#id}\n\n"+e))
}
})
}
/**
*
* get a random roast
* @returns {Promise<string>} A random roast.
*/
export async function getRandom(){
return new Promise((resolve,reject)=>{
try{
const jokes = JSON.parse(readFileSync(new URL('jokes.json', import.meta.url)));
let bigArray = Object.values(jokes)
let bruh = []
for(thing of bigArray){
bruh.push(...thing)
}
return resolve(bruh[Math.floor(Math.random() * bruh.length)])
}catch(e){
reject(new Error("something messed up\n\n"+e))
}
})
}
/**
* Get a random roast based on topic
*
* @param {string} topic Decides what array to use.
* @returns {Promise<string>} A random roast based on your topic
* @throws An error if topic doesn't exist.
*/
export async function getRandomTopic(topic){
return new Promise((resolve,reject)=>{
try{
const jokes = JSON.parse(readFileSync(new URL('jokes.json', import.meta.url)));
const bruh = Object.values(jokes[topic])
return resolve(bruh[Math.floor(Math.random() * bruh.length)])
}catch(e){
reject(new Error("something messed up\n\n"+e))
}
})
}
/**
* Flattens jokes.json and returns desired entry.
*
* @param {int} id Position of desired roast.
* @returns {Promise<string>} Said roast.
* @throws An error if roast doesn't exist.
*/
export async function getID(id){
return new Promise((resolve,reject)=>{
try{
const jokes = JSON.parse(readFileSync(new URL('jokes.json', import.meta.url)));
let bigArray = Object.values(jokes)
let bruh = []
for(const thing of bigArray){
bruh.push(...thing)
}
return resolve(bruh[id])
}catch(e){
reject(new Error("something messed up\n\n"+e))
}
})
}