Skip to content

Commit

Permalink
Support keys with '.' characters in them (#194)
Browse files Browse the repository at this point in the history
* Add support for keys with nested dots.

Through modifications to `doc-path` and `deeks` dependency underlying
code, I've worked to add support for the keys with nested dots in the
JSON key path and conversion back to CSV. This was achieved through the
`deeks` module's new `escapeNestedDots` option which will now be set to
`true`. Additionally, support for this escaping was added to `doc-path`
so that the correct values are retrieved and can also be correctly set
for csv2json. This commit adds the option specification for the `deeks`
module and the updated versions of the dependency modules in
package.json.

Fixes #184
  • Loading branch information
mrodrig committed May 28, 2021
1 parent 8778fb2 commit bd1fd28
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const Json2Csv = function(options) {
expandingWithoutUnwinding = options.expandArrayObjects && !options.unwindArrays,
deeksOptions = {
expandArrayObjects: expandingWithoutUnwinding,
ignoreEmptyArraysWhenExpanding: expandingWithoutUnwinding
ignoreEmptyArraysWhenExpanding: expandingWithoutUnwinding,
escapeNestedDots: true
};

/** HEADER FIELD FUNCTIONS **/
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"cli"
],
"dependencies": {
"deeks": "2.3.0",
"doc-path": "2.3.0"
"deeks": "2.4.1",
"doc-path": "3.0.0"
},
"devDependencies": {
"babel-eslint": "10.1.0",
Expand Down
5 changes: 4 additions & 1 deletion test/config/testCsvFilesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ const fs = require('fs'),
{key: 'emptyLastFieldValue', file: '../data/csv/emptyLastFieldValue.csv'},
{key: 'emptyLastFieldValueNoEol', file: '../data/csv/emptyLastFieldValueNoEol.csv'},
{key: 'lastCharFieldDelimiter', file: '../data/csv/lastCharFieldDelimiter.csv'},
{key: 'nativeMapMethod', file: '../data/csv/nativeMapMethod.csv'}
{key: 'nativeMapMethod', file: '../data/csv/nativeMapMethod.csv'},
{key: 'nestedDotKeys', file: '../data/csv/nestedDotKeys.csv'},
{key: 'nestedDotKeysWithArray', file: '../data/csv/nestedDotKeysWithArray.csv'},
{key: 'nestedDotKeysWithArrayExpandedUnwound', file: '../data/csv/nestedDotKeysWithArrayExpandedUnwound.csv'}
];

function readCsvFile(filePath) {
Expand Down
5 changes: 4 additions & 1 deletion test/config/testJsonFilesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@ module.exports = {
firstColumnWrapCRLF: require('../data/json/firstColumnWrapCRLF.json'),
emptyLastFieldValue: require('../data/json/emptyLastFieldValue.json'),
emptyLastFieldValueNoEol: require('../data/json/emptyLastFieldValueNoEol.json'),
nativeMapMethod: require('../data/json/nativeMapMethod.json')
nativeMapMethod: require('../data/json/nativeMapMethod.json'),
nestedDotKeys: require('../data/json/nestedDotKeys.json'),
nestedDotKeysWithArray: require('../data/json/nestedDotKeysWithArray.json'),
nestedDotKeysWithArrayExpandedUnwound: require('../data/json/nestedDotKeysWithArrayExpandedUnwound.json')
};
27 changes: 27 additions & 0 deletions test/csv2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,33 @@ function runTests(jsonTestData, csvTestData) {
done();
});
});

// Test case for #184
it('should properly convert keys with escaped/nested dots in them', (done) => {
converter.csv2json(csvTestData.nestedDotKeys, (err, json) => {
if (err) done(err);
json.should.deepEqual(jsonTestData.nestedDotKeys);
done();
});
});

// Test case for #184
it('should properly convert keys with escaped/nested dots in them even when they appear in an array', (done) => {
converter.csv2json(csvTestData.nestedDotKeysWithArray, (err, json) => {
if (err) done(err);
json.should.deepEqual(jsonTestData.nestedDotKeysWithArray);
done();
});
});

// Test case for #184
it('should properly convert keys with escaped/nested dots in them even when they appear in an expanded, unwound array', (done) => {
converter.csv2json(csvTestData.nestedDotKeysWithArrayExpandedUnwound, (err, json) => {
if (err) done(err);
json.should.deepEqual(jsonTestData.nestedDotKeysWithArrayExpandedUnwound);
done();
});
});
});

