Skip to content

Commit 90a4fab

Browse files
committed
add priceHistorical()
1 parent 1af6495 commit 90a4fab

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ cryptocompare
77

88
[npm-image]: https://img.shields.io/npm/v/cryptocompare.svg?style=flat-square
99
[npm-url]: https://www.npmjs.com/package/cryptocompare
10-
[travis-image]: https://img.shields.io/travis/jprichardson/cryptocompare.svg?style=flat-square
11-
[travis-url]: https://travis-ci.org/jprichardson/cryptocompare
10+
[travis-image]: https://img.shields.io/travis/exodusmovement/cryptocompare.svg?style=flat-square
11+
[travis-url]: https://travis-ci.org/exodusmovement/cryptocompare
1212
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
1313
[standard-url]: http://npm.im/standard
1414

@@ -67,7 +67,11 @@ cc.coinList()
6767
Reference: https://www.cryptocompare.com/api/#-api-data-price-
6868
6969
70-
70+
### priceHistorical
71+
72+
https://www.cryptocompare.com/api/#-api-data-pricehistorical-
73+
74+
7175
7276
7377

index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@ function price (fsym, tsyms, useBtc) {
2424
return fetchJSON(url)
2525
}
2626

27+
function priceHistorical (fsym, tsyms, time) {
28+
if (!Array.isArray(tsyms)) tsyms = [tsyms]
29+
if (!time instanceof Date) throw new Error('time parameter must be an instance of Date.')
30+
time = time.getTime() / 1000
31+
var url = baseUrl + 'price?fsym=' + fsym + '&tsyms=' + tsyms.join(',') + '&ts=' + time
32+
return fetchJSON(url)
33+
}
34+
2735
module.exports = {
2836
coinList,
29-
price
37+
price,
38+
priceHistorical
3039
}

test/cryptocompare.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ test('price()', function (t) {
2121
t.end()
2222
}).catch(t.end)
2323
})
24+
25+
test('priceHistorical()', function (t) {
26+
t.plan(4)
27+
var oneYearAgo = new Date((Date.now() / 1000 - 31536000) * 1000)
28+
cc.priceHistorical('BTC', ['USD', 'CNY'], oneYearAgo).then(prices => {
29+
t.strictEqual(prices[0].Symbol, 'USD', 'prices[0].Symbol === USD')
30+
t.strictEqual(typeof prices[0].Price, 'number', 'prices[0].Price is a number')
31+
t.strictEqual(prices[1].Symbol, 'CNY')
32+
t.strictEqual(typeof prices[1].Price, 'number')
33+
t.end()
34+
}).catch(t.end)
35+
})

0 commit comments

Comments
 (0)