Skip to content

Instantly share code, notes, and snippets.

@raulpineda
Created March 30, 2020 11:27
Show Gist options
  • Save raulpineda/bf4d8bd12481190876a379b174617f7e to your computer and use it in GitHub Desktop.
Save raulpineda/bf4d8bd12481190876a379b174617f7e to your computer and use it in GitHub Desktop.
const csv = require("csv-parser");
const fs = require("fs");
const parseArgs = require("minimist");
const createCsvWriter = require("csv-writer").createObjectCsvWriter;
const _ = require("lodash");
function printProgress(progress) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(progress);
}
function multiplyCsv() {
const args = parseArgs(process.argv.slice(2));
let csvData = [];
let multipliedData = [];
const { input, n = 10, output } = args;
if (!input) {
throw new Error("CSV input is necessary");
}
if (!output) {
throw new Error("CSV output is necessary");
}
const fileStream = fs
.createReadStream(input)
.pipe(csv())
.on("data", row => {
csvData = [...csvData, row];
});
fileStream.on("end", () => {
if (csvData.length > 0 && n > 0) {
for (let index = 0; index < n; index++) {
printProgress(`${((index / n) * 100).toFixed(4)}% apended`);
multipliedData = [...multipliedData, ..._.shuffle(csvData)];
}
if (multipliedData.length) {
const header = Object.keys(csvData[0]).map(key => ({
id: key,
title: key
}));
const csvWriter = createCsvWriter({
path: output,
header
});
process.stdout.write(
`beginning to write ${multipliedData.length} lines`
);
csvWriter
.writeRecords(multipliedData)
.then(() => console.log("The CSV file was written successfully"));
} else {
console.error("multiplied data was empty");
}
} else {
console.error("Input was empty");
}
});
}
multiplyCsv();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment