Skip to content

Commit

Permalink
Fixes kaue#78. Added "", to the relevant row instead of keeping them …
Browse files Browse the repository at this point in the history
…sparse so that fillGaps finds the columns for the first row accurately. Updated test to use os.EOL instead of \n for object.js
  • Loading branch information
iuabhmalat committed Jul 23, 2020
1 parent f486a71 commit 53db21e
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 4 deletions.
121 changes: 121 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,127 @@ speed.min,5
size,10;20
```

### Complex JSON Array

#### Code (fillGaps, fillTopRow = true)

```javascript
const jsonexport = require('jsonexport');

const stats = [{
manufacturer: 'BMW',
country: 'USA',
inventory: {
profile: ['public'],
cars: [
{
model: '3-series',
price: '34850'
},
{
model: '5-series',
price: '55000'
},
{
model: '7-series',
price: '72000'
}
]
},
category: [
{
sedan: [
{
seats: 4,
doors: 4,
sunroof: true
}
]
}
],
location: {
address: [
{
city: 'Raleigh',
state: 'NC',
dealer: 'Raleigh Motors'
},
{
city: 'Durham',
state: 'NC',
dealer: 'Durham BMW'
}
]
}
},
{
manufacturer: 'Audi',
country: 'USA',
inventory: {
profile: ['public'],
cars: [
{
model: 'Q3',
price: '36700'
},
{
model: 'Q5',
price: '48900'
},
{
model: 'Q7',
price: '91350'
}
]
},
category: [
{
sedan: [
{
seats: 7,
doors: 4,
sunroof: true,
heatedSeats: 'front'
}
]
}
],
location: {
address: [
{
city: 'Greensboro',
state: 'NC',
dealer: 'Audi Greensboro'
},
{
city: 'Asheville',
state: 'NC',
dealer: 'Asheville Audi Motors'
}
]
}
}
];

jsonexport(stats, {fillGaps: true, fillTopRow: true},
function(err, csv){
if(err) return console.error(err);
console.log(csv);
});
```

#### Result

```
manufacturer,country,inventory.profile,inventory.cars.model,inventory.cars.price,category.sedan.seats,category.sedan.doors,category.sedan.sunroof,location.address.city,location.address.state,location.address.dealer,category.sedan.heatedSeats
BMW,USA,public,3-series,34850,4,4,true,Raleigh,NC,Raleigh Motors
BMW,USA,public,5-series,55000,4,4,true,Durham,NC,Durham BMW
BMW,USA,public,7-series,72000,4,4,true,Durham,NC,Durham BMW
Audi,USA,public,Q3,36700,7,4,true,Greensboro,NC,Audi Greensboro,front
Audi,USA,public,Q5,48900,7,4,true,Asheville,NC,Asheville Audi Motors,front
Audi,USA,public,Q7,91350,7,4,true,Asheville,NC,Asheville Audi Motors,front
```

## Options

In order to get the most of out of this module, you can customize many parameters and functions.
Expand Down
10 changes: 10 additions & 0 deletions lib/parser/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,20 @@ class Parser {
emptyRowIndexByHeader[elementHeaderIndex] = emptyRowIndexByHeader[elementHeaderIndex] || 0;
// make sure there isnt a empty row for this header
if (self._options.fillTopRow && emptyRowIndexByHeader[elementHeaderIndex] < rows.length) {
// Fill gap between row's current length and elementHeaderIndex with ""
let rowGap = elementHeaderIndex - rows[emptyRowIndexByHeader[elementHeaderIndex]].length;
if (rowGap > 0) {
rows[emptyRowIndexByHeader[elementHeaderIndex]] = rows[emptyRowIndexByHeader[elementHeaderIndex]].concat(
Array(rowGap).fill(""));
}
rows[emptyRowIndexByHeader[elementHeaderIndex]][elementHeaderIndex] = self._escape(element.value);
emptyRowIndexByHeader[elementHeaderIndex] += 1;
continue;
}
if (currentRow.length < elementHeaderIndex) {
currentRow = currentRow.concat(
Array(elementHeaderIndex - currentRow.length).fill(""));
}
currentRow[elementHeaderIndex] = self._escape(element.value);
emptyRowIndexByHeader[elementHeaderIndex] += 1;
}
Expand Down
7 changes: 3 additions & 4 deletions tests/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var {assert, expect} = require('chai');
var jsonexport = require('../lib/index');
var os = require('os');


const isRemoteTest = process.env.APPVEYOR || process.env.TRAVIS;
if( isRemoteTest ){
console.log('\x1b[34mRemote testing server detected on '+os.type()+' '+os.platform()+' '+os.release()+'\x1b[0m');
Expand Down Expand Up @@ -100,7 +99,7 @@ describe('Object', () => {
if(parent===contacts){
return 'parentless-'+index;
}

return value.toString();
}
}
Expand All @@ -121,6 +120,6 @@ describe('Object', () => {
}
], {})

assert.equal(csv, `a,b,c,d,e\n0,,,1,this`)
assert.equal(csv, `a,b,c,d,e`+ os.EOL +`0,,,1,this`)
});
});
});

0 comments on commit 53db21e

Please sign in to comment.