Skip to content

Commit

Permalink
fix: Remove fieldNames (#232)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Remove fieldNames

* Increase coverage back to 100%
  • Loading branch information
juanjoDiaz authored and knownasilya committed Jan 24, 2018
1 parent 2096ade commit 6cc74b2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 45 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ try {
- `options` - **Required**; Options hash.
- `data` - **Required**; Array of JSON objects.
- `fields` - Array of Objects/Strings. Defaults to toplevel JSON attributes. See example below.
- `fieldNames` Array of Strings, names for the fields at the same indexes.
Must be the same length as `fields` array. (Optional. Maintained for backwards compatibility. Use `fields` config object for more features)
- `delimiter` - String, delimiter of columns. Defaults to `,` if not specified.
- `defaultValue` - String, default value to use when missing data. Defaults to `<empty>` if not specified. (Overridden by `fields[].default`)
- `quote` - String, quote around cell values and column names. Defaults to `"` if not specified.
Expand Down Expand Up @@ -193,9 +191,14 @@ You can choose custom column names for the exported file.

```javascript
var json2csv = require('json2csv');
var fields = ['car', 'price'];
var fieldNames = ['Car Name', 'Price USD'];
var csv = json2csv({ data: myCars, fields: fields, fieldNames: fieldNames });
var fields = [{
label: 'Car Name',
value: 'car'
},{
label: 'Price USD',
value: 'price'
}];
var csv = json2csv({ data: myCars, fields: fields });

console.log(csv);
```
Expand All @@ -207,11 +210,9 @@ You can choose custom quotation marks.
```javascript
var json2csv = require('json2csv');
var fields = ['car', 'price'];
var fieldNames = ['Car Name', 'Price USD'];
var opts = {
data: myCars,
fields: fields,
fieldNames: fieldNames,
quote: ''
};
var csv = json2csv(opts);
Expand Down
1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ declare namespace json2csv {
interface Options<T> {
data: T[];
fields?: (string | Field | CallbackField<T>)[];
fieldNames?: string[];
delimiter?: string;
defaultValue?: string;
quote?: string;
Expand Down
17 changes: 4 additions & 13 deletions lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ var flatten = require('flat');
* @typedef {Object}
* @property {Array} data - array of JSON objects
* @property {Array} [fields] - see documentation for details
* @property {String[]} [fieldNames] - names for fields at the same indexes. Must be same length as fields array
* (Optional. Maintained for backwards compatibility. Use fields config object for more features)
* @property {String} [delimiter=","] - delimiter of columns
* @property {String} [defaultValue="<empty>"] - default value to use when missing data
* @property {String} [quote='"'] - quote around cell values and column names
Expand Down Expand Up @@ -81,18 +79,11 @@ function checkParams(params) {
params.fields = lodashUniq(dataFields);
}


// check fieldNames
if (params.fieldNames && params.fieldNames.length !== params.fields.length) {
throw new Error('fieldNames and fields should be of the same length, if fieldNames is provided.');
}

// Get fieldNames from fields
params.fieldNames = params.fields.map(function (field, i) {
if (params.fieldNames && typeof field === 'string') {
return params.fieldNames[i];
}
return (typeof field === 'string') ? field : (field.label || field.value);
params.fieldNames = params.fields.map(function (field) {
return (typeof field === 'string')
? field
: (field.label || field.value);
});

// check delimiter
Expand Down
66 changes: 42 additions & 24 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,6 @@ loadFixtures().then(function (csvFixtures) {
t.end();
});

test('should error if fieldNames don\'t line up to fields', function (t) {
var csv;
try {
csv = json2csv({
data: jsonDefault,
field: ['carModel'],
fieldNames: ['test', 'blah']
});
t.notOk(true);
} catch (error) {
t.equal(error.message, 'fieldNames and fields should be of the same length, if fieldNames is provided.');
t.notOk(csv);
t.end();
}
});

test('should parse json to csv', function (t) {
var csv = json2csv({
data: jsonDefault,
Expand Down Expand Up @@ -238,22 +222,41 @@ loadFixtures().then(function (csvFixtures) {
t.end();
});

test('should name columns as specified in \'fieldNames\' property', function (t) {
test('should name columns as specified in \'fields\' property', function (t) {
var csv = json2csv({
data: jsonDefault,
fields: ['carModel', 'price'],
fieldNames: ['Car Model', 'Price USD']
fields: [{
label: 'Car Model',
value: 'carModel'
},{
label: 'Price USD',
value: 'price'
}]
});

t.equal(csv, csvFixtures.fieldNames);
t.end();
});

test('should output nested properties', function (t) {
var csv = json2csv({
data: jsonNested,
fields: ['car.make', 'car.model', 'price', 'color', 'car.ye.ar'],
fieldNames: ['Make', 'Model', 'Price', 'Color', 'Year']
fields: [{
label: 'Make',
value: 'car.make'
},{
label: 'Model',
value: 'car.model'
},{
label: 'Price',
value: 'price'
},{
label: 'Color',
value: 'color'
},{
label: 'Year',
value: 'car.ye.ar'
}]
});

t.equal(csv, csvFixtures.nested);
Expand Down Expand Up @@ -282,13 +285,28 @@ loadFixtures().then(function (csvFixtures) {
t.end();
});

test('should error if params is not an object', function (t) {
test('should error synchronously if fieldNames don\'t line up to fields', function (t) {
var csv;

try {
csv = json2csv({
data: 'not an object',
field: ['carModel']
});
t.notOk(true);
} catch (error) {
t.equal(error.message, 'params should include "fields" and/or non-empty "data" array of objects');
t.notOk(csv);
t.end();
}
});

test('should error asynchronously if params is not an object', function (t) {
var csv;

try {
csv = json2csv({
data: 'not an object',
field: ['carModel'],
fieldNames: ['test', 'blah']
});
} catch(error) {
Expand Down

0 comments on commit 6cc74b2

Please sign in to comment.