Skip to content

Instantly share code, notes, and snippets.

@lorenzoongithub
Created April 22, 2015 19:59
Show Gist options
  • Save lorenzoongithub/1dbd4556e748bdb830b3 to your computer and use it in GitHub Desktop.
Save lorenzoongithub/1dbd4556e748bdb830b3 to your computer and use it in GitHub Desktop.
papaparse.js
//
// see: papaparse.com
// use cases derived from https://raw.githubusercontent.com/mholt/PapaParse/master/tests/test-cases.js
//
load('http://papaparse.com/resources/js/papaparse.js');
//One row
oj = Papa.parse("A,b,c");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.errors === null) throw "";
// Two rows
oj = Papa.parse("A,b,c\nd,E,f");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "E") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.errors === null) throw "";
// Three rows
oj = Papa.parse("A,b,c\nd,E,f\nG,h,i");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "E") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.data[2] === null) throw "";
if (oj.data[2][0] !== "G") throw "";
if (oj.data[2][1] !== "h") throw "";
if (oj.data[2][2] !== "i") throw "";
if (oj.errors === null) throw "";
// Whitespace at edges of unquoted field
oj = Papa.parse("a, b ,c");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== " b ") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.errors === null) throw "";
// Quoted field
oj = Papa.parse("A,\"B\",C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "B") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Quoted field with extra whitespace on edges
oj = Papa.parse("A,\" B \",C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== " B ") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Quoted field with delimiter
oj = Papa.parse("A,\"B,B\",C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "B,B") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Quoted field with line break
oj = Papa.parse("A,\"B\nB\",C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "B\nB") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Quoted fields with line breaks
oj = Papa.parse("A,\"B\nB\",\"C\nC\nC\"");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "B\nB") throw "";
if (oj.data[0][2] !== "C\nC\nC") throw "";
if (oj.errors === null) throw "";
// Quoted fields at end of row with delimiter and line break
oj = Papa.parse("a,b,\"c,c\nc\"\nd,e,f");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c,c\nc") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "e") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.errors === null) throw "";
// Quoted field with escaped quotes
oj = Papa.parse("A,\"B\"\"B\"\"B\",C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "B\"B\"B") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Quoted field with escaped quotes at boundaries
oj = Papa.parse("A,\"\"\"B\"\"\",C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "\"B\"") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Unquoted field with quotes at end of field
oj = Papa.parse("A,B\",C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "B\"") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Quoted field with quotes around delimiter
oj = Papa.parse("A,\"\"\",\"\"\",C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "\",\"") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Quoted field with quotes on right side of delimiter
oj = Papa.parse("A,\",\"\"\",C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== ",\"") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Quoted field with quotes on left side of delimiter
oj = Papa.parse("A,\"\"\",\",C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "\",") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Quoted field with 5 quotes in a row and a delimiter in there, too
oj = Papa.parse("\"1\",\"cnonce=\"\"\"\",nc=\"\"\"\"\",\"2\"");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "1") throw "";
if (oj.data[0][1] !== "cnonce=\"\",nc=\"\"") throw "";
if (oj.data[0][2] !== "2") throw "";
if (oj.errors === null) throw "";
// Quoted field with whitespace around quotes
oj = Papa.parse("A, \"B\" ,C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== " \"B\" ") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Misplaced quotes in data, not as opening quotes
oj = Papa.parse("A,B \"B\",C");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "A") throw "";
if (oj.data[0][1] !== "B \"B\"") throw "";
if (oj.data[0][2] !== "C") throw "";
if (oj.errors === null) throw "";
// Quoted field has no closing quote
oj = Papa.parse("a,\"b,c\nd,e,f");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b,c\nd,e,f") throw "";
if (oj.errors === null) throw "";
if (oj.errors[0] === null) throw "";
if (oj.errors[0].code !== "MissingQuotes") throw "";
if (oj.errors[0].index !== 3) throw "";
if (oj.errors[0].message !== "Quoted field unterminated") throw "";
if (oj.errors[0].row !== 0) throw "";
if (oj.errors[0].type !== "Quotes") throw "";
// Line starts with quoted field
oj = Papa.parse("a,b,c\n\"d\",e,f");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "e") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.errors === null) throw "";
// Line ends with quoted field
oj = Papa.parse("a,b,c\nd,e,f\n\"g\",\"h\",\"i\"\n\"j\",\"k\",\"l\"");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "e") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.data[2] === null) throw "";
if (oj.data[2][0] !== "g") throw "";
if (oj.data[2][1] !== "h") throw "";
if (oj.data[2][2] !== "i") throw "";
if (oj.data[3] === null) throw "";
if (oj.data[3][0] !== "j") throw "";
if (oj.data[3][1] !== "k") throw "";
if (oj.data[3][2] !== "l") throw "";
if (oj.errors === null) throw "";
// Quoted field at end of row (but not at EOF) has quotes
oj = Papa.parse("a,b,\"c\"\"c\"\"\"\nd,e,f");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c\"c\"") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "e") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.errors === null) throw "";
// Multiple consecutive empty fields
oj = Papa.parse("a,b,,,c,d\n,,e,,,f");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "") throw "";
if (oj.data[0][3] !== "") throw "";
if (oj.data[0][4] !== "c") throw "";
if (oj.data[0][5] !== "d") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "") throw "";
if (oj.data[1][1] !== "") throw "";
if (oj.data[1][2] !== "e") throw "";
if (oj.data[1][3] !== "") throw "";
if (oj.data[1][4] !== "") throw "";
if (oj.data[1][5] !== "f") throw "";
if (oj.errors === null) throw "";
// Empty input string
oj = Papa.parse("");
if (oj.data === null) throw "";
if (oj.errors === null) throw "";
// Input is just the delimiter (2 empty fields)
oj = Papa.parse(",");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "") throw "";
if (oj.data[0][1] !== "") throw "";
if (oj.errors === null) throw "";
// Input is just empty fields
oj = Papa.parse(",,\n,,,");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "") throw "";
if (oj.data[0][1] !== "") throw "";
if (oj.data[0][2] !== "") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "") throw "";
if (oj.data[1][1] !== "") throw "";
if (oj.data[1][2] !== "") throw "";
if (oj.data[1][3] !== "") throw "";
if (oj.errors === null) throw "";
// Input is just a string (a single field)
oj = Papa.parse("Abc def");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "Abc def") throw "";
if (oj.errors === null) throw "";
// Commented line at beginning
oj = Papa.parse("# Comment!\na,b,c",{"comments":true});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.errors === null) throw "";
// Commented line in middle
oj = Papa.parse("a,b,c\n# Comment\nd,e,f",{"comments":true});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "e") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.errors === null) throw "";
// Commented line at end
oj = Papa.parse("a,true,false\n# Comment",{"comments":true});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "true") throw "";
if (oj.data[0][2] !== "false") throw "";
if (oj.errors === null) throw "";
// Two comment lines consecutively
oj = Papa.parse("a,b,c\n#comment1\n#comment2\nd,e,f",{"comments":true});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "e") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.errors === null) throw "";
// Two comment lines consecutively at end of file
oj = Papa.parse("a,b,c\n#comment1\n#comment2",{"comments":true});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.errors === null) throw "";
// Three comment lines consecutively at beginning of file
oj = Papa.parse("#comment1\n#comment2\n#comment3\na,b,c",{"comments":true});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.errors === null) throw "";
// Entire file is comment lines
oj = Papa.parse("#comment1\n#comment2\n#comment3",{"comments":true});
if (oj.data === null) throw "";
if (oj.errors === null) throw "";
// Comment with non-default character
oj = Papa.parse("a,b,c\n!Comment goes here\nd,e,f",{"comments":"!"});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "e") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.errors === null) throw "";
// Bad comments value specified
oj = Papa.parse("a,b,c\n5comment\nd,e,f",{"comments":5});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "5comment") throw "";
if (oj.data[2] === null) throw "";
if (oj.data[2][0] !== "d") throw "";
if (oj.data[2][1] !== "e") throw "";
if (oj.data[2][2] !== "f") throw "";
if (oj.errors === null) throw "";
// Multi-character comment string
oj = Papa.parse("a,b,c\n=N(Comment)\nd,e,f",{"comments":"=N("});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "e") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.errors === null) throw "";
// Input with only a commented line
oj = Papa.parse("#commented line",{"comments":true,"delimiter":","});
if (oj.data === null) throw "";
if (oj.errors === null) throw "";
// Input with only a commented line and blank line after
oj = Papa.parse("#commented line\n",{"comments":true,"delimiter":","});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "") throw "";
if (oj.errors === null) throw "";
// Input with only a commented line, without comments enabled
oj = Papa.parse("#commented line",{"delimiter":","});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "#commented line") throw "";
if (oj.errors === null) throw "";
// Input without comments with line starting with whitespace
oj = Papa.parse("a\n b\nc",{"delimiter":","});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== " b") throw "";
if (oj.data[2] === null) throw "";
if (oj.data[2][0] !== "c") throw "";
if (oj.errors === null) throw "";
// Multiple rows, one column (no delimiter found)
oj = Papa.parse("a\nb\nc\nd\ne");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "b") throw "";
if (oj.data[2] === null) throw "";
if (oj.data[2][0] !== "c") throw "";
if (oj.data[3] === null) throw "";
if (oj.data[3][0] !== "d") throw "";
if (oj.data[4] === null) throw "";
if (oj.data[4][0] !== "e") throw "";
if (oj.errors === null) throw "";
// One column input with empty fields
oj = Papa.parse("a\nb\n\n\nc\nd\ne\n");
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "b") throw "";
if (oj.data[2] === null) throw "";
if (oj.data[2][0] !== "") throw "";
if (oj.data[3] === null) throw "";
if (oj.data[3][0] !== "") throw "";
if (oj.data[4] === null) throw "";
if (oj.data[4][0] !== "c") throw "";
if (oj.data[5] === null) throw "";
if (oj.data[5][0] !== "d") throw "";
if (oj.data[6] === null) throw "";
if (oj.data[6][0] !== "e") throw "";
if (oj.data[7] === null) throw "";
if (oj.data[7][0] !== "") throw "";
if (oj.errors === null) throw "";
// Fast mode, basic
oj = Papa.parse("a,b,c\nd,e,f",{"fastMode":true});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "e") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.errors === null) throw "";
// Fast mode with comments
oj = Papa.parse("// Commented line\na,b,c",{"fastMode":true,"comments":"//"});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.errors === null) throw "";
// Fast mode with preview
oj = Papa.parse("a,b,c\nd,e,f\nh,j,i\n",{"fastMode":true,"preview":2});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "d") throw "";
if (oj.data[1][1] !== "e") throw "";
if (oj.data[1][2] !== "f") throw "";
if (oj.errors === null) throw "";
// Fast mode with blank line at end
oj = Papa.parse("a,b,c\n",{"fastMode":true});
if (oj.data === null) throw "";
if (oj.data[0] === null) throw "";
if (oj.data[0][0] !== "a") throw "";
if (oj.data[0][1] !== "b") throw "";
if (oj.data[0][2] !== "c") throw "";
if (oj.data[1] === null) throw "";
if (oj.data[1][0] !== "") throw "";
if (oj.errors === null) throw "";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment