-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (41 loc) · 1.03 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
'use strict';
const alfy = require('alfy');
const api = 'https://api.chucknorris.io';
const output = [];
const promises = [];
function addJoke(joke) {
output.push({
title: joke.value,
subtitle: `Category: ${joke.category}. Press ⏎ to see, ⌘⏎ to copy, ⌥⏎ to read, or ⌃⏎ to open`,
arg: joke.value,
mods: {
cmd: {
subtitle: 'press ⏎ to copy to clipboard',
arg: joke.value
},
alt: {
subtitle: 'press ⏎ to read aloud',
arg: joke.value
},
ctrl: {
subtitle: `press ⏎ to open in browser: ${joke.url}`,
arg: joke.url
}
},
icon: {
path: 'icon.png'
},
quicklookurl: joke.url,
text: {
copy: joke.value,
largetype: joke.value
}
});
}
function getChuckNorrisJokeFromCategory(category) {
return alfy.fetch(`${api}/jokes/random?category=${category}`).then(addJoke);
}
alfy.fetch(`${api}/jokes/categories`).then(categories => {
categories.forEach(category => promises.push(getChuckNorrisJokeFromCategory(category)));
Promise.all(promises).then(() => alfy.output(output));
});