Created
May 17, 2020 13:26
-
-
Save Kein1945/72fc7f8dd56be2a31dd270db20971c3d to your computer and use it in GitHub Desktop.
Bash/Javascript currency converter. Use fixer.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
money() { | |
node <<EOF | |
const http = require('http'); | |
const url = 'http://data.fixer.io/api/latest?access_key=API_KEY'; | |
http.get(url, (res) => { | |
res.setEncoding('utf8'); | |
let body = ''; | |
res.on('data', (data) => { | |
body += data; | |
}); | |
res.on('end', () => { | |
var to="${3:-RUB}" | |
var from="${2:-USD}" | |
var amount="${1:-1}" | |
if(isNaN(amount)) { | |
from = amount; | |
amount = 1; | |
} | |
const { rates } = JSON.parse(body); | |
console.log({from, to, amount}); | |
console.log((amount / rates[from] * rates[to]).toFixed(2)); | |
}); | |
}); | |
EOF | |
} | |
# Usage | |
# money 56 AUD USD -> Convert 56 AUD to USD | |
# money 12 THB -> convert 10 THB to default target currency (hardcoded RUB in this case) | |
# money USD -> convert 1 USD to default target currency | |
# money 10 -> convert 10 USD(default source currency, hardcoded USD in this case) to default target currency |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment