-
Notifications
You must be signed in to change notification settings - Fork 0
/
sci.mjs
executable file
·40 lines (35 loc) · 952 Bytes
/
sci.mjs
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
#! /usr/bin/env node
import fetch from "node-fetch";
import yargs from "yargs";
//const { argv } = yargs(process.argv);
const argv = yargs(process.argv.slice(2))
.options({
one: undefined,
many: undefined,
help: undefined,
})
.parse();
// call to get array of sci fi objs
async function getQuotes() {
const res = await fetch("https://sci-fi-api.netlify.app/");
const data = await res.json();
return data.quotes;
}
// call to get random number limited to array size
async function randomizer(arr) {
return Math.floor(Math.random() * arr.length);
}
if (argv.one) {
const sciFiArray = await getQuotes();
const randomKey = await randomizer(sciFiArray);
console.log(sciFiArray[randomKey].line);
} else if (argv.many) {
const sciFiArray = await getQuotes();
console.log(sciFiArray);
} else if (argv.help) {
console.log(`
sci -help : Get help menu
sci -one : Get 1 quote
sci -many : Get multiple quotes
`);
}