From b7c73fc964ac052052e251ca4ed984d63ae518e6 Mon Sep 17 00:00:00 2001 From: "Jyotirmoy Bandyopadhyaya [Bravo68]" Date: Mon, 28 Nov 2022 13:06:05 +0530 Subject: [PATCH] Completed the assignment --- src/functions/calculate.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/functions/calculate.js b/src/functions/calculate.js index 756a0de..5948e65 100644 --- a/src/functions/calculate.js +++ b/src/functions/calculate.js @@ -9,12 +9,9 @@ const { const results = [], amountStore = {}; async function calculate(file, tokens, date) { - let tokenArr = tokens.split(',') - const USDConvStore = await getRates(tokenArr); - - function USDConvStoreFn(parentCurreny, value){ - const ans = value / USDConvStore[parentCurreny] - return ans.toFixed(2) + let tokenArr + if(tokens) { + tokenArr = tokens.split(',') } let dateToEpoch @@ -25,6 +22,7 @@ async function calculate(file, tokens, date) { fs.createReadStream(file) .pipe(csv()) .on('data', (data) => { + if(dateToEpoch){ if(data.timestamp > dateToEpoch){ results.push(data); @@ -36,8 +34,17 @@ async function calculate(file, tokens, date) { results.push(data); } }) - .on('end', () => { + .on('end', async () => { const uniqueCoins = [...new Set(results.map(item => item.token))]; + if(!tokens){ + tokenArr = uniqueCoins + } + const USDConvStore = await getRates(tokenArr); + + function USDConvStoreFn(parentCurreny, value){ + const ans = value / USDConvStore[parentCurreny] + return ans.toFixed(2) + } uniqueCoins.forEach(coin => { amountStore[coin] = 0