From 6cc74b2761203ba7a916e6c73a653b31e270c9cb Mon Sep 17 00:00:00 2001 From: Juanjo Diaz Date: Wed, 24 Jan 2018 17:54:27 +0200 Subject: [PATCH] fix: Remove fieldNames (#232) BREAKING CHANGE: Remove fieldNames * Increase coverage back to 100% --- README.md | 15 +++++------ index.d.ts | 1 - lib/json2csv.js | 17 +++---------- test/index.js | 66 +++++++++++++++++++++++++++++++------------------ 4 files changed, 54 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 608d75ad..81da3a68 100644 --- a/README.md +++ b/README.md @@ -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 `` if not specified. (Overridden by `fields[].default`) - `quote` - String, quote around cell values and column names. Defaults to `"` if not specified. @@ -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); ``` @@ -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); diff --git a/index.d.ts b/index.d.ts index d22bb353..48a7c938 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,7 +19,6 @@ declare namespace json2csv { interface Options { data: T[]; fields?: (string | Field | CallbackField)[]; - fieldNames?: string[]; delimiter?: string; defaultValue?: string; quote?: string; diff --git a/lib/json2csv.js b/lib/json2csv.js index 02c38df4..09db7614 100644 --- a/lib/json2csv.js +++ b/lib/json2csv.js @@ -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=""] - default value to use when missing data * @property {String} [quote='"'] - quote around cell values and column names @@ -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 diff --git a/test/index.js b/test/index.js index 6ece43ac..49759a0f 100644 --- a/test/index.js +++ b/test/index.js @@ -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, @@ -238,13 +222,18 @@ 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(); }); @@ -252,8 +241,22 @@ loadFixtures().then(function (csvFixtures) { 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); @@ -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) {