From cb68215f705ce5a60622a4a3b00932c96d86982f Mon Sep 17 00:00:00 2001 From: Steve Fisher Date: Wed, 18 Apr 2018 10:28:30 -0400 Subject: [PATCH] Add option to trim whitespace from headers (#491) Allow developers to trim leading/trailing spaces from headers --- papaparse.js | 10 +++++++++- tests/test-cases.js | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/papaparse.js b/papaparse.js index c8b5efa..6506565 100755 --- a/papaparse.js +++ b/papaparse.js @@ -990,7 +990,15 @@ return; for (var i = 0; needsHeaderRow() && i < _results.data.length; i++) for (var j = 0; j < _results.data[i].length; j++) - _fields.push(_results.data[i][j]); + { + var header = _results.data[i][j]; + + if (_config.trimHeaders) { + header = header.trim(); + } + + _fields.push(header); + } _results.data.splice(0, 1); } diff --git a/tests/test-cases.js b/tests/test-cases.js index 109a3b3..fc21635 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -713,6 +713,15 @@ var PARSE_TESTS = [ errors: [] } }, + { + description: "Header row with whitespace trimmed", + input: ' A , B , C \r\na,b,c', + config: { header: true, trimHeaders: true }, + expected: { + data: [{"A": "a", "B": "b", "C": "c"}], + errors: [] + } + }, { description: "Tab delimiter", input: 'a\tb\tc\r\nd\te\tf',