Skip to content

Commit 959e1cd

Browse files
committed
ref #6 Only allow either comma or semicolon for a field delimiter
1 parent 1e1a2ce commit 959e1cd

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

lib/csv-stringifier-factory.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
const ArrayCsvStringifier = require('./csv-stringifiers/array');
55
const FieldStringifier = require('./field-stringifier');
66
const ObjectCsvStringifier = require('./csv-stringifiers/object');
7+
78
const DEFAULT_FIELD_DELIMITER = ',';
9+
const VALID_FIELD_DELIMITERS = [DEFAULT_FIELD_DELIMITER, ';'];
810

911
class CsvStringifierFactory {
1012

1113
createArrayCsvStringifier(params) {
1214
const fieldDelimiter = params.fieldDelimiter || DEFAULT_FIELD_DELIMITER;
15+
_validateFieldDelimiter(fieldDelimiter);
1316
return new ArrayCsvStringifier({
1417
fieldStringifier: new FieldStringifier({fieldDelimiter}),
1518
fieldDelimiter,
@@ -19,6 +22,7 @@ class CsvStringifierFactory {
1922

2023
createObjectCsvStringifier(params) {
2124
const fieldDelimiter = params.fieldDelimiter || DEFAULT_FIELD_DELIMITER;
25+
_validateFieldDelimiter(fieldDelimiter);
2226
return new ObjectCsvStringifier({
2327
fieldStringifier: new FieldStringifier({fieldDelimiter}),
2428
fieldDelimiter,
@@ -28,4 +32,10 @@ class CsvStringifierFactory {
2832

2933
}
3034

35+
function _validateFieldDelimiter(delimiter) {
36+
if (VALID_FIELD_DELIMITERS.indexOf(delimiter) === -1) {
37+
throw new Error(`Invalid field delimiter \`${delimiter}\` is specified`);
38+
}
39+
}
40+
3141
module.exports = CsvStringifierFactory;

test/unit/csv-stringifiers/array.test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
const assert = require('assert');
32
const resolveDelimiterChar = require('../helper/delimiter').resolveDelimiterChar;
43
const createArrayCsvStringifier = require('../../../index').createArrayCsvStringifier;
@@ -13,6 +12,14 @@ describe('ArrayCsvStringifier', () => {
1312

1413
describe('When field delimiter is semicolon', generateTestCases(';'));
1514

15+
describe('When field delimiter is neither comma nor semicolon', () => {
16+
it('throws an exception', () => {
17+
assert.throws(() => {
18+
createArrayCsvStringifier({fieldDelimiter: '/'});
19+
});
20+
});
21+
});
22+
1623
function generateTestCases(fieldDelimiter) {
1724
const delim = resolveDelimiterChar(fieldDelimiter);
1825
return () => {

test/unit/csv-stringifiers/object.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
const assert = require('assert');
32
const resolveDelimiterChar = require('../helper/delimiter').resolveDelimiterChar;
43
const createObjectCsvStringifier = require('../../../index').createObjectCsvStringifier;
@@ -13,6 +12,17 @@ describe('ObjectCsvStringifier', () => {
1312

1413
describe('When field delimiter is semicolon', generateTestCases(';'));
1514

15+
describe('When field delimiter is neither comma nor semicolon', () => {
16+
it('throws an exception', () => {
17+
assert.throws(() => {
18+
createObjectCsvStringifier({
19+
header: ['FIELD_A', 'FIELD_B'],
20+
fieldDelimiter: '/'
21+
});
22+
});
23+
});
24+
});
25+
1626
function generateTestCases(fieldDelimiter) {
1727
const delim = resolveDelimiterChar(fieldDelimiter);
1828
return () => {

0 commit comments

Comments
 (0)