Skip to content

Commit

Permalink
adds null / undefined check to the core::joiner
Browse files Browse the repository at this point in the history
adds additional tests to ensure funtionality of core::joiner
  • Loading branch information
NandoTheessen committed Oct 31, 2018
1 parent 2cfc138 commit b7673ac
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 8 deletions.
3 changes: 2 additions & 1 deletion cdn/react-csv-1.0.18.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ var elementOrEmpty = exports.elementOrEmpty = function elementOrEmpty(element) {
var joiner = exports.joiner = function joiner(data) {
var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ',';
return data.map(function (row, index) {
return row.map(function (element) {
if (row == null) return null;
row.map(function (element) {
return "\"" + elementOrEmpty(element) + "\"";
}).join(separator);
}).join("\n");
Expand Down
2 changes: 1 addition & 1 deletion cdn/react-csv-1.0.18.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion cdn/react-csv-latest.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ var elementOrEmpty = exports.elementOrEmpty = function elementOrEmpty(element) {
var joiner = exports.joiner = function joiner(data) {
var separator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ',';
return data.map(function (row, index) {
return row.map(function (element) {
if (row == null) return null;
row.map(function (element) {
return "\"" + elementOrEmpty(element) + "\"";
}).join(separator);
}).join("\n");
Expand Down
2 changes: 1 addition & 1 deletion cdn/react-csv-latest.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export const getHeaderValue = (property, obj) => {
export const elementOrEmpty = (element) => element || element === 0 ? element : '';

export const joiner = ((data,separator = ',') =>
data.map((row, index) => row.map((element) => "\"" + elementOrEmpty(element) + "\"").join(separator)).join(`\n`)
data.map((row, index) => {
if(row == null) return null
return row.map((element) => "\"" + elementOrEmpty(element) + "\"").join(separator)
}).join(`\n`)
);

export const arrays2csv = ((data, headers, separator) =>
Expand Down
15 changes: 13 additions & 2 deletions test/coreSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
jsons2csv,
string2csv,
toCSV,
buildURI
buildURI,
joiner
} from '../src/core';

describe('In browser environment', () => {
Expand Down Expand Up @@ -271,7 +272,7 @@ describe('In browser environment', () => {
expect(() => toCSV(fixtures.string)).toNotThrow();
});

});
})

describe(`core::buildURI`, () =>{
let fixtures;
Expand Down Expand Up @@ -305,4 +306,14 @@ describe('In browser environment', () => {

});
});
describe('core::joiner', () => {
const data = [null, undefined, [1, 2, 3, 5], ["hello hello"]];
it('does not throw upon receiving empty (null / undefined) indices in the data array', () => {
expect(joiner).toNotThrow(data)
})
it('does return the valid data contained between null and undefined values', () => {
expect(joiner(data)).toMatch('\n\n"1","2","3","5"\n"hello hello"')
})
})

});

0 comments on commit b7673ac

Please sign in to comment.