describe('Error Handling', () => {
Expand Down
2 changes: 2 additions & 0 deletions test/data/csv/nestedDotKeys.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a\.a,a\.b.c\.d,a\.b.c,a\.b.c\.f,a.a,a.b,a.b\.c\.d,a.f.g\.h,a.f.g\.h\.k,a.f.h.l\.m\.n\.o\.p
a,4,5,6,1,2,32,3,5,qrstuv
2 changes: 2 additions & 0 deletions test/data/csv/nestedDotKeysWithArray.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a\.a,a\.b.c\.d,a\.b.c,a\.b.c\.f,a.a,a.b,a.b\.c\.d,a.f.g\.h,a.f.g\.h\.k,a.f.h.l\.m\.n\.o\.p,c
a,4,5,6,1,2,32,3,5,qrstuv,"[{""d.e"":103,""f"":2},{""d.e"":105,""f"":5}]"
3 changes: 3 additions & 0 deletions test/data/csv/nestedDotKeysWithArrayExpandedUnwound.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a\.a,a\.b.c\.d,a\.b.c,a\.b.c\.f,a.a,a.b,a.b\.c\.d,a.f.g\.h,a.f.g\.h\.k,a.f.h.l\.m\.n\.o\.p,c.d\.e,c.f
a,4,5,6,1,2,32,3,5,qrstuv,103,2
a,4,5,6,1,2,32,3,5,qrstuv,105,5
22 changes: 22 additions & 0 deletions test/data/json/nestedDotKeys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"a.a": "a",
"a.b": {
"c.d": 4,
"c": 5,
"c.f": 6
},
"a": {
"a": 1,
"b": 2,
"b.c.d": 32,
"f": {
"g.h": 3,
"g.h.k": 5,
"h": {
"l.m.n.o.p": "qrstuv"
}
}
}
}
]
26 changes: 26 additions & 0 deletions test/data/json/nestedDotKeysWithArray.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"a.a": "a",
"a.b": {
"c.d": 4,
"c": 5,
"c.f": 6
},
"a": {
"a": 1,
"b": 2,
"b.c.d": 32,
"f": {
"g.h": 3,
"g.h.k": 5,
"h": {
"l.m.n.o.p": "qrstuv"
}
}
},
"c": [
{ "d.e" : 103, "f": 2 },
{ "d.e" : 105, "f": 5 }
]
}
]
44 changes: 44 additions & 0 deletions test/data/json/nestedDotKeysWithArrayExpandedUnwound.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"a.a": "a",
"a.b": {
"c.d": 4,
"c": 5,
"c.f": 6
},
"a": {
"a": 1,
"b": 2,
"b.c.d": 32,
"f": {
"g.h": 3,
"g.h.k": 5,
"h": {
"l.m.n.o.p": "qrstuv"
}
}
},
"c": { "d.e" : 103, "f": 2 }
},
{
"a.a": "a",
"a.b": {
"c.d": 4,
"c": 5,
"c.f": 6
},
"a": {
"a": 1,
"b": 2,
"b.c.d": 32,
"f": {
"g.h": 3,
"g.h.k": 5,
"h": {
"l.m.n.o.p": "qrstuv"
}
}
},
"c": { "d.e" : 105, "f": 5 }
}
]
34 changes: 34 additions & 0 deletions test/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ function runTests(jsonTestData, csvTestData) {
done();
});
});

// Test case for #184
it('should properly handle keys with nested dots in them', (done) => {
converter.json2csv(jsonTestData.nestedDotKeys, (err, csv) => {
if (err) done(err);
csv.should.equal(csvTestData.nestedDotKeys);
done();
});
});

// Test case for #184
it('should properly handle keys with nested dots in them even when they appear in array fields', (done) => {
converter.json2csv(jsonTestData.nestedDotKeysWithArray, (err, csv) => {
if (err) done(err);
csv.should.equal(csvTestData.nestedDotKeysWithArray);
done();
});
});
});

describe('Error Handling', () => {
Expand Down Expand Up @@ -637,6 +655,22 @@ function runTests(jsonTestData, csvTestData) {
wrapBooleans: true
});
});

// Test case for #184
it('should handle keys with nested dots when expanding and unwinding arrays', (done) => {
converter.json2csv(jsonTestData.nestedDotKeysWithArrayExpandedUnwound, (err, csv) => {
if (err) done(err);

// Replace raw boolean values with quoted versions
let expectedCsv = csvTestData.nestedDotKeysWithArrayExpandedUnwound;

csv.should.equal(expectedCsv);
done();
}, {
expandArrayObjects: true,
unwindArrays: true
});
});
});
});

Expand Down

0 comments on commit bd1fd28

Please sign in to comment.