Skip to content
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

bugfix nested arrays with empty strings filled by the next array item #67

Merged
merged 2 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 39 additions & 47 deletions dist/parser/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,18 @@ var Parser = function () {

//Generate the csv output
fillRows = function fillRows(result) {
//Initialize the array with empty strings to handle 'unpopular' headers
var resultRows = [Array(self._headers.length).join(".").split(".")];
var rows = [];
var fillAndPush = function fillAndPush(row) {
return rows.push(row.map(function (col) {
return col || '';
}));
};
// initialize the array with empty strings to handle 'unpopular' headers
var newRow = function newRow() {
return new Array(self._headers.length).fill(null);
};
var emptyRowIndexByHeader = {};
var currentRow = newRow();
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
Expand All @@ -111,42 +121,21 @@ var Parser = function () {
var element = _step2.value;

var elementHeaderIndex = getHeaderIndex(element.item);
var placed = false;
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;

try {
for (var _iterator3 = resultRows[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var row = _step3.value;

if (!placed && row[elementHeaderIndex] === '' || row[elementHeaderIndex] === undefined) {
row[elementHeaderIndex] = self._escape(element.value);
placed = true;
break;
}
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
if (currentRow[elementHeaderIndex] != undefined) {
fillAndPush(currentRow);
currentRow = newRow();
}

if (!placed) {
var newRow = Array(self._headers.length).join(".").split(".");
newRow[elementHeaderIndex] = self._escape(element.value);
resultRows.push(newRow);
emptyRowIndexByHeader[elementHeaderIndex] = emptyRowIndexByHeader[elementHeaderIndex] || 0;
// make sure there isnt a empty row for this header
if (emptyRowIndexByHeader[elementHeaderIndex] < rows.length) {
rows[emptyRowIndexByHeader[elementHeaderIndex]][elementHeaderIndex] = self._escape(element.value);
emptyRowIndexByHeader[elementHeaderIndex] += 1;
continue;
}
currentRow[elementHeaderIndex] = self._escape(element.value);
emptyRowIndexByHeader[elementHeaderIndex] += 1;
}
// push last row
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
Expand All @@ -162,32 +151,35 @@ var Parser = function () {
}
}

fileRows = fileRows.concat(self._checkRows(resultRows));
if (currentRow.length > 0) {
fillAndPush(currentRow);
}
fileRows = fileRows.concat(self._checkRows(rows));
};
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;

try {
for (var _iterator4 = json[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var item = _step4.value;
for (var _iterator3 = json[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var item = _step3.value;

//Call checkType to list all items inside this object
//Items are returned as a object {item: 'Prop Value, Item Name', value: 'Prop Data Value'}
var itemResult = self._handler.check(item, self._options.mainPathItem, item, json);
fillRows(itemResult);
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
Expand Down
36 changes: 22 additions & 14 deletions lib/parser/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,33 @@ class Parser {

//Generate the csv output
fillRows = function(result) {
//Initialize the array with empty strings to handle 'unpopular' headers
let resultRows = [Array(self._headers.length).join(".").split(".")];
const rows = [];
const fillAndPush = (row) => rows.push(row.map(col => col || ''));
// initialize the array with empty strings to handle 'unpopular' headers
const newRow = () => new Array(self._headers.length).fill(null);
const emptyRowIndexByHeader = {};
let currentRow = newRow();
for (let element of result) {
let elementHeaderIndex = getHeaderIndex(element.item);
let placed = false;
for (let row of resultRows) {
if (!placed && row[elementHeaderIndex] === '' || row[elementHeaderIndex] === undefined) {
row[elementHeaderIndex] = self._escape(element.value);
placed = true;
break;
}
if (currentRow[elementHeaderIndex] != undefined) {
fillAndPush(currentRow);
currentRow = newRow();
}
if (!placed) {
let newRow = Array(self._headers.length).join(".").split(".");
newRow[elementHeaderIndex] = self._escape(element.value);
resultRows.push(newRow);
emptyRowIndexByHeader[elementHeaderIndex] = emptyRowIndexByHeader[elementHeaderIndex] || 0;
// make sure there isnt a empty row for this header
if (emptyRowIndexByHeader[elementHeaderIndex] < rows.length) {
rows[emptyRowIndexByHeader[elementHeaderIndex]][elementHeaderIndex] = self._escape(element.value);
emptyRowIndexByHeader[elementHeaderIndex] += 1;
continue;
}
currentRow[elementHeaderIndex] = self._escape(element.value);
emptyRowIndexByHeader[elementHeaderIndex] += 1;
}
fileRows = fileRows.concat(self._checkRows(resultRows));
// push last row
if (currentRow.length > 0) {
fillAndPush(currentRow);
}
fileRows = fileRows.concat(self._checkRows(rows));
};
for (let item of json) {
//Call checkType to list all items inside this object
Expand Down
28 changes: 28 additions & 0 deletions tests/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,32 @@ describe('Array', () => {
expect(csv).to.equal(`a.b,a.c.d,a.e.f${os.EOL}true,1,1${os.EOL},2,2${os.EOL},3,${os.EOL},4,`);
});
});
it('with nested arrays & empty strings', () => {
jsonexport([
{
"a": "",
"b": "b",
"c": [
{
"a": "a1",
"b": "b1"
},
{
"a": "a2",
"b": "b2"
},
{
"a": "",
"b": "b3"
},
{
"a": "a4",
"b": "b4"
}
]
}
], {}, (err, csv) => {
expect(csv).to.equal(`a,b,c.a,c.b${os.EOL},b,a1,b1${os.EOL},,a2,b2${os.EOL},,,b3${os.EOL},,a4,b4`);
});
});
});