-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·89 lines (68 loc) · 2.25 KB
/
index.js
File metadata and controls
executable file
·89 lines (68 loc) · 2.25 KB
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
#! /usr/bin/env node
'use strict'
const optimist = require('optimist')
.usage('Usage: $0 -f [currency] -t [currency] [num]')
.demand(['f', 't'])
.alias('f', 'from')
.describe('f', `Currency that you are using as base
USD,EUR,GBP,COP...
Ways to use it:
-f USD, --f USD, --f=USD, --from USD, --from=USD`)
.alias('t', 'to')
.describe('t', `Currency that you want to convert
USD,EUR,GBP,COP...
Ways to use it:
-t USD, --t USD, --t=USD, --to USD, --to=USD`)
.alias('h', 'help')
.alias('s', 'super')
.describe('s', `Show more details about the conversion
if you don't pass this param it will show the basic information
Ways to use it:
-s, --s, -super, --super`)
.argv
const http = require('http')
if (typeof optimist.from !== 'string') {
console.log('From param should be string')
process.exit()
}
optimist.from = optimist.from.toUpperCase()
if (typeof optimist.to !== 'string') {
console.log('To param should be string')
process.exit()
}
optimist.to = optimist.to.toUpperCase()
let amount
process.argv.forEach((value) => {
if (!isNaN(parseFloat(value)) && isFinite(value)) {
amount = value
}
})
if (!amount) {
console.log('Amount param should be defined')
process.exit()
}
let type = 'simple'
if (optimist.s) {
type = 'full'
};
var code = `${optimist.from}_${optimist.to}`
http.get('http://free.currencyconverterapi.com/api/v3/convert?q=' + code + '&compact=ultra', (res) => {
res.on('data', (data) => {
data = JSON.parse(data)
if (!data[code]) {
console.log('Currency no supported or bad spell')
} else {
// raw response
if (type === 'full') {
console.log(`Amount: ${amount} ${optimist.from}`)
console.log(`From: ${optimist.from}`)
console.log(`To: ${optimist.to}`)
console.log(`Current Rate: ${data[code]} ${optimist.to}`)
console.log(`Total: ${amount * data[code]} ${optimist.to}`)
} else {
console.log(`${amount * data[code]} ${optimist.to}`)
}
console.log('---------------------------------------------------------')
}
})
})