Skip to content

Commit

Permalink
Add tryConversion option to priceHistorical()
Browse files Browse the repository at this point in the history
- Update docs
- Harden tests
  • Loading branch information
RyanZim committed Feb 22, 2017
1 parent 27512b9 commit d8045f5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
15 changes: 14 additions & 1 deletion test/cryptocompare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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')
Expand Down

0 comments on commit d8045f5

Please sign in to comment.