From 56ab83adcd7ed038f850cf20521d2ec13001fa4b Mon Sep 17 00:00:00 2001 From: quintezkiller Date: Thu, 9 Mar 2017 07:42:35 -0500 Subject: [PATCH 1/6] Wrapping each element of the the row in quotes Commas inside of an element were not supported as the element in the data source wasn't being wrapped in quotes. Wrapping the element in quotes before joining should resolve the problem. --- src/core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core.js b/src/core.js index 47f80eb..3f9e42f 100644 --- a/src/core.js +++ b/src/core.js @@ -19,7 +19,7 @@ export const jsons2arrays = (jsons, headers) => { }; export const joiner = ((data,separator = ',') => - data.map((row, index) => row.join(separator)).join(`\n`) + data.map((row, index) => row.map((element) => "\"" + element + "\"").join(separator)).join(`\n`) ); export const arrays2csv = ((data, headers, separator) => From b6218fa8c4480510dec0edd1f80f5819c93031c8 Mon Sep 17 00:00:00 2001 From: quintezkiller Date: Thu, 9 Mar 2017 08:26:40 -0500 Subject: [PATCH 2/6] Fixing some tests --- test/coreSpec.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/test/coreSpec.js b/test/coreSpec.js index 264d490..89a9ec1 100644 --- a/test/coreSpec.js +++ b/test/coreSpec.js @@ -146,7 +146,7 @@ describe(`core::arrays2csv`, () => { it(`converts Array of arrays to string in CSV format`, () => { const actual = arrays2csv(fixtures); expect(actual).toBeA('string'); - expect(actual.split(`\n`).join(`|`)).toEqual(`a,b|c,d`); + expect(actual.split(`\n`).join(`|`)).toEqual(`"a","b"|"c","d"`); }); it(`renders CSV headers whenever it was given `, () => { @@ -173,7 +173,7 @@ describe(`core::jsons2csv`, () => { const actual = jsons2csv(fixtures); const expectedHeaders = ['X', 'Y']; - const expected = `${expectedHeaders.join(`,`)}|88,97|77,99`; + const expected = `"X","Y"|"88","97"|"77","99"`; expect(actual).toBeA('string'); expect(actual.split(`\n`).join(`|`)).toEqual(expected); }); @@ -199,14 +199,27 @@ describe(`core::string2csv`, () =>{ it(`prepends headers at the top of input`, () => { const headers =[`X`, `Y`]; - expect(string2csv(fixtures, headers)).toEqual(`${headers.join(`,`)}\n${fixtures}`); + expect(string2csv(fixtures, headers)).toEqual(`"X","Y"\n${fixtures}`); }); }); describe(`core::toCSV`, () =>{ let fixtures; beforeEach(() => { - fixtures = {string:'Xy', arrays:[[],[]],jsons:[{}, {}]}; + fixtures = {string:' + + + + + + + + + + + + + y', arrays:[[],[]],jsons:[{}, {}]}; }); it(`requires one argument at least`, () => { expect(() => toCSV()).toThrow(); From 2d6ad78f7acb609fccd585e7370e0976857236d0 Mon Sep 17 00:00:00 2001 From: quintezkiller Date: Thu, 9 Mar 2017 08:37:27 -0500 Subject: [PATCH 3/6] Adding back lines that were accidentally removed --- test/coreSpec.js | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/test/coreSpec.js b/test/coreSpec.js index 89a9ec1..e195603 100644 --- a/test/coreSpec.js +++ b/test/coreSpec.js @@ -206,20 +206,7 @@ describe(`core::string2csv`, () =>{ describe(`core::toCSV`, () =>{ let fixtures; beforeEach(() => { - fixtures = {string:' - - - - - - - - - - - - - y', arrays:[[],[]],jsons:[{}, {}]}; + fixtures = {string:'Xy', arrays:[[],[]],jsons:[{}, {}]}; }); it(`requires one argument at least`, () => { expect(() => toCSV()).toThrow(); From 77412d6c1f078748b98bd840407b26d7df3fa5b7 Mon Sep 17 00:00:00 2001 From: quintezkiller Date: Thu, 9 Mar 2017 08:49:37 -0500 Subject: [PATCH 4/6] More test updates --- test/coreSpec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/coreSpec.js b/test/coreSpec.js index e195603..bfa1b39 100644 --- a/test/coreSpec.js +++ b/test/coreSpec.js @@ -152,7 +152,7 @@ describe(`core::arrays2csv`, () => { it(`renders CSV headers whenever it was given `, () => { const headers = [`X`, `Y`]; const firstLineOfCSV = arrays2csv(fixtures, headers).split(`\n`)[0]; - expect(firstLineOfCSV).toEqual(headers.join(`,`)); + expect(firstLineOfCSV).toEqual("X","Y"); }); }); @@ -181,8 +181,8 @@ describe(`core::jsons2csv`, () => { let fixtures =[{X:'12', Y:'bb'}, {Y:'ee', X:'55'}] const headers = ['Y', 'X', 'Z']; const actual = jsons2csv(fixtures, headers); - expect(actual.startsWith(headers.join(`,`))).toBeTruthy(); - expect(actual.endsWith(`ee,55,`)).toBeTruthy(); + expect(actual.startsWith("Y","X","Z")).toBeTruthy(); + expect(actual.endsWith(`"ee","55",""`)).toBeTruthy(); }); From b374b314221d0e3ebc8186d227016fea41e43cf6 Mon Sep 17 00:00:00 2001 From: quintezkiller Date: Thu, 9 Mar 2017 08:58:43 -0500 Subject: [PATCH 5/6] Test updates --- test/coreSpec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/coreSpec.js b/test/coreSpec.js index bfa1b39..046406c 100644 --- a/test/coreSpec.js +++ b/test/coreSpec.js @@ -152,7 +152,7 @@ describe(`core::arrays2csv`, () => { it(`renders CSV headers whenever it was given `, () => { const headers = [`X`, `Y`]; const firstLineOfCSV = arrays2csv(fixtures, headers).split(`\n`)[0]; - expect(firstLineOfCSV).toEqual("X","Y"); + expect(firstLineOfCSV).toEqual(`"X","Y"`); }); }); @@ -181,7 +181,7 @@ describe(`core::jsons2csv`, () => { let fixtures =[{X:'12', Y:'bb'}, {Y:'ee', X:'55'}] const headers = ['Y', 'X', 'Z']; const actual = jsons2csv(fixtures, headers); - expect(actual.startsWith("Y","X","Z")).toBeTruthy(); + expect(actual.startsWith(`"Y","X","Z"`)).toBeTruthy(); expect(actual.endsWith(`"ee","55",""`)).toBeTruthy(); }); @@ -199,7 +199,7 @@ describe(`core::string2csv`, () =>{ it(`prepends headers at the top of input`, () => { const headers =[`X`, `Y`]; - expect(string2csv(fixtures, headers)).toEqual(`"X","Y"\n${fixtures}`); + expect(string2csv(fixtures, headers)).toEqual(`X,Y\n${fixtures}`); }); }); From dbc0a7ffe62499556a6746b54f84b399e29a9639 Mon Sep 17 00:00:00 2001 From: quintezkiller Date: Thu, 9 Mar 2017 09:08:07 -0500 Subject: [PATCH 6/6] Upping version number --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0cf2e29..9acf765 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-csv", - "version": "1.0.3-RC1", + "version": "1.0.4-RC1", "description": "Build CSV files on the fly basing on Array/literal object of data ", "main": "index.js", "jsnext:main": "src/index.js",