-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
42 lines (37 loc) · 970 Bytes
/
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
'use strict';
const condense = require('no-whitespace');
const ccompare = require('cryptocompare');
global.fetch = require('node-fetch');
module.exports = (from, to) => {
if (typeof from !== 'string' || typeof to !== 'string') {
throw new TypeError(`ccoin expects two string parameters, got ${typeof from}, ${typeof to}`);
}
const arrify = str => {
return str.split(',').map(x => condense(x.toUpperCase()));
};
const f = arrify(from);
const t = arrify(to);
return Promise.all(f.map(fromSymbol => {
return ccompare.price(fromSymbol, t)
.then(prices => {
const results = {
from: fromSymbol,
to: []
};
Object.keys(prices).forEach(symbol => {
if (symbol !== fromSymbol) {
results.to.push(`${symbol}: ${prices[symbol]}`);
}
});
return results;
});
}))
.then(results => {
// console.log('results', results);
const final = {};
results.forEach(obj => {
final[obj.from] = obj.to;
});
return final;
});
};