Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop papaparse streaming after some results

I am using PapaPase to parse big CSV file, using chunk mode.

I am validating csv data, and I want to stop streaming when validation fails.

But I am unable to stop streaming, after some parsing.

I tried to stop using return false from chunk callback, but it's not working.

Below is the code.

$("#fileselect").on("change", function(e){
    if (this.files.length) {
        var file = this.files[0]
        count = 0;
        Papa.parse(file, {
            worker: true,
            delimiter: "~~",
            skipEmptyLines:true,
            chunk: function (result) {
                count += result.data.length;
                console.clear();
                console.log(count);
                if (count>60000) {
                    return false;
                }
            },
            complete: function (result, file) {
                console.log(result)
            }
        });
    }
});
like image 322
Lalitkumar Tarsariya Avatar asked Sep 04 '18 08:09

Lalitkumar Tarsariya


1 Answers

Chunk, and Step, both have access to parser too, you can use that to pause, resume, or (as you might want) abort.

step: function(results, parser) {
console.log("Row data:", results.data);
console.log("Row errors:", results.errors);
}’

So in your instance, you would need to do this (untested):

$("#fileselect").on("change", function(e){
    if (this.files.length) {
        var file = this.files[0]
        count = 0;
        Papa.parse(file, {
            worker: true,
            delimiter: "~~",
            skipEmptyLines:true,
            chunk: function (result, parser) {
                count += result.data.length;
                console.clear();
                console.log(count);
                if (count>60000) {
                    //return false;
                    parser.abort(); // <-- stop streaming
                }
            },
            complete: function (result, file) {
                console.log(result)
            }
        });
    }
});

Take a look at the documentation for step and chunk.

https://www.papaparse.com/docs

Hope this helped!

like image 142
Jon Humphrey Avatar answered Oct 03 '22 05:10

Jon Humphrey