From 04fa44bc7e8cf6b1f0a7b73f8802863f45d8e0f3 Mon Sep 17 00:00:00 2001 From: RyanZim Date: Wed, 22 Feb 2017 10:00:05 -0500 Subject: [PATCH] Add priceMulti() --- CHANGELOG.md | 1 + README.md | 31 +++++++++++++++++++++++++++++++ index.js | 7 +++++++ test/cryptocompare.test.js | 19 +++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e233aa..27eb078 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Switched to https://min-api.cryptocompare.com/ - removed: `coinList()` - changed: `price()` returns a different data structure - changed: `priceHistorical()` returns a different data structure +- added: `priceMulti()` 0.0.2 / 2016-11-21 ------------------ diff --git a/README.md b/README.md index bf496b2..4bf3fdd 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,37 @@ cc.price('BTC', 'USD') .catch(console.error) ``` +### `priceMulti()` + +Works like `price()`, except it allows you to specify a matrix of From Symbols. + +`priceMulti(fsym, tsyms[, tryConversion])` + +- `fsyms` (Array of Strings | String) From Symbol(s) +- `tsyms` (Array of Strings | String) To Symbol(s) +- `tryConversion` (Boolean) By default, if the crypto does not trade directly into the toSymbol requested, BTC will be used for conversion. Set `tryConversion` to `false` to disable using BTC for conversion. + +```js +const cc = require('cryptocompare') + +// Basic Usage: +cc.priceMulti(['BTC', 'ETH'], ['USD', 'EUR']) +.then(prices => { + console.log(prices) + // -> { BTC: { USD: 1114.63, EUR: 1055.82 }, + // ETH: { USD: 12.74, EUR: 12.06 } } +}) +.catch(console.error) + +// Passing a single pair of currencies: +cc.priceMulti('BTC', 'USD') +.then(prices => { + console.log(prices) + // -> { BTC: { USD: 1114.63 } } +}) +.catch(console.error) +``` + ### priceHistorical `priceHistorical(fsym, tsyms, time[, tryConversion])` diff --git a/index.js b/index.js index 6cc4a0e..0ad9251 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,12 @@ function price (fsym, tsyms, tryConversion) { return fetchJSON(url) } +function priceMulti (fsyms, tsyms, tryConversion) { + let url = `${baseUrl}pricemulti?fsyms=${fsyms}&tsyms=${tsyms}` + if (tryConversion === false) url += '&tryConversion=false' + return fetchJSON(url) +} + function priceHistorical (fsym, tsyms, time, tryConversion) { if (!(time instanceof Date)) throw new Error('time parameter must be an instance of Date.') time = Math.floor(time.getTime() / 1000) @@ -29,5 +35,6 @@ function priceHistorical (fsym, tsyms, time, tryConversion) { module.exports = { price, + priceMulti, priceHistorical } diff --git a/test/cryptocompare.test.js b/test/cryptocompare.test.js index 3076332..705c0ac 100644 --- a/test/cryptocompare.test.js +++ b/test/cryptocompare.test.js @@ -32,6 +32,25 @@ test("price()'s tryConversion=false works", t => { }).catch(t.end) }) +test('priceMulti()', t => { + t.plan(4) + cc.priceMulti(['BTC', 'ETH'], ['USD', 'EUR']).then(prices => { + t.strictEqual(typeof prices.BTC.USD, 'number', 'prices.BTC.USD is a number') + t.strictEqual(typeof prices.BTC.EUR, 'number', 'prices.BTC.EUR is a number') + t.strictEqual(typeof prices.ETH.USD, 'number', 'prices.ETH.USD is a number') + t.strictEqual(typeof prices.ETH.EUR, 'number', 'prices.ETH.EUR is a number') + t.end() + }).catch(t.end) +}) + +test('priceMulti() allows passing strings instead of arrays', t => { + t.plan(1) + cc.priceMulti('BTC', 'USD').then(prices => { + t.strictEqual(typeof prices.BTC.USD, 'number', 'prices.BTC.USD is a number') + t.end() + }).catch(t.end) +}) + test('priceHistorical()', t => { t.plan(3) // NOTE: Historical values are hard-coded into this test