Skip to content

Commit dce4d33

Browse files
juanjoDiazknownasilya
authored andcommitted
chore: Refactor the entire library to ES6 (#233)
BREAKING CHANGE: Remove `preserveNewLinesInValues` option, preserve by default * Refactor the entire library to ES6 * Fix PR issues * Add strict mode for node 4.X
1 parent 6cc74b2 commit dce4d33

15 files changed

+11272
-785
lines changed

README.md

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ $ npm install json2csv --save
2121
Include the module and run or [use it from the Command Line](https://github.com/zemirco/json2csv#command-line-interface). It's also possible to include `json2csv` as a global using an HTML script tag, though it's normally recommended that modules are used.
2222

2323
```javascript
24-
var json2csv = require('json2csv');
25-
var fields = ['field1', 'field2', 'field3'];
24+
const json2csv = require('json2csv');
25+
const fields = ['field1', 'field2', 'field3'];
2626

2727
try {
28-
var result = json2csv({ data: myData, fields: fields });
28+
const result = json2csv(myData, { fields });
2929
console.log(result);
3030
} catch (err) {
3131
// Errors are thrown for bad options, or if the data is empty and no fields are provided.
@@ -56,19 +56,17 @@ try {
5656
### Available Options
5757

5858
- `options` - **Required**; Options hash.
59-
- `data` - **Required**; Array of JSON objects.
6059
- `fields` - Array of Objects/Strings. Defaults to toplevel JSON attributes. See example below.
61-
- `delimiter` - String, delimiter of columns. Defaults to `,` if not specified.
60+
- `unwind` - Array of Strings, creates multiple rows from a single JSON document similar to MongoDB's $unwind
61+
- `flatten` - Boolean, flattens nested JSON using [flat]. Defaults to `false`.
6262
- `defaultValue` - String, default value to use when missing data. Defaults to `<empty>` if not specified. (Overridden by `fields[].default`)
6363
- `quote` - String, quote around cell values and column names. Defaults to `"` if not specified.
6464
- `doubleQuote` - String, the value to replace double quote in strings. Defaults to 2x`quotes` (for example `""`) if not specified.
65-
- `header` - Boolean, determines whether or not CSV file will contain a title column. Defaults to `true` if not specified.
65+
- `delimiter` - String, delimiter of columns. Defaults to `,` if not specified.
6666
- `eol` - String, overrides the default OS line ending (i.e. `\n` on Unix and `\r\n` on Windows).
67-
- `flatten` - Boolean, flattens nested JSON using [flat]. Defaults to `false`.
68-
- `unwind` - Array of Strings, creates multiple rows from a single JSON document similar to MongoDB's $unwind
6967
- `excelStrings` - Boolean, converts string data into normalized Excel style data.
68+
- `header` - Boolean, determines whether or not CSV file will contain a title column. Defaults to `true` if not specified.
7069
- `includeEmptyRows` - Boolean, includes empty rows. Defaults to `false`.
71-
- `preserveNewLinesInValues` - Boolean, preserve \r and \n in values. Defaults to `false`.
7270
- `withBOM` - Boolean, with BOM character. Defaults to `false`.
7371

7472
#### Example `fields` option
@@ -85,11 +83,7 @@ try {
8583
// Supports label -> derived value
8684
{
8785
label: 'some label', // Supports duplicate labels (required, else your column will be labeled [function])
88-
value: function(row, field, data) {
89-
// field = { label, default }
90-
// data = full data object
91-
return row.path1 + row.path2;
92-
},
86+
value: (row, field) => row.path1 + row.path2, // field = { label, default }
9387
default: 'NULL', // default if value function returns null or undefined
9488
stringify: true // If value is function use this flag to signal if resulting string will be quoted (stringified) or not (optional, default: true)
9589
},
@@ -104,10 +98,10 @@ try {
10498
### Example 1
10599

106100
```javascript
107-
var json2csv = require('json2csv');
108-
var fs = require('fs');
109-
var fields = ['car', 'price', 'color'];
110-
var myCars = [
101+
const json2csv = require('json2csv');
102+
const fs = require('fs');
103+
const fields = ['car', 'price', 'color'];
104+
const myCars = [
111105
{
112106
"car": "Audi",
113107
"price": 40000,
@@ -122,9 +116,9 @@ var myCars = [
122116
"color": "green"
123117
}
124118
];
125-
var csv = json2csv({ data: myCars, fields: fields });
119+
const csv = json2csv(myCars, { fields });
126120

127-
fs.writeFile('file.csv', csv, function(err) {
121+
fs.writeFile('file.csv', csv, (err) => {
128122
if (err) throw err;
129123
console.log('file saved');
130124
});
@@ -144,10 +138,10 @@ car, price, color
144138
Similarly to [mongoexport](http://www.mongodb.org/display/DOCS/mongoexport) you can choose which fields to export.
145139

146140
```javascript
147-
var json2csv = require('json2csv');
148-
var fields = ['car', 'color'];
141+
const json2csv = require('json2csv');
142+
const fields = ['car', 'color'];
149143

150-
var csv = json2csv({ data: myCars, fields: fields });
144+
const csv = json2csv(myCars, { fields });
151145

152146
console.log(csv);
153147
```
@@ -166,9 +160,9 @@ car, color
166160
Use a custom delimiter to create tsv files. Add it as the value of the delimiter property on the parameters:
167161

168162
```javascript
169-
var json2csv = require('json2csv');
170-
var fields = ['car', 'price', 'color'];
171-
var tsv = json2csv({ data: myCars, fields: fields, delimiter: '\t' });
163+
const json2csv = require('json2csv');
164+
const fields = ['car', 'price', 'color'];
165+
const tsv = json2csv(myCars, { fields, delimiter: '\t' });
172166

173167
console.log(tsv);
174168
```
@@ -190,15 +184,15 @@ If no delimiter is specified, the default `,` is used
190184
You can choose custom column names for the exported file.
191185

192186
```javascript
193-
var json2csv = require('json2csv');
194-
var fields = [{
187+
const json2csv = require('json2csv');
188+
const fields = [{
195189
label: 'Car Name',
196190
value: 'car'
197191
},{
198192
label: 'Price USD',
199193
value: 'price'
200194
}];
201-
var csv = json2csv({ data: myCars, fields: fields });
195+
const csv = json2csv(myCars, { fields });
202196

203197
console.log(csv);
204198
```
@@ -208,14 +202,15 @@ console.log(csv);
208202
You can choose custom quotation marks.
209203

210204
```javascript
211-
var json2csv = require('json2csv');
212-
var fields = ['car', 'price'];
213-
var opts = {
214-
data: myCars,
215-
fields: fields,
216-
quote: ''
217-
};
218-
var csv = json2csv(opts);
205+
const json2csv = require('json2csv');
206+
const fields = [{
207+
label: 'Car Name',
208+
value: 'car'
209+
},{
210+
label: 'Price USD',
211+
value: 'price'
212+
}];
213+
const csv = json2csv(myCars, { fields, quote: '' });
219214

220215
console.log(csv);
221216
```
@@ -234,10 +229,10 @@ Porsche, 30000
234229
You can also specify nested properties using dot notation.
235230

236231
```javascript
237-
var json2csv = require('json2csv');
238-
var fs = require('fs');
239-
var fields = ['car.make', 'car.model', 'price', 'color'];
240-
var myCars = [
232+
const json2csv = require('json2csv');
233+
const fs = require('fs');
234+
const fields = ['car.make', 'car.model', 'price', 'color'];
235+
const myCars = [
241236
{
242237
"car": {"make": "Audi", "model": "A3"},
243238
"price": 40000,
@@ -252,9 +247,9 @@ var myCars = [
252247
"color": "green"
253248
}
254249
];
255-
var csv = json2csv({ data: myCars, fields: fields });
250+
const csv = json2csv(myCars, { fields });
256251

257-
fs.writeFile('file.csv', csv, function(err) {
252+
fs.writeFile('file.csv', csv, (err) => {
258253
if (err) throw err;
259254
console.log('file saved');
260255
});
@@ -274,10 +269,10 @@ car.make, car.model, price, color
274269
You can unwind arrays similar to MongoDB's $unwind operation using the `unwind` option.
275270

276271
```javascript
277-
var json2csv = require('json2csv');
278-
var fs = require('fs');
279-
var fields = ['carModel', 'price', 'colors'];
280-
var myCars = [
272+
const json2csv = require('json2csv');
273+
const fs = require('fs');
274+
const fields = ['carModel', 'price', 'colors'];
275+
const myCars = [
281276
{
282277
"carModel": "Audi",
283278
"price": 0,
@@ -296,9 +291,9 @@ var myCars = [
296291
"colors": ["green","teal","aqua"]
297292
}
298293
];
299-
var csv = json2csv({ data: myCars, fields: fields, unwind: 'colors' });
294+
const csv = json2csv(myCars, { fields, unwind: 'colors' });
300295

301-
fs.writeFile('file.csv', csv, function(err) {
296+
fs.writeFile('file.csv', csv, (err) => {
302297
if (err) throw err;
303298
console.log('file saved');
304299
});
@@ -324,10 +319,10 @@ The content of the "file.csv" should be
324319
You can also unwind arrays multiple times or with nested objects.
325320

326321
```javascript
327-
var json2csv = require('json2csv');
328-
var fs = require('fs');
329-
var fields = ['carModel', 'price', 'items.name', 'items.color', 'items.items.position', 'items.items.color'];
330-
var myCars = [
322+
const json2csv = require('json2csv');
323+
const fs = require('fs');
324+
const fields = ['carModel', 'price', 'items.name', 'items.color', 'items.items.position', 'items.items.color'];
325+
const myCars = [
331326
{
332327
"carModel": "BMW",
333328
"price": 15000,
@@ -370,9 +365,9 @@ var myCars = [
370365
]
371366
}
372367
];
373-
var csv = json2csv({ data: myCars, fields: fields, unwind: ['items', 'items.items'] });
368+
const csv = json2csv(myCars, { fields, unwind: ['items', 'items.items'] });
374369

375-
fs.writeFile('file.csv', csv, function(err) {
370+
fs.writeFile('file.csv', csv, (err) => {
376371
if (err) throw err;
377372
console.log('file saved');
378373
});
@@ -395,29 +390,29 @@ The content of the "file.csv" should be
395390
`json2csv` can also be called from the command line if installed with `-g`.
396391

397392
```bash
398-
Usage: json2csv [options]
393+
Usage: json2csv [options]
394+
399395

400396
Options:
401397

402-
-h, --help output usage information
403398
-V, --version output the version number
404399
-i, --input <input> Path and name of the incoming json file. If not provided, will read from stdin.
405400
-o, --output [output] Path and name of the resulting csv file. Defaults to stdout.
401+
-L, --ldjson Treat the input as Line-Delimited JSON.
406402
-f, --fields <fields> Specify the fields to convert.
407-
-l, --fieldList [list] Specify a file with a list of fields to include. One field per line.
408-
-d, --delimiter [delimiter] Specify a delimiter other than the default comma to use.
403+
-l, --field-list [list] Specify a file with a list of fields to include. One field per line.
404+
-u, --unwind <paths> Creates multiple rows from a single JSON document similar to MongoDB unwind.
405+
-F, --flatten Flatten nested objects
409406
-v, --default-value [defaultValue] Specify a default value other than empty string.
410-
-e, --eol [value] Specify an End-of-Line value for separating rows.
411407
-q, --quote [value] Specify an alternate quote value.
412408
-dq, --double-quotes [value] Specify a value to replace double quote in strings
409+
-d, --delimiter [delimiter] Specify a delimiter other than the default comma to use.
410+
-e, --eol [value] Specify an End-of-Line value for separating rows.
413411
-ex, --excel-strings Converts string data into normalized Excel style data
414412
-n, --no-header Disable the column name header
415-
-F, --flatten Flatten nested objects
416-
-u, --unwind <paths> Creates multiple rows from a single JSON document similar to MongoDB unwind.
417-
-L, --ldjson Treat the input as Line-Delimited JSON.
418-
-p, --pretty Use only when printing to console. Logs output in pretty tables.
419413
-a, --include-empty-rows Includes empty rows in the resulting CSV output.
420414
-b, --with-bom Includes BOM character at the beginning of the csv.
415+
-p, --pretty Use only when printing to console. Logs output in pretty tables.
421416
-h, --help output usage information
422417
```
423418

0 commit comments

Comments
 (0)