Skip to content

Add tests and docs to unwind-blank feature #287

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

Merged
merged 1 commit into from
Apr 16, 2018
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
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