diff --git a/README.md b/README.md index c24fe55..bf496b2 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Usage ### `price()` -`price(fsym, tsyms, [tryConversion])` +`price(fsym, tsyms[, tryConversion])` - `fsym` (String) From Symbol - `tsym` (Array of Strings | String) To Symbol(s) @@ -55,11 +55,12 @@ cc.price('BTC', 'USD') ### priceHistorical -`priceHistorical(fsym, tsyms, time)` +`priceHistorical(fsym, tsyms, time[, tryConversion])` - `fsym` (String) From Symbol - `tsym` (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. ```js const cc = require('cryptocompare') diff --git a/index.js b/index.js index a816e23..6cc4a0e 100644 --- a/index.js +++ b/index.js @@ -18,10 +18,11 @@ function price (fsym, tsyms, tryConversion) { return fetchJSON(url) } -function priceHistorical (fsym, tsyms, time) { +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) let url = `${baseUrl}pricehistorical?fsym=${fsym}&tsyms=${tsyms}&ts=${time}` + if (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 6b7d7be..3076332 100644 --- a/test/cryptocompare.test.js +++ b/test/cryptocompare.test.js @@ -23,10 +23,11 @@ test('price() allows passing a string as the second parameter', t => { }) test("price()'s tryConversion=false works", t => { + t.plan(1) cc.price('LTD', 'USD', false).then(prices => { t.end('Promise should not resolve') }).catch(e => { - t.ok(e, 'Converting LTD to USD fails') + t.ok(e.match(/market does not exist/i), 'Converting LTD to USD fails') t.end() }).catch(t.end) }) @@ -43,6 +44,18 @@ test('priceHistorical()', t => { }).catch(t.end) }) +test("priceHistorical()'s tryConversion=false works", t => { + t.plan(1) + // NOTE: Historical values are hard-coded into this test + const timestamp = new Date('2017-01-01') + cc.priceHistorical('LTD', 'USD', timestamp, false).then(prices => { + t.end('Promise should not resolve') + }).catch(e => { + t.ok(e.match(/market does not exist/i), 'Converting LTD to USD fails') + t.end() + }).catch(t.end) +}) + test('error handling', t => { cc.price('BTC').then(prices => { t.end('Promise should not resolve')