From 4132d810abbc7e3699259f2314f627f32ba981a6 Mon Sep 17 00:00:00 2001 From: Simon <33730997+TheSlimvReal@users.noreply.github.com> Date: Tue, 15 Mar 2022 09:29:51 +0100 Subject: [PATCH] fixcolumns config works with input type object (#919) Closes #918 --- papaparse.js | 2 +- tests/test-cases.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/papaparse.js b/papaparse.js index df7ba06..667a109 100755 --- a/papaparse.js +++ b/papaparse.js @@ -307,7 +307,7 @@ License: MIT if (Array.isArray(_input.data)) { if (!_input.fields) - _input.fields = _input.meta && _input.meta.fields; + _input.fields = _input.meta && _input.meta.fields || _columns; if (!_input.fields) _input.fields = Array.isArray(_input.data[0]) 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"'}],