-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (103 loc) · 3.04 KB
/
index.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env node
"use strict";
const Table = require("cli-table");
const chalk = require("chalk");
const CoinMarketCap = require("node-coinmarketcap");
const DEFAULT_CURRENCY = "USD";
const table = new Table({
chars: {
top: "",
"top-mid": "",
"top-left": "",
"top-right": "",
bottom: "",
"bottom-mid": "",
"bottom-left": "",
"bottom-right": "",
left: "",
"left-mid": "",
mid: "",
"mid-mid": "",
right: "",
"right-mid": "",
middle: ""
}
});
const options = {
events: false,
refresh: 0,
convert: DEFAULT_CURRENCY
};
const client = new CoinMarketCap(options);
const formatCurrency = val => {
return Number.parseFloat(val)
.toFixed(2)
.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
};
const formatGrowth = val => {
return val.indexOf("-") === -1 ? chalk.green(val) : chalk.red(val);
};
const formatDefault = val => {
return chalk.white(val);
};
const readableNumber = val => {
const humanReadableNumbers = [ "hundred", "thoushand", "million", "billion"];
if (val === null) return "0"; // if undefined, return a zero
var e = Math.floor(Math.log(val) / Math.log(1000));
return (val/ Math.pow(1000, e)).toFixed(2) + " " + humanReadableNumbers[e]
}
const formatRow = (data = {}) => {
let {
name,
symbol,
rank,
price_usd,
market_cap_usd,
"24h_volume_usd": volume_usd,
available_supply,
percent_change_1h,
percent_change_24h,
percent_change_7d
} = data;
percent_change_1h = formatGrowth(percent_change_1h);
percent_change_24h = formatGrowth(percent_change_24h);
percent_change_7d = formatGrowth(percent_change_7d);
price_usd = Number.parseFloat(price_usd),
market_cap_usd = Number.parseFloat(market_cap_usd),
available_supply = Number.parseFloat(available_supply),
volume_usd = Number.parseFloat(volume_usd);
return [
formatDefault(rank),
formatDefault(name),
formatDefault(symbol),
percent_change_1h,
percent_change_24h,
percent_change_7d,
formatDefault(formatCurrency(price_usd)),
formatDefault(readableNumber(market_cap_usd)),
formatDefault(readableNumber(available_supply)),
formatDefault(readableNumber(volume_usd))
];
};
const run = () => {
client.getTop(10, data => {
table.push(
[
"#",
"Name",
"Symbol",
"% 1h",
"% 24h",
"% 7d",
`Price (${options.convert})`,
`Market Cap (${options.convert})`,
`Circulating Supply (${options.convert})`,
`Volume (24h/${options.convert})`
].map(val => chalk.bold(val))
);
data.map(formatRow).forEach(row => table.push(row));
console.log(table.toString());
});
};
console.log("Getting TOP10 crypto currencies information from CoinMarketCap...");
run();