-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·73 lines (60 loc) · 1.77 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
#!/usr/bin/env node
const axios = require('axios')
const chalk = require('chalk')
const ora = require('ora')
const table = require('table').table
const keys = require('./keys')
// Validate keyword
if (process.argv.length !== 3) {
console.log('Usage: wtf wtf')
return
}
// Get keyword
const term = process.argv[2]
const spinner = ora(chalk.blue('Looking for ' + term + '...')).start();
// Send request
axios.get(makeRequestUrl(term))
.then(response => {
// Stop loading
spinner.stop()
let results = [['No.', 'Description', 'Rating']]
// Get top 10
const top10 = response.data.result.slice(0, 10)
top10.forEach((meaning, index) => {
// Add description
results.push([
chalk.yellow(index),
meaning.definition,
rate(5, meaning.score)
])
})
// Print results
printResults(results)
})
.catch(error => {
console.log(error)
})
// Print out formatted results
function printResults(results) {
console.log(chalk.red('##############################'))
console.log(table(results))
console.log(chalk.magenta('Results provided courtesy of Abbreviations.com (https://www.abbreviations.com/)'))
console.log(chalk.red('##############################'))
}
// Get ratings styles
function rate(total, likes) {
let ratings = ''
for (let i = 0; i < total; i++) {
if (i < likes) {
ratings += chalk.green('+')
}
else {
ratings += chalk.red('-')
}
}
return ratings
}
// Make request url
function makeRequestUrl(term) {
return `https://www.abbreviations.com/services/v2/abbr.php?uid=${keys.userId}&tokenid=${keys.token}&term=${term}&format=json`
}