Search code examples
javascriptcasperjspapaparse

How to parse CSV in CasperJS with Papa Parse?


I am trying to load and parse a csv file in CasperJS with papa parse.

This is the code for that task

var casper = require('casper').create({   
    verbose: true, 
    logLevel: 'debug',
    userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:34.0) Gecko/20100101 Firefox/34.0',
    pageSettings: {
        loadImages:  true,
        loadPlugins: false,
        webSecurityEnabled: false     
    },
    clientScript: [
        'jquery-2.1.3.min.js',
        'jquery.csv-0.71.min.js',
        'papaparse.min.js'
        ],

    viewportSize: {
        width: 1440,
        height: 900
    }
});

var fs = require('fs');

casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
});

casper.start();

casper.then(function(){

    var input = fs.read('order4.csv');

    var config = {
        delimiter: "",  // auto-detect
        newline: "",    // auto-detect
        header: true,
        dynamicTyping: false,
        preview: 0,
        encoding: "",
        worker: false,
        comments: false,
        step: undefined,
        complete: undefined,
        error: undefined,
        download: false,
        skipEmptyLines: true,
        chunk: undefined,
        fastMode: undefined
    }

    var a = {'config' : config, 'input' : input}

/*    var result = this.evaluate(function(input) {
            //console.log(input);
            var results = jquery.csv.toObjects(input);
            console.log(results);
            return results;
        },input);*/

    //var result = Papa.parse(a['input'],a['config']);


    var result = this.evaluate(function(a) { 
        console.log('111111111111111111111' + a['input']);
        var results = Papa.parse(a['input'],a['config']);
        console.log('2222222222222222222' + results);

        return results;
    },a);

    this.echo('333333333333333' + result);
});

casper.run();

$input inside evaluate is visible but $results is null as well as $result in the last command.

Is there a better way load and parse a csv file in CasperJS. How can call Papa.parse outside of evaluate() since I just want to load a local csv file.


Solution

  • It seems that Papa Parse works in the outer context, so you don't need evaluate to use it.

    Here is a complete script:

    var fs = require('fs');
    eval(fs.read('papaparse.min.js'));
    
    var config = {
        delimiter: "",  // auto-detect
        newline: "",    // auto-detect
        header: true,
        dynamicTyping: false,
        preview: 0,
        encoding: "",
        worker: false,
        comments: false,
        step: undefined,
        complete: undefined,
        error: undefined,
        download: false,
        skipEmptyLines: true,
        chunk: undefined,
        fastMode: undefined
    };
    
    console.log(JSON.stringify(Papa.parse(fs.read('order4.csv'), config), undefined, 4));
    
    phantom.exit();
    

    Note that eval is not evil in this case if you know that papaparse.min.js actually does what it is supposed to do.