From fa77e1852ebb1e2433992c5deb383f06412f96c7 Mon Sep 17 00:00:00 2001 From: Danieth Date: Sun, 12 Mar 2017 15:15:51 -0400 Subject: [PATCH 1/2] Added a failing test showing issue #6 * Replaced '99' with a numerical 0 * https://github.com/abdennour/react-csv/issues/6 --- test/coreSpec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/coreSpec.js b/test/coreSpec.js index 046406c..33ce708 100644 --- a/test/coreSpec.js +++ b/test/coreSpec.js @@ -100,7 +100,7 @@ describe(`core::jsons2arrays`, () => { sport: '97' }, { maths: '77', - sport: '99' + sport: 0 }] }); it(`converts an Array of literal objects to Array of arrays`, () => { @@ -109,7 +109,7 @@ describe(`core::jsons2arrays`, () => { ['maths', 'sport'], ['90', ''], ['', '97'], - ['77', '99'] + ['77', 0] ]; expect(actual).toEqual(expected); }); @@ -117,7 +117,7 @@ describe(`core::jsons2arrays`, () => { it(`converts to Array of arrays following the order of headers`, () => { const actual = jsons2arrays(fixtures, ['sport', 'maths']); const expected = [ - ['maths', 'sport'].reverse(), ['90', ''].reverse(), ['', '97'].reverse(), ['77', '99'].reverse() + ['maths', 'sport'].reverse(), ['90', ''].reverse(), ['', '97'].reverse(), ['77', 0].reverse() ]; expect(actual).toEqual(expected); }); @@ -127,7 +127,7 @@ describe(`core::jsons2arrays`, () => { const expected = [ headers, ['90', '', '', ''], ['', '97', '', ''], - ['77', '99', '', ''] + ['77', 0, '', ''] ]; expect(actual).toEqual(expected); }); From 486589d967f004fdb4717036a0fcea25117f36a0 Mon Sep 17 00:00:00 2001 From: Danieth Date: Sun, 12 Mar 2017 15:17:52 -0400 Subject: [PATCH 2/2] Modified jsonsHeaders#jsons2arrays to work with falsy keys * `header in object` should be a sufficient condition. The suggested `String(object[header])` wasn't passing the original tests - it caused keys that didn't exist on the object to be inserted as undefined. Checking if the key is in the object allows for falsy values, while preventing keys that haven't ever been set from leaking in. * Should close issue #6 * https://github.com/abdennour/react-csv/issues/6 --- src/core.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/core.js b/src/core.js index 3f9e42f..b3bb290 100644 --- a/src/core.js +++ b/src/core.js @@ -12,10 +12,9 @@ export const jsonsHeaders = ((array) => Array.from( )); export const jsons2arrays = (jsons, headers) => { - headers = headers || jsonsHeaders(jsons); - return [headers, ...jsons.map((object) => - headers.map((header) => - object[header] ? object[header] : ''))] + headers = headers || jsonsHeaders(jsons); + const data = jsons.map((object) => headers.map((header) => (header in object) ? object[header] : '')); + return [headers, ...data]; }; export const joiner = ((data,separator = ',') =>