Skip to content

Commit

Permalink
Add priceMulti()
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Feb 23, 2017
1 parent d8045f5 commit 04fa44b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])`
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -29,5 +35,6 @@ function priceHistorical (fsym, tsyms, time, tryConversion) {

module.exports = {
price,
priceMulti,
priceHistorical
}
19 changes: 19 additions & 0 deletions test/cryptocompare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 04fa44b

Please sign in to comment.