Skip to content

Instantly share code, notes, and snippets.

@dalaidunc
Last active February 14, 2019 09:28
Show Gist options
  • Save dalaidunc/7cb92a168a8dea4f86158dc41e84b872 to your computer and use it in GitHub Desktop.
Save dalaidunc/7cb92a168a8dea4f86158dc41e84b872 to your computer and use it in GitHub Desktop.
Create CSV files with dummy data (using node.js)
const fs = require('fs');
const cols = 8;
function randomChar () {
return String.fromCharCode(Math.floor(Math.random() * (122-65) + 65));
}
function randomString (length) {
let string = '';
for (let i = 0; i < length; i++) {
string += randomChar();
}
return string;
}
function randomRow () {
const cells = [];
for (let i = 0; i < cols; i++) {
cells.push(randomString(20));
}
return cells.join(',') + '\n';
}
async function writeRow (stream) {
return new Promise((resolve, reject) => {
stream.write(randomRow(), 'utf8', () => resolve());
});
}
async function createCSVs (sizes) {
sizes.forEach(async size => {
const stream = fs.createWriteStream(`dummy${size}.csv`);
for (let i = 0; i < size; i++) {
await writeRow(stream);
}
stream.end();
});
}
createCSVs([1000, 10000, 100000, 1000000]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment