From 84a90aed50ec5f1fa71ef30153a9eefa9dd27cee Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 24 Feb 2022 20:39:47 +0100 Subject: [PATCH] fix: columns config works with input type object Closes #918 --- papaparse.js | 7 ++++--- tests/test-cases.js | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/papaparse.js b/papaparse.js index 91de82f..91fa3e0 100755 --- a/papaparse.js +++ b/papaparse.js @@ -306,9 +306,10 @@ License: MIT if (Array.isArray(_input.data)) { - if (!_input.fields) - _input.fields = _input.meta && _input.meta.fields; - + if (!_input.fields) { + _input.fields = _input.meta && _input.meta.fields || _columns; + console.log('input', _input.fields); + } if (!_input.fields) _input.fields = Array.isArray(_input.data[0]) ? _input.fields diff --git a/tests/test-cases.js b/tests/test-cases.js index ab806ba..469c74c 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -1865,6 +1865,13 @@ var UNPARSE_TESTS = [ config: {columns: ['a', 'b', 'c']}, expected: 'a,b,c\r\n1,2,\r\n\r\n3,,4' }, + { + description: "Column option used to manually specify keys with input type object", + notes: "Should not throw any error when attempting to serialize key not present in object. Columns are different than keys of the first object. When an object is missing a key then the serialized value should be an empty string.", + input: { data: [{a: 1, b: '2'}, {}, {a: 3, d: 'd', c: 4,}] }, + config: {columns: ['a', 'b', 'c']}, + expected: 'a,b,c\r\n1,2,\r\n\r\n3,,4' + }, { description: "Use different escapeChar", input: [{a: 'foo', b: '"quoted"'}],