Skip to content

Instantly share code, notes, and snippets.

@iursevla
Last active May 21, 2017 17:27
Show Gist options
  • Save iursevla/0d4c5e38f621aea3fa6d28f184875945 to your computer and use it in GitHub Desktop.
Save iursevla/0d4c5e38f621aea3fa6d28f184875945 to your computer and use it in GitHub Desktop.
CSV process worker
let numMsgsReceived = 0; //Number messages received
let options = null; //Save options
let headerIndeces = []; //Indeces of header to save
/**
* Receive options and file to read from main Thread.
*/
this.onmessage = function (e) {
if (numMsgsReceived === 0) { //Save options
options = e.data;
headerIndeces = options.headerIndeces;
numMsgsReceived++;
console.log(options);
}
else {
let rows = e.data.rows.split(/\r?\n/); //<--- options.delimiter http://stackoverflow.com/a/5035005/5869289
let numRows = rows.length;
if (e.data.ignoreLastRow)
pushRows(rows, numRows - 1)
else
pushRows(rows, numRows);
}
}
function pushRows(rows, numRows) {
let mapa = [];
for (let i = 0; i < numRows; i++) {
let values = rows[i].split(options.delimiter);
let row = []
for (const his of headerIndeces)
row.push(values[his]);
mapa.push(row);
}
self.postMessage(mapa);
delete mapa;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment