Browse Source

- empty column option will throw an error

pull/632/head
janisdd 6 years ago
parent
commit
3389a5c8cd
  1. 8
      papaparse.js
  2. 12
      tests/test-cases.js

8
papaparse.js

@ -291,10 +291,7 @@ License: MIT @@ -291,10 +291,7 @@ License: MIT
if (!_input.length || Array.isArray(_input[0]))
return serialize(null, _input, _skipEmptyLines);
else if (typeof _input[0] === 'object')
return serialize(
_columns ? _columns : objectKeys(_input[0]),
_input, _skipEmptyLines
);
return serialize(_columns || objectKeys(_input[0]), _input, _skipEmptyLines);
}
else if (typeof _input === 'object')
{
@ -351,6 +348,9 @@ License: MIT @@ -351,6 +348,9 @@ License: MIT
_writeHeader = _config.header;
if (Array.isArray(_config.columns)) {
if (_config.columns.length === 0) throw new Error('Option columns was empty');
_columns = _config.columns;
}
}

12
tests/test-cases.js

@ -1742,10 +1742,10 @@ var UNPARSE_TESTS = [ @@ -1742,10 +1742,10 @@ var UNPARSE_TESTS = [
},
{
description: "Column option is empty",
notes: "Header (columns) is empty so header is skipped",
notes: "Columns is empty so no data can be generated",
input: [{a: 1, b: '2'}, {}, {a: 3, d: 'd', c: 4,}],
config: {columns: []},
expected: '\r\n\r\n'
expectsError: true
}
];
@ -1754,6 +1754,14 @@ describe('Unparse Tests', function() { @@ -1754,6 +1754,14 @@ describe('Unparse Tests', function() {
(test.disabled ? it.skip : it)(test.description, function() {
var actual;
if (test.expectsError) {
assert.throws(function() {
Papa.unparse(test.input, test.config);
});
return;
}
try {
actual = Papa.unparse(test.input, test.config);
} catch (e) {

Loading…
Cancel
Save