-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
65 lines (44 loc) · 2.24 KB
/
index.js
File metadata and controls
65 lines (44 loc) · 2.24 KB
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
var path = require('path');
const fs = require('fs');
const { parse } = require('csv-parse');
const { Parser } = require('json2csv');
const {
validateOrders,
getAverageProductQuantityPurchasedPerOrder,
getMostPopularBrandPerProduct
} = require('./src/orders/orders-manager');
const fileName = process.argv.slice(2)[0];
if (!fileName || path.extname(fileName).toLowerCase() != '.csv') {
console.error('Please pass an argument input file name of extension .csv and ensure it is placed in the data folder.');
return;
}
const orders = [];
fs.createReadStream(`data/${fileName}`)
.on('error', e => console.error([e.message, 'Please ensure the csv file is in data folder.'].join('. ')))
.pipe(parse({ delimiter: ',' }))
.on('data', row => orders.push(row))
.on('end', _ => {
try {
if (validateOrders(orders) === false) {
console.error(`Please ensure input csv file has between 1 and ${Math.pow(10, 4)} rows to process.`);
return;
}
let averageProductQuantityPurchasedPerOrder = getAverageProductQuantityPurchasedPerOrder(orders);
let mostPopularBrandPerProduct = getMostPopularBrandPerProduct(orders);
console.info('Average product quantity per order', Object.fromEntries(averageProductQuantityPurchasedPerOrder));
console.info('Most popular brand per product', Object.fromEntries(mostPopularBrandPerProduct));
const parser = new Parser({ header: false, quote: '' });
const csvAverageProductQuantityPurchasedPerOrder = parser.parse(averageProductQuantityPurchasedPerOrder);
const csvMostPopularBrandPerProduct = parser.parse(mostPopularBrandPerProduct);
fs.writeFile(`output/0_${fileName}`, csvAverageProductQuantityPurchasedPerOrder, error => {
if (error) throw error;
console.info(`0_${fileName} created in the output folder.`)
});
fs.writeFile(`output/1_${fileName}`, csvMostPopularBrandPerProduct, error => {
if (error) throw error;
console.info(`1_${fileName} created in the output folder.`)
});
} catch (error) {
console.error(error);
}
});