From ecc6590479e32fb5d18c7718c655f9ea91f91165 Mon Sep 17 00:00:00 2001 From: RyanZim Date: Sat, 25 Feb 2017 17:29:27 -0500 Subject: [PATCH] Replace tryConversion parameter with an options object --- CHANGELOG.md | 1 + README.md | 20 ++++++++++++-------- index.js | 20 ++++++++++++-------- test/cryptocompare.test.js | 14 ++++++++++---- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7f19f0..250e49d 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 +- changed: `price()` and `priceHistorical)()` function signature has changed - added: `priceMulti()` - added: `priceFull()` - added: `topPairs()` diff --git a/README.md b/README.md index 1a8b5c7..68e45ae 100644 --- a/README.md +++ b/README.md @@ -40,11 +40,12 @@ Usage Get the current price of any cryptocurrency in any other currency. -`price(fsym, tsyms[, tryConversion])` +`price(fsym, tsyms[, options])` - `fsym` (String) From Symbol - `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. +- `options` (Object) + - `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') @@ -70,11 +71,12 @@ cc.price('BTC', 'USD') Works like `price()`, except it allows you to specify a matrix of From Symbols. -`priceMulti(fsyms, tsyms[, tryConversion])` +`priceMulti(fsyms, tsyms[, options])` - `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. +- `options` (Object) + - `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') @@ -101,11 +103,12 @@ cc.priceMulti('BTC', 'USD') Get all the current trading info (price, vol, open, high, low, etc.) of any list of cryptocurrencies in any other currency. -`priceFull(fsyms, tsyms[, tryConversion])` +`priceFull(fsyms, tsyms[, options])` - `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. +- `options` (Object) + - `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') @@ -149,12 +152,13 @@ cc.priceFull(['BTC', 'ETH'], ['USD', 'EUR']) Get the price of any cryptocurrency in any other currency at a given timestamp. The price comes from the daily info - so it would be the price at the end of the day GMT based on the requested timestamp. -`priceHistorical(fsym, tsyms, time[, tryConversion])` +`priceHistorical(fsym, tsyms, time[, options])` - `fsym` (String) From Symbol - `tsyms` (Array of Strings | String) To Symbol(s) - `time` (Date) Date in history that you want price data for -- `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. +- `options` (Object) + - `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') diff --git a/index.js b/index.js index 7bb7f8f..84e0e8d 100644 --- a/index.js +++ b/index.js @@ -12,29 +12,33 @@ function fetchJSON (url) { }) } -function price (fsym, tsyms, tryConversion) { +function price (fsym, tsyms, options) { + options = options || {} let url = `${baseUrl}price?fsym=${fsym}&tsyms=${tsyms}` - if (tryConversion === false) url += '&tryConversion=false' + if (options.tryConversion === false) url += '&tryConversion=false' return fetchJSON(url) } -function priceMulti (fsyms, tsyms, tryConversion) { +function priceMulti (fsyms, tsyms, options) { + options = options || {} let url = `${baseUrl}pricemulti?fsyms=${fsyms}&tsyms=${tsyms}` - if (tryConversion === false) url += '&tryConversion=false' + if (options.tryConversion === false) url += '&tryConversion=false' return fetchJSON(url) } -function priceFull (fsyms, tsyms, tryConversion) { +function priceFull (fsyms, tsyms, options) { + options = options || {} let url = `${baseUrl}pricemultifull?fsyms=${fsyms}&tsyms=${tsyms}` - if (tryConversion === false) url += '&tryConversion=false' + if (options.tryConversion === false) url += '&tryConversion=false' // We want the RAW data, not the DISPLAY data: return fetchJSON(url).then(result => result.RAW) } -function priceHistorical (fsym, tsyms, time, tryConversion) { +function priceHistorical (fsym, tsyms, time, options) { + options = options || {} time = dateToTimestamp(time) let url = `${baseUrl}pricehistorical?fsym=${fsym}&tsyms=${tsyms}&ts=${time}` - if (tryConversion === false) url += '&tryConversion=false' + if (options.tryConversion === false) url += '&tryConversion=false' // The API returns json with an extra layer of nesting, so remove it return fetchJSON(url).then(result => result[fsym]) } diff --git a/test/cryptocompare.test.js b/test/cryptocompare.test.js index 6565ab0..dea96bb 100644 --- a/test/cryptocompare.test.js +++ b/test/cryptocompare.test.js @@ -23,7 +23,7 @@ test('price() allows passing a string as the second parameter', t => { }) test("price()'s tryConversion=false works", t => { - helpers.testTryConversion(cc.price(helpers.NOT_USD_TRADABLE, 'USD', false), t) + helpers.testTryConversion(cc.price(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t) }) test('priceMulti()', t => { @@ -46,7 +46,7 @@ test('priceMulti() allows passing strings instead of arrays', t => { }) test("priceMulti()'s tryConversion=false works", t => { - helpers.testTryConversion(cc.priceMulti(helpers.NOT_USD_TRADABLE, 'USD', false), t) + helpers.testTryConversion(cc.priceMulti(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t) }) test('priceFull()', t => { @@ -80,7 +80,7 @@ test('priceFull() allows passing strings instead of arrays', t => { }) test("priceFull()'s tryConversion=false works", t => { - helpers.testTryConversion(cc.priceFull(helpers.NOT_USD_TRADABLE, 'USD', false), t) + helpers.testTryConversion(cc.priceFull(helpers.NOT_USD_TRADABLE, 'USD', { tryConversion: false }), t) }) test('priceHistorical()', t => { @@ -97,7 +97,13 @@ test('priceHistorical()', t => { test("priceHistorical()'s tryConversion=false works", t => { const timestamp = new Date('2017-01-01') - helpers.testTryConversion(cc.priceHistorical(helpers.NOT_USD_TRADABLE, 'USD', timestamp, false), t) + helpers.testTryConversion( + cc.priceHistorical( + helpers.NOT_USD_TRADABLE, + 'USD', + timestamp, + { tryConversion: false } + ), t) }) test('generateAvg()', t => {