Skip to content

Commit

Permalink
Use ES6 & add 'use strict'
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Feb 22, 2017
1 parent c5a5fb1 commit 47f22cc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
/* global fetch */

var baseUrl = 'https://www.cryptocompare.com/api/data/'
const baseUrl = 'https://www.cryptocompare.com/api/data/'

function fetchJSON (url) {
return fetch(url)
Expand All @@ -13,22 +14,20 @@ function fetchJSON (url) {
}

function coinList () {
var url = baseUrl + 'coinlist/'
let url = `${baseUrl}coinlist/`
return fetchJSON(url)
}

function price (fsym, tsyms, useBtc) {
if (!Array.isArray(tsyms)) tsyms = [tsyms]
var url = baseUrl + 'price?fsym=' + fsym + '&tsyms=' + tsyms.join(',')
let url = `${baseUrl}price?fsym=${fsym}&tsyms=${tsyms}`
if (useBtc) url += 'usebtc=true'
return fetchJSON(url)
}

function priceHistorical (fsym, tsyms, time) {
if (!Array.isArray(tsyms)) tsyms = [tsyms]
if (!(time instanceof Date)) throw new Error('time parameter must be an instance of Date.')
time = Math.floor(time.getTime() / 1000)
var url = baseUrl + 'pricehistorical?fsym=' + fsym + '&tsyms=' + tsyms.join(',') + '&ts=' + time
let url = `${baseUrl}pricehistorical?fsym=${fsym}&tsyms=${tsyms}&ts=${time}`
return fetchJSON(url)
}

Expand Down
13 changes: 7 additions & 6 deletions test/cryptocompare.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
var test = require('tape')
'use strict'
const test = require('tape')

// set to global
global.fetch = require('node-fetch')
var cc = require('../')
const cc = require('../')

test('coinList()', function (t) {
test('coinList()', t => {
t.plan(1)
cc.coinList().then(coinList => {
t.strictEqual(coinList.BTC.CoinName, 'Bitcoin', 'CoinName field should be Bitcoin')
}).catch(t.end)
})

test('price()', function (t) {
test('price()', t => {
t.plan(4)
cc.price('BTC', ['USD', 'CNY']).then(prices => {
t.strictEqual(prices[0].Symbol, 'USD', 'prices[0].Symbol === USD')
Expand All @@ -22,10 +23,10 @@ test('price()', function (t) {
}).catch(t.end)
})

test('priceHistorical()', function (t) {
test('priceHistorical()', t => {
t.plan(5)
// Arbitrary timestamp; historical values are hard-coded into this test
var timestamp = new Date(1456149600 * 1000)
const timestamp = new Date(1456149600 * 1000)
cc.priceHistorical('BTC', ['USD', 'CNY'], timestamp).then(prices => {
t.strictEqual(prices[0].Symbol, 'USD', 'prices[0].Symbol === USD')
t.strictEqual(typeof prices[0].Price, 'number', 'prices[0].Price is a number')
Expand Down

0 comments on commit 47f22cc

Please sign in to comment.