Skip to content

Commit

Permalink
fix: Add tests and docs to unwind-blank feature (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Apr 16, 2018
1 parent d97a668 commit e3d4a05
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,75 @@ will output to console
"Porsche",30000,"dashboard",,"right","black"
```

#### Example 9

You can also unwind arrays blanking the repeated fields.

```javascript
const Json2csvParser = require('json2csv').Parser;
const fields = ['carModel', 'price', 'items.name', 'items.color', 'items.items.position', 'items.items.color'];
const myCars = [
{
"carModel": "BMW",
"price": 15000,
"items": [
{
"name": "airbag",
"color": "white"
}, {
"name": "dashboard",
"color": "black"
}
]
}, {
"carModel": "Porsche",
"price": 30000,
"items": [
{
"name": "airbag",
"items": [
{
"position": "left",
"color": "white"
}, {
"position": "right",
"color": "gray"
}
]
}, {
"name": "dashboard",
"items": [
{
"position": "left",
"color": "gray"
}, {
"position": "right",
"color": "black"
}
]
}
]
}
];

const json2csvParser = new Json2csvParser({ fields, unwind: ['items', 'items.items'], unwindBlank: true });
const csv = json2csvParser.parse(myCars);

console.log(csv);
```

will output to console

```
"carModel","price","items.name","items.color","items.items.position","items.items.color"
"BMW",15000,"airbag","white",,
,,"dashboard","black",,
"Porsche",30000,"airbag",,"left","white"
,,,,"right","gray"
,,"dashboard",,"left","gray"
,,,,"right","black"
```

### Migrating from 3.X to 4.X

What in 3.X used to be
Expand Down
12 changes: 12 additions & 0 deletions test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,18 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});
});

testRunner.add('hould unwind and blank out repeated data', (t) => {
const opts = ' --fields carModel,price,items.name,items.color,items.items.position,items.items.color'
+ ' --unwind items,items.items --unwind-blank';

child_process.exec(cli + '-i ' + getFixturePath('/json/unwind2.json') + opts, (err, stdout, stderr) => {
t.notOk(stderr);
const csv = stdout;
t.equal(csv, csvFixtures.unwind2Blank);
t.end();
});
});

testRunner.add('should support flattenning deep JSON', (t) => {
const opts = ' --flatten';

Expand Down
2 changes: 1 addition & 1 deletion test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
const opts = {
fields: ['carModel', 'price', 'items.name', 'items.color', 'items.items.position', 'items.items.color'],
unwind: ['items', 'items.items'],
unwindBlank: true,
unwindBlank: true
};

const parser = new Json2csvParser(opts);
Expand Down
22 changes: 22 additions & 0 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,28 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
.on('error', err => t.notOk(true, err.message));
});



testRunner.add('should unwind and blank out repeated data', (t) => {
const opts = {
fields: ['carModel', 'price', 'items.name', 'items.color', 'items.items.position', 'items.items.color'],
unwind: ['items', 'items.items'],
unwindBlank: true
};

const transform = new Json2csvTransform(opts);
const processor = jsonFixtures.unwind2().pipe(transform);

let csv = '';
processor
.on('data', chunk => (csv += chunk.toString()))
.on('end', () => {
t.equal(csv, csvFixtures.unwind2Blank);
t.end();
})
.on('error', err => t.notOk(true, err.message));
});

testRunner.add('should support flattenning deep JSON', (t) => {
const opts = {
flatten: true
Expand Down

0 comments on commit e3d4a05

Please sign in to comment.