PapaParse
PapaParse copied to clipboard
[Feature request] unparse's header parameter does not force header row for empty data array
Issue
I've got a case where I want the result from unparse to include a header row even if the provided data array is empty.
// The following returns an empty string when I need it to return "foo,bar".
unparse([], {
columns: ['foo', 'bar'],
header: true,
})
Current workaround
I've found that if I provide an array with an empty object, I can get my desired result.
// The following returns my desired result of "foo,bar".
unparse([{}], {
columns: ['foo', 'bar'],
header: true,
})
Suggested solution
Since this is likely a niche use case and an empty-string may be the desired result for other use cases under this implementation, I don't think it would make sense for header: true to force a header row in the case of an empty data array. My proposal is that we add the alternative value of 'force for header to achieve this particular case.
// The following would return the result of "foo,bar".
unparse([], {
columns: ['foo', 'bar'],
header: 'force',
})
Maybe I’m missing something but why don’t you use the plain column array as the result if the data is empty? If the columns contain something that needs to be escaped, use the column as data for unparse?
Somewhat related to #918. It seems like the columns options doesn't really enforce anything at the moment