Skip to content

Commit

Permalink
fix: Remove callback support
Browse files Browse the repository at this point in the history
BREAKING CHANGE: callback is no longer available, just return the csv from the json2csv.

- updated tests
- updated readme
  • Loading branch information
Ilya Radchenko committed Jan 23, 2018
1 parent 7143bc7 commit 2096ade
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 286 deletions.
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ try {
- `includeEmptyRows` - Boolean, includes empty rows. Defaults to `false`.
- `preserveNewLinesInValues` - Boolean, preserve \r and \n in values. Defaults to `false`.
- `withBOM` - Boolean, with BOM character. Defaults to `false`.
- `callback` - `function (error, csvString) {}`. If provided, will callback asynchronously. Only supported for compatibility reasons.

#### Example `fields` option
``` javascript
Expand Down Expand Up @@ -145,16 +144,14 @@ car, price, color
### Example 2

Similarly to [mongoexport](http://www.mongodb.org/display/DOCS/mongoexport) you can choose which fields to export.
Note: this example uses the optional callback format.

```javascript
var json2csv = require('json2csv');
var fields = ['car', 'color'];

json2csv({ data: myCars, fields: fields }, function(err, csv) {
if (err) console.log(err);
console.log(csv);
});
var csv = json2csv({ data: myCars, fields: fields });

console.log(csv);
```

Results in
Expand Down
33 changes: 6 additions & 27 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,17 @@ var flatten = require('flat');
/**
* Main function that converts json to csv.
*
* @param {Json2CsvParams} params Function parameters containing data, fields,
* delimiter (default is ','), header (default is true)
* and default value (default is '')
* @param {Function} [callback] Callback function
* if error, returning error in call back.
* if csv is created successfully, returning csv output to callback.
* @param {Json2CsvParams} params parameters containing data and
* and options to configure how that data is processed.
* @returns {String} The CSV formated data as a string
*/
module.exports = function (params, callback) {
var hasCallback = typeof callback === 'function';

try {
checkParams(params);
} catch (err) {
if (hasCallback) {
return process.nextTick(function () {
callback(err);
});
} else {
throw err;
}
}
module.exports = function (params) {
checkParams(params);

var titles = createColumnTitles(params);
var csv = createColumnContent(params, titles);

if (hasCallback) {
return process.nextTick(function () {
callback(null, csv);
});
} else {
return csv;
}
return csv;
};


Expand Down
Loading

0 comments on commit 2096ade

Please sign in to comment.