Comparing version 3.0.0 to 4.0.0
51
cli.js
#!/usr/bin/env node | ||
'use strict'; | ||
var pkg = require('./package.json'); | ||
var currency = require('./'); | ||
var argv = process.argv.slice(2); | ||
const currency = require('./') | ||
const argv = process.argv.slice(2) | ||
const updateNotifier = require('update-notifier'); | ||
updateNotifier({pkg}).notify(); | ||
const updateNotifier = require('update-notifier') | ||
const pkg = require('./package.json') | ||
updateNotifier({pkg}).notify() | ||
function help() { | ||
console.log([ | ||
'', | ||
' ' + pkg.description, | ||
' ' + pkg.description, | ||
'', | ||
' Example', | ||
' currency 10 usd dkk', | ||
' $ currency 10 usd dkk', | ||
' => 10 USD = 62.208 DKK', | ||
'', | ||
' => 57.75516' | ||
].join('\n')); | ||
' See readme.md for detailed usage.' | ||
].join('\n')) | ||
} | ||
if (argv.indexOf('--help') !== -1) { | ||
help(); | ||
return; | ||
help() | ||
return | ||
} | ||
if (argv.indexOf('--version') !== -1) { | ||
console.log(pkg.version); | ||
return; | ||
console.log(pkg.version) | ||
return | ||
} | ||
var opts = { | ||
amount: argv[0], | ||
from: argv[1], | ||
to: argv[2] | ||
}; | ||
let opts = { | ||
amount: argv[0] || 1, | ||
from: (argv[1] || 'USD').toUpperCase(), | ||
to: (argv[2] || 'BTC').toUpperCase() | ||
} | ||
currency(opts).then(console.log).catch(err => { | ||
console.log(err); | ||
currency(opts) | ||
.then(result => { | ||
console.log(`${opts.amount} ${opts.from} = ${result} ${opts.to}`) | ||
}) | ||
.catch(err => { | ||
console.log(err) | ||
process.exit(1); | ||
}); | ||
process.exit(1) | ||
}) |
104
index.js
@@ -1,32 +0,94 @@ | ||
'use strict'; | ||
const got = require('got') | ||
const money = require('money') | ||
const got = require('got'); | ||
const money = require('money'); | ||
const FIXER_URL = 'https://api.fixer.io/latest' | ||
const BLOCKCHAIN_URL = 'https://blockchain.info/ticker' | ||
const ETHERCHAIN_URL = 'https://etherchain.org/api/statistics/price' | ||
const path = 'https://api.fixer.io/latest'; | ||
const CURRENCY_BITCOIN = 'BTC' | ||
const CURRENCY_ETHEREUM = 'ETH' | ||
let isAnyBTC = (from, to) => [from, to].includes(CURRENCY_BITCOIN) | ||
let isAnyETH = (from, to) => [from, to].includes(CURRENCY_ETHEREUM) | ||
const httpOpts = { | ||
json: true | ||
} | ||
module.exports = (opts) => { | ||
if (opts.amount === void 0) { | ||
opts.amount = 1; | ||
} | ||
let { | ||
amount = 1, | ||
from = 'USD', | ||
to = CURRENCY_BITCOIN | ||
} = opts | ||
if (opts.from === void 0) { | ||
opts.from = 'usd'; | ||
let base = from | ||
let promises = [] | ||
const anyBTC = isAnyBTC(from, to) | ||
const anyETH = isAnyETH(from, to) | ||
if (anyBTC) { | ||
base = (from === CURRENCY_BITCOIN) ? to : from | ||
promises.push(got(BLOCKCHAIN_URL, httpOpts)) | ||
} | ||
if (opts.to === void 0) { | ||
opts.to = 'dkk'; | ||
if (anyETH) { | ||
// always default base to USD when dealing with Etherum | ||
base = 'USD' | ||
promises.push(got(ETHERCHAIN_URL, httpOpts)) | ||
} | ||
return got(path, {json:true}).then(response => { | ||
money.base = response.body.base; | ||
money.rates = response.body.rates; | ||
promises.unshift(got(`${FIXER_URL}?base=${base}`, httpOpts)) | ||
const converted = money.convert(opts.amount, { | ||
from: opts.from.toUpperCase(), | ||
to: opts.to.toUpperCase() | ||
}); | ||
return Promise.all(promises).then(result => { | ||
let [fixer] = result | ||
return Number(converted.toFixed(3)); | ||
}); | ||
}; | ||
money.base = fixer.body.base | ||
money.rates = fixer.body.rates | ||
let conversionOpts = { | ||
from, | ||
to | ||
} | ||
if (anyBTC) { | ||
let blockchain = result.find(r => r.body.hasOwnProperty(base)) | ||
Object.assign(money.rates, { | ||
BTC: blockchain.body[base].last | ||
}) | ||
} | ||
if (anyETH) { | ||
let etherchain = result.find(r => r.body.hasOwnProperty('data') && r.body.status === 1) | ||
let {usd} = etherchain.body.data[etherchain.body.data.length - 1] | ||
let ethTo = to === CURRENCY_ETHEREUM ? from : to | ||
let etherumConversionOpts = { | ||
from: 'USD', // always convert from USD | ||
to: ethTo | ||
} | ||
let eth = money.convert(usd, etherumConversionOpts) | ||
// set proper base | ||
money.base = ethTo | ||
Object.assign(money.rates, { | ||
ETH: eth | ||
}) | ||
} | ||
if (anyBTC || anyETH) { | ||
// swap the conversion opts | ||
Object.assign(conversionOpts, { | ||
from: to, | ||
to: from | ||
}) | ||
} | ||
return money.convert(amount, conversionOpts) | ||
}) | ||
} |
{ | ||
"name": "currency", | ||
"version": "3.0.0", | ||
"description": "simple currency conversion cli using openexchangerates.org", | ||
"version": "4.0.0", | ||
"description": "simple currency conversion cli", | ||
"main": "index.js", | ||
@@ -10,3 +10,5 @@ "keywords": [ | ||
"rates", | ||
"convert" | ||
"convert", | ||
"bitcoin", | ||
"etherum" | ||
], | ||
@@ -21,5 +23,5 @@ "license": "MIT", | ||
"dependencies": { | ||
"got": "6.1.1", | ||
"got": "7.1.0", | ||
"money": "0.2.0", | ||
"update-notifier": "0.6.0" | ||
"update-notifier": "2.2.0" | ||
}, | ||
@@ -40,5 +42,5 @@ "author": { | ||
"devDependencies": { | ||
"ava": "0.12.0", | ||
"nock": "7.2.2" | ||
"ava": "0.22.0", | ||
"nock": "9.0.19" | ||
} | ||
} |
# currency [![Build Status](http://img.shields.io/travis/srn/currency.svg?style=flat-square)](https://travis-ci.org/srn/currency) | ||
> simple currency conversion cli using [fixer.io](http://fixer.io/) | ||
> simple currency conversion cli using [fixer.io](http://fixer.io), [blockchain.info](https://blockchain.info) and [etherchain.org](https://etherchain.org) | ||
@@ -11,2 +11,8 @@ ## Install | ||
## Currencies | ||
- All currencies listed in the [European Central Bank](https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html) API | ||
- Bitcoin using [blockchain.info](https://blockchain.info) | ||
- Ethereum using [etherchain.org](https://etherchain.org) | ||
## CLI | ||
@@ -20,9 +26,24 @@ | ||
Example | ||
currency 10 usd dkk | ||
$ currency 10 usd dkk | ||
=> 10 USD = 62.208 DKK | ||
=> 57.75516 | ||
$ currency 1 btc usd | ||
=> 1 BTC = 3746.18 USD | ||
$ currency 500 usd btc | ||
=> 500 USD = 0.13358982579886716 BTC | ||
$ currency 1 eth usd | ||
=> 1 ETH = 282.81 USD | ||
$ currency 500 usd eth | ||
=> 500 USD = 1.767971429581698 ETH | ||
``` | ||
## API | ||
## License | ||
MIT © [Søren Brokær](http://srn.io) |
137
test.js
@@ -1,30 +0,123 @@ | ||
import test from 'ava'; | ||
import nock from 'nock'; | ||
import test from 'ava' | ||
import nock from 'nock' | ||
import currency from './'; | ||
import currency from './' | ||
test.beforeEach(t => { | ||
const exchangeRates = { | ||
base: 'USD', | ||
rates: { | ||
DKK: 5.775516, | ||
MYR: 3.230421 | ||
} | ||
}; | ||
nock('https://api.fixer.io') | ||
.get('/latest?base=USD') | ||
.reply(200, { | ||
base: 'USD', | ||
rates: { | ||
DKK: 6.2208 | ||
} | ||
}) | ||
nock('https://api.fixer.io') | ||
.get('/latest') | ||
.reply(200, exchangeRates); | ||
}); | ||
.get('/latest?base=DKK') | ||
.reply(200, { | ||
base: 'DKK', | ||
rates: { | ||
USD: 0.16075 | ||
} | ||
}) | ||
test('convert 10 usd to dkk', async t => { | ||
var opts = { | ||
amount: 10, | ||
from: 'usd', | ||
to: 'dkk' | ||
}; | ||
nock('https://blockchain.info') | ||
.get('/ticker') | ||
.reply(200, { | ||
"USD" : {"15m" : 3755.12, "last" : 3755.12, "buy" : 3755.29, "sell" : 3754.95, "symbol" : "$"}, | ||
"DKK" : {"15m" : 23383.98, "last" : 23383.98, "buy" : 23385.04, "sell" : 23382.92, "symbol" : "kr"} | ||
}) | ||
const converted = currency(opts); | ||
nock('https://etherchain.org') | ||
.get('/api/statistics/price') | ||
.reply(200, { | ||
"status": 1, | ||
"data": [ | ||
{ | ||
"time": "2017-09-23T16:10:56.000Z", | ||
"usd": 278.91 | ||
} | ||
] | ||
}) | ||
}) | ||
t.is(await converted, 57.755); | ||
}); | ||
test('convert 1 USD to DKK', async t => { | ||
const converted = currency({ | ||
amount: 1, | ||
from: 'USD', | ||
to: 'DKK' | ||
}) | ||
t.is(await converted, 6.2208) | ||
}) | ||
test('convert 1 DKK to USD', async t => { | ||
const converted = currency({ | ||
amount: 1, | ||
from: 'DKK', | ||
to: 'USD' | ||
}) | ||
t.is(await converted, 0.16075) | ||
}) | ||
test('convert 1 BTC to USD', async t => { | ||
const converted = currency({ | ||
amount: 1, | ||
from: 'BTC', | ||
to: 'USD' | ||
}) | ||
t.is(await converted, 3755.12) | ||
}) | ||
test('convert 1 BTC to DKK', async t => { | ||
const converted = currency({ | ||
amount: 1, | ||
from: 'BTC', | ||
to: 'DKK' | ||
}) | ||
t.is(await converted, 23383.98) | ||
}) | ||
test('convert 1 DKK to BTC', async t => { | ||
const converted = currency({ | ||
amount: 1, | ||
from: 'DKK', | ||
to: 'BTC' | ||
}) | ||
t.is(await converted, 0.00004276431984632214) | ||
}) | ||
test('convert 1 ETH to USD', async t => { | ||
const converted = currency({ | ||
amount: 1, | ||
from: 'ETH', | ||
to: 'USD' | ||
}) | ||
t.is(await converted, 278.91) | ||
}) | ||
test('convert 1 ETH to DKK', async t => { | ||
const converted = currency({ | ||
amount: 1, | ||
from: 'ETH', | ||
to: 'DKK' | ||
}) | ||
t.is(await converted, 1735.043328) | ||
}) | ||
test('convert 1 USD to ETH', async t => { | ||
const converted = currency({ | ||
amount: 1, | ||
from: 'USD', | ||
to: 'ETH' | ||
}) | ||
t.is(await converted, 0.0035853859667993255) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7420
212
48
1
+ Addedansi-align@2.0.0(transitive)
+ Addedansi-regex@3.0.1(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedboxen@1.3.0(transitive)
+ Addedcamelcase@4.1.0(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcli-boxes@1.0.0(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addedconfigstore@3.1.5(transitive)
+ Addedcross-spawn@5.1.0(transitive)
+ Addedcrypto-random-string@1.0.0(transitive)
+ Addeddecompress-response@3.3.0(transitive)
+ Addeddot-prop@4.2.1(transitive)
+ Addedexeca@0.7.0(transitive)
+ Addedget-stream@3.0.0(transitive)
+ Addedgot@6.7.17.1.0(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedhas-symbol-support-x@1.4.2(transitive)
+ Addedhas-to-string-tag-x@1.4.1(transitive)
+ Addedimport-lazy@2.1.0(transitive)
+ Addedis-fullwidth-code-point@2.0.0(transitive)
+ Addedis-obj@1.0.1(transitive)
+ Addedis-object@1.0.2(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedisurl@1.0.0(transitive)
+ Addedlatest-version@3.1.0(transitive)
+ Addedlru-cache@4.1.5(transitive)
+ Addedmake-dir@1.3.0(transitive)
+ Addedmimic-response@1.0.1(transitive)
+ Addednpm-run-path@2.0.2(transitive)
+ Addedp-cancelable@0.3.0(transitive)
+ Addedp-finally@1.0.0(transitive)
+ Addedp-timeout@1.2.1(transitive)
+ Addedpackage-json@4.0.1(transitive)
+ Addedpath-key@2.0.1(transitive)
+ Addedpify@3.0.0(transitive)
+ Addedpseudomap@1.0.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedshebang-command@1.2.0(transitive)
+ Addedshebang-regex@1.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedstring-width@2.1.1(transitive)
+ Addedstrip-ansi@4.0.0(transitive)
+ Addedstrip-eof@1.0.0(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedterm-size@1.2.0(transitive)
+ Addedtimed-out@4.0.1(transitive)
+ Addedunique-string@1.0.0(transitive)
+ Addedunzip-response@2.0.1(transitive)
+ Addedupdate-notifier@2.2.0(transitive)
+ Addedurl-to-options@1.0.1(transitive)
+ Addedwhich@1.3.1(transitive)
+ Addedwidest-line@2.0.1(transitive)
+ Addedwrite-file-atomic@2.4.3(transitive)
+ Addedxdg-basedir@3.0.0(transitive)
+ Addedyallist@2.1.2(transitive)
- Removedconfigstore@1.4.0(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedduplexer2@0.1.4(transitive)
- Removederror-ex@1.3.2(transitive)
- Removedget-stream@1.1.0(transitive)
- Removedgot@5.7.16.1.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-arrayish@0.2.1(transitive)
- Removedis-finite@1.1.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedlatest-version@2.0.0(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removednode-status-codes@1.0.02.0.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedos-homedir@1.0.2(transitive)
- Removedos-tmpdir@1.0.2(transitive)
- Removedosenv@0.1.5(transitive)
- Removedpackage-json@2.4.0(transitive)
- Removedparse-json@2.2.0(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedread-all-stream@3.1.0(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedrepeating@2.0.1(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedslide@1.1.6(transitive)
- Removedstring-length@1.0.1(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedtimed-out@2.0.03.1.3(transitive)
- Removedunzip-response@1.0.2(transitive)
- Removedupdate-notifier@0.6.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removeduuid@2.0.3(transitive)
- Removedwrite-file-atomic@1.3.4(transitive)
- Removedxdg-basedir@2.0.0(transitive)
Updatedgot@7.1.0
Updatedupdate-notifier@2.2.0