-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate headers in Parser
, based upon presence of _fields
, without changing _input
(fixes issue #985)
#989
base: master
Are you sure you want to change the base?
Changes from 5 commits
27cc377
86a6d27
b84183a
a151558
569c262
80a8a92
34fc5a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -591,34 +591,43 @@ var CORE_PARSER_TESTS = [ | |
input: 'A,A,A,A\n1,2,3,4', | ||
config: { header: true }, | ||
expected: { | ||
data: [['A', 'A_1', 'A_2', 'A_3'], ['1', '2', '3', '4']], | ||
errors: [] | ||
data: [['1', '2', '3', '4']], | ||
errors: [], | ||
meta: { | ||
fields: ['A', 'A_1', 'A_2', 'A_3'] | ||
} | ||
} | ||
}, | ||
{ | ||
description: "Duplicate header names with headerTransform", | ||
input: 'A,A,A,A\n1,2,3,4', | ||
config: { header: true, transformHeader: function(header) { return header.toLowerCase(); } }, | ||
expected: { | ||
data: [['a', 'a_1', 'a_2', 'a_3'], ['1', '2', '3', '4']], | ||
errors: [] | ||
data: [['1', '2', '3', '4']], | ||
errors: [], | ||
meta: { | ||
fields: ['a', 'a_1', 'a_2', 'a_3'] | ||
} | ||
} | ||
}, | ||
{ | ||
description: "Duplicate header names existing column", | ||
input: 'c,c,c,c_1\n1,2,3,4', | ||
config: { header: true }, | ||
expected: { | ||
data: [['c', 'c_1', 'c_2', 'c_1_0'], ['1', '2', '3', '4']], | ||
errors: [] | ||
data: [['1', '2', '3', '4']], | ||
errors: [], | ||
meta: { | ||
fields: ['c', 'c_1', 'c_2', 'c_1_0'] | ||
} | ||
} | ||
}, | ||
]; | ||
|
||
describe('Core Parser Tests', function() { | ||
function generateTest(test) { | ||
(test.disabled ? it.skip : it)(test.description, function() { | ||
var actual = new Papa.Parser(test.config).parse(test.input); | ||
var actual = new Papa.Parser(test.config, []).parse(test.input); | ||
assert.deepEqual(actual.errors, test.expected.errors); | ||
assert.deepEqual(actual.data, test.expected.data); | ||
}); | ||
|
@@ -2018,6 +2027,51 @@ describe('Unparse Tests', function() { | |
|
||
|
||
var CUSTOM_TESTS = [ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add new tests at the end? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup! Done. |
||
description: "Pause and resume works with headers and duplicate fields (Regression Test for Bug #985)", | ||
expected: [[ | ||
["Column 1", "Column 2", "Column 3", "Column 4", "Column 5"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to have 5 rows? Can we make the test simpler with less rows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not. I reduced it to two rows and four columns, enough to make sure we could test column value duplicates and that the headers didn't shift between pause/resume stages. |
||
["Column 1", "Column 2", "Column 3", "Column 4", "Column 5"], | ||
["Column 1", "Column 2", "Column 3", "Column 4", "Column 5"], | ||
["Column 1", "Column 2", "Column 3", "Column 4", "Column 5"] | ||
], [ | ||
{ "Column 1": "R1C1", "Column 2": "", "Column 3": "R1C3", "Column 4": "", "Column 5": "" }, | ||
{ "Column 1": "R2C1", "Column 2": "", "Column 3": "", "Column 4": "", "Column 5": "" }, | ||
{ "Column 1": "R3C1", "Column 2": "", "Column 3": "", "Column 4": "R3C4", "Column 5": "" }, | ||
{ "Column 1": "R4C1", "Column 2": "", "Column 3": "", "Column 4": "", "Column 5": "" }, | ||
]], | ||
run: function(callback) { | ||
var inputString = [ | ||
"Column 1,Column 2,Column 3,Column 4,Column 5", | ||
"R1C1,,R1C3,,", | ||
"R2C1,,,,", | ||
"R3C1,,,R3C4,", | ||
"R4C1,,,," | ||
].join("\n"); | ||
var output = []; | ||
var dataRows = []; | ||
var headerResults = []; | ||
Papa.parse(inputString, { | ||
header: true, | ||
step: function(results, parser) { | ||
if (results) | ||
{ | ||
headerResults.push(results.meta.fields); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why pusshing headers multiple times? Won't be better to just push when empty? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to double-check that multiple passes returned the same headers. It looked like the original code could have updated the headers on each pass, which seemed problematic. Perhaps that question makes sense as a separate test, or do you think it isn't worth the check? |
||
parser.pause(); | ||
parser.resume(); | ||
if (results.data) { | ||
dataRows.push(results.data); | ||
} | ||
} | ||
}, | ||
complete: function() { | ||
output.push(headerResults); | ||
output.push(dataRows); | ||
callback(output); | ||
} | ||
}); | ||
} | ||
}, | ||
{ | ||
description: "Pause and resume works (Regression Test for Bug #636)", | ||
disabled: !XHR_ENABLED, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes to existing tests were required to reflect that:
Parser
now requires an array as its second argument for its constructorParser.parse()
no longer returns the header row as data