Skip to content

Instantly share code, notes, and snippets.

@Kein1945
Created May 17, 2020 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kein1945/72fc7f8dd56be2a31dd270db20971c3d to your computer and use it in GitHub Desktop.
Save Kein1945/72fc7f8dd56be2a31dd270db20971c3d to your computer and use it in GitHub Desktop.
Bash/Javascript currency converter. Use fixer.io
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