-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
97 lines (90 loc) · 2.16 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const subDays = require('date-fns/sub_days');
const format = require('date-fns/format');
const differenceInDays = require('date-fns/difference_in_days');
const coinWatchlist = {
ADA: 'Cardano',
AE: 'Aeternity',
AMP: 'Synereo',
ARDR: 'Ardor',
ARK: 'Ark',
BCH: 'Bitcoin Cash',
BCN: 'Bytecoin',
BNB: 'Binance Coin',
BTC: 'Bitcoin',
DASH: 'Dash',
DCR: 'Decred',
DGB: 'DigiByte',
DGD: 'DigixDAO',
DOGE: 'Dogecoin',
EOS: 'EOS',
ETC: 'Ethereum Classic',
ETH: 'Ethereum',
GAME: 'GameCredits',
ICX: 'ICON',
KMD: 'Komodo',
LSK: 'Lisk',
LTC: 'Litecoin',
MAID: 'MaidSafeCoin',
MONA: 'MonaCoin',
NEO: 'NEO',
NMC: 'Namecoin',
NXT: 'Nxt',
OMG: 'OmiseGO',
PPC: 'Peercoin',
REP: 'Augur',
STEEM: 'Steem',
STRAT: 'Stratis',
SYS: 'Syscoin',
TRX: 'TRON',
VEN: 'VeChain',
VTC: 'Vertcoin',
WAVES: 'Waves',
XCP: 'Counterparty',
XEM: 'NEM',
XLM: 'Stellar',
XMR: 'Monero',
XRP: 'Ripple',
XVG: 'Verge',
ZEC: 'Zcash',
ZRX: '0x'
};
function getStartDate(date) {
const startDate = subDays(new Date(date), 7);
return format(startDate, 'YYYY-MM-DD');
}
function parsePriceData(data) {
// Filter to find only the coins in our watchlist
const filteredData = data.filter(coin => Object.keys(coinWatchlist).includes(coin.currency));
// Extract and parse the data client needs
const priceData = filteredData.map(coin => {
const coinDatum = {
currency: coin.currency,
name: coinWatchlist[coin.currency],
current: _round(coin.close)
};
// Check if coin price seven days earlier exists
const dateA = new Date(coin.close_timestamp);
const dateB = new Date(coin.open_timestamp);
if (differenceInDays(dateA, dateB) === 7) {
coinDatum.sevenDaysAgo = _round(coin.open);
} else {
coinDatum.sevenDaysAgo = 'N/A';
}
return coinDatum;
});
return priceData;
}
function _round(value, decimals = 2) {
// Round input to standardized number of decimals
if (Math.abs(value) >= 1) {
return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals)
.toFixed(decimals)
.toString();
} else {
return value;
}
}
module.exports = {
getStartDate,
parsePriceData
};