From 6d21cbc11a079aa489f69e505581ba808bc3ddc9 Mon Sep 17 00:00:00 2001 From: Vinicius Horewicz Date: Thu, 28 Aug 2014 15:41:37 +0200 Subject: [PATCH] Treat JSON null as empty value --- papaparse.js | 14 +++++++------- tests/test-cases.js | 7 ++++++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/papaparse.js b/papaparse.js index fe11b81..8666e93 100644 --- a/papaparse.js +++ b/papaparse.js @@ -364,7 +364,7 @@ // Encloses a value around quotes if needed (makes a value safe for CSV insertion) function safe(str, col) { - if (typeof str === "undefined") + if (typeof str === "undefined" || str === null) return ""; str = str.toString().replace(/"/g, '""'); @@ -501,7 +501,7 @@ config.chunk(results); // TODO: Implement abort? (like step) results = undefined; } - + if (finishedWithEntireFile && isFunction(config.complete)) config.complete(results); else if (results && results.meta.aborted && isFunction(config.complete)) @@ -545,7 +545,7 @@ config = config || {}; if (!config.chunkSize) config.chunkSize = Papa.LocalChunkSize; - + var start = 0; var aggregate = ""; var partialLine = ""; @@ -633,7 +633,7 @@ config.chunk(results, file); results = undefined; } - + if (finishedWithEntireFile && isFunction(config.complete)) 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...) @@ -948,7 +948,7 @@ if (_aborted) break; if (_preview > 0 && _runningRowIdx >= _preview) break; if (_paused) return finishParsing(); - + if (_ch == '"') parseQuotes(); else if (_inQuotes) @@ -1085,7 +1085,7 @@ function twoCharLineBreak(i) { - return i < _input.length - 1 && + return i < _input.length - 1 && ((_input[i] == "\r" && _input[i+1] == "\n") || (_input[i] == "\n" && _input[i+1] == "\r")) } @@ -1306,4 +1306,4 @@ { return typeof func === 'function'; } -})(this); \ No newline at end of file +})(this); diff --git a/tests/test-cases.js b/tests/test-cases.js index 86734cc..4811c62 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -640,5 +640,10 @@ var UNPARSE_TESTS = [ description: "Mismatched field counts in rows", input: [['a', 'b', 'c'], ['d', 'e'], ['f']], 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' } -]; \ No newline at end of file +];