Browse Source

Allow to specify the columns used for unparse (#632)

pull/647/head
janisdd 6 years ago committed by Sergi Almacellas Abellana
parent
commit
941033094a
  1. 11
      docs/docs.html
  2. 12
      papaparse.js
  3. 7
      tests/test-cases.js

11
docs/docs.html

@ -261,7 +261,8 @@
delimiter: ",", delimiter: ",",
header: true, header: true,
newline: "\r\n", newline: "\r\n",
skipEmptyLines: false, //or 'greedy' skipEmptyLines: false, //or 'greedy',
columns: null //or array of strings
} }
</code></pre> </code></pre>
</div> </div>
@ -324,6 +325,14 @@
If <code>true</code>, lines that are completely empty (those which evaluate to an empty string) will be skipped. If set to <code>'greedy'</code>, lines that don't have any content (those which have only whitespace after parsing) will also be skipped. If <code>true</code>, lines that are completely empty (those which evaluate to an empty string) will be skipped. If set to <code>'greedy'</code>, lines that don't have any content (those which have only whitespace after parsing) will also be skipped.
</td> </td>
</tr> </tr>
<tr>
<td>
<code>columns</code>
</td>
<td>
If <code>data</code> is an array of objects this option can be used to manually specify the keys (columns) you expect in the objects. If not set the keys of the first objects are used as column.
</td>
</tr>
</table> </table>
</div> </div>
<div class="clear"></div> <div class="clear"></div>

12
papaparse.js

@ -276,6 +276,9 @@ License: MIT
/** whether to skip empty lines */ /** whether to skip empty lines */
var _skipEmptyLines = false; var _skipEmptyLines = false;
/** the columns (keys) we expect when we unparse objects */
var _columns = null;
unpackConfig(); unpackConfig();
var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g'); var quoteCharRegex = new RegExp(escapeRegExp(_quoteChar), 'g');
@ -288,7 +291,7 @@ License: MIT
if (!_input.length || Array.isArray(_input[0])) if (!_input.length || Array.isArray(_input[0]))
return serialize(null, _input, _skipEmptyLines); return serialize(null, _input, _skipEmptyLines);
else if (typeof _input[0] === 'object') else if (typeof _input[0] === 'object')
return serialize(objectKeys(_input[0]), _input, _skipEmptyLines); return serialize(_columns || objectKeys(_input[0]), _input, _skipEmptyLines);
} }
else if (typeof _input === 'object') else if (typeof _input === 'object')
{ {
@ -343,6 +346,13 @@ License: MIT
if (typeof _config.header === 'boolean') if (typeof _config.header === 'boolean')
_writeHeader = _config.header; _writeHeader = _config.header;
if (Array.isArray(_config.columns)) {
if (_config.columns.length === 0) throw new Error('Option columns is empty');
_columns = _config.columns;
}
} }

7
tests/test-cases.js

@ -1732,6 +1732,13 @@ var UNPARSE_TESTS = [
input: [{a: null, b: ' '}, {}, {a: '1', b: '2'}], input: [{a: null, b: ' '}, {}, {a: '1', b: '2'}],
config: {skipEmptyLines: 'greedy', header: true}, config: {skipEmptyLines: 'greedy', header: true},
expected: 'a,b\r\n1,2' expected: 'a,b\r\n1,2'
},
{
description: "Column option used to manually specify keys",
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: [{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'
} }
]; ];

Loading…
Cancel
Save