Browse Source

Merge pull request #82 from wicz/handle-json-null

Treat JSON null as empty value in unparse
pull/93/head
Matt Holt 11 years ago
parent
commit
a0991ad6b0
  1. 14
      papaparse.js
  2. 7
      tests/test-cases.js

14
papaparse.js

@ -364,7 +364,7 @@
// Encloses a value around quotes if needed (makes a value safe for CSV insertion) // Encloses a value around quotes if needed (makes a value safe for CSV insertion)
function safe(str, col) function safe(str, col)
{ {
if (typeof str === "undefined") if (typeof str === "undefined" || str === null)
return ""; return "";
str = str.toString().replace(/"/g, '""'); str = str.toString().replace(/"/g, '""');
@ -501,7 +501,7 @@
config.chunk(results); // TODO: Implement abort? (like step) config.chunk(results); // TODO: Implement abort? (like step)
results = undefined; results = undefined;
} }
if (finishedWithEntireFile && isFunction(config.complete)) if (finishedWithEntireFile && isFunction(config.complete))
config.complete(results); config.complete(results);
else if (results && results.meta.aborted && isFunction(config.complete)) else if (results && results.meta.aborted && isFunction(config.complete))
@ -545,7 +545,7 @@
config = config || {}; config = config || {};
if (!config.chunkSize) if (!config.chunkSize)
config.chunkSize = Papa.LocalChunkSize; config.chunkSize = Papa.LocalChunkSize;
var start = 0; var start = 0;
var aggregate = ""; var aggregate = "";
var partialLine = ""; var partialLine = "";
@ -633,7 +633,7 @@
config.chunk(results, file); config.chunk(results, file);
results = undefined; results = undefined;
} }
if (finishedWithEntireFile && isFunction(config.complete)) if (finishedWithEntireFile && isFunction(config.complete))
config.complete(undefined, file); config.complete(undefined, file);
else if (results && results.meta.aborted && isFunction(config.complete)) // TODO: Abort needs reworking like pause/resume need it (if streaming, no results object is returned, so it has no meta to say aborted: true...) else if (results && results.meta.aborted && isFunction(config.complete)) // TODO: Abort needs reworking like pause/resume need it (if streaming, no results object is returned, so it has no meta to say aborted: true...)
@ -948,7 +948,7 @@
if (_aborted) break; if (_aborted) break;
if (_preview > 0 && _runningRowIdx >= _preview) break; if (_preview > 0 && _runningRowIdx >= _preview) break;
if (_paused) return finishParsing(); if (_paused) return finishParsing();
if (_ch == '"') if (_ch == '"')
parseQuotes(); parseQuotes();
else if (_inQuotes) else if (_inQuotes)
@ -1085,7 +1085,7 @@
function twoCharLineBreak(i) function twoCharLineBreak(i)
{ {
return i < _input.length - 1 && return i < _input.length - 1 &&
((_input[i] == "\r" && _input[i+1] == "\n") ((_input[i] == "\r" && _input[i+1] == "\n")
|| (_input[i] == "\n" && _input[i+1] == "\r")) || (_input[i] == "\n" && _input[i+1] == "\r"))
} }
@ -1306,4 +1306,4 @@
{ {
return typeof func === 'function'; return typeof func === 'function';
} }
})(this); })(this);

7
tests/test-cases.js

@ -640,5 +640,10 @@ var UNPARSE_TESTS = [
description: "Mismatched field counts in rows", description: "Mismatched field counts in rows",
input: [['a', 'b', 'c'], ['d', 'e'], ['f']], input: [['a', 'b', 'c'], ['d', 'e'], ['f']],
expected: 'a,b,c\r\nd,e\r\nf' expected: 'a,b,c\r\nd,e\r\nf'
},
{
description: "JSON null is treated as empty value",
input: [{ "Col1": "a", "Col2": null, "Col3": "c" }],
expected: 'Col1,Col2,Col3\r\na,,c'
} }
]; ];

Loading…
Cancel
Save