Skip to content

Commit beb388b

Browse files
committed
Adding tests for key specification, updating to 1.1.2, updated README
1 parent 64abcfc commit beb388b

11 files changed

+356
-22
lines changed

README.md

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ var converter = require('json-2-csv');
4040
##### json2csv Example:
4141

4242
```javascript
43-
4443
var converter = require('json-2-csv');
4544

4645
var documents = [
@@ -70,7 +69,6 @@ var json2csvCallback = function (err, csv) {
7069
};
7170

7271
converter.json2csv(documents, json2csvCallback);
73-
7472
```
7573

7674
The above code prints out:
@@ -90,7 +88,10 @@ BMW,X5,2014,3287,M
9088
* `FIELD` - String - Field Delimiter. Default: `','`
9189
* `ARRAY` - String - Array Value Delimiter. Default: `';'`
9290
* `EOL` - String - End of Line Delimiter. Default: `'\n'`
93-
* `PARSE_CSV_NUMBERS` - Boolean - Should numbers that are found in the CSV be converted to numbers? Default: `false`
91+
* `PARSE_CSV_NUMBERS` - Boolean - (TODO) Should numbers that are found in the CSV be converted to numbers? Default: `false`
92+
* `KEYS` - Array - Specify the keys (as strings) that should be converted. Default: `null`
93+
* If you have a nested object (ie. {info : {name: 'Mike'}}), then set options.KEYS to ['info.name']
94+
* If you want all keys to be converted, then specify ```null``` or don't specify the option to utilize the default.
9495

9596
##### csv2json Example:
9697

@@ -137,19 +138,72 @@ _Note_: This requires `mocha`, `should`, `async`, and `underscore`.
137138
## Features
138139

139140
- Header Generation (per document keys)
141+
- Allows for conversion of specific keys in both json2csv and csv2json via the options.KEYS parameter (as of 1.1.2)
140142
- Verifies all documents have same schema (schema field order does not matter as of 1.1.0)
141143
- Supports sub-documents natively
142144
- Supports arrays as document values for both json2csv and csv2json
143145
- Custom ordering of columns (see F.A.Q. for more information)
144146
- Ability to re-generate the JSON documents that were used to generate the CSV (including nested documents)
145147
- Allows for custom field delimiters, end of line delimiters, etc.
146-
- Promisifiable via bluebird's .promisify(<function) and .promisifyAll() (as of 1.1.1)
148+
- Promisifiable via bluebird's .promisify(<function>) and .promisifyAll(<object>) (as of 1.1.1)
147149

148150
## F.A.Q.
149151

150152
- Can the order of the keys be changed in the output?
151153
__Yes.__ Currently, changing the order of the keys in the JSON document will also change the order of the columns. (Tested on Node 10.xx)
152154

155+
- Can I specify the keys that I would like to have converted to CSV or JSON?
156+
__Yes.__ This is currently supported for both json2csv and csv2json. Specify the keys in options.KEYS. For example,
157+
158+
```javascript
159+
var converter = require('json-2-csv');
160+
161+
var options = {
162+
KEYS : ['info.name', 'year']
163+
};
164+
165+
var documents = [
166+
{
167+
"info": {
168+
"name": "Mike"
169+
},
170+
"coursesTaken": ["CS2500", "CS2510"],
171+
"year": "Sophomore"
172+
},
173+
{
174+
"info": {
175+
"name": "John"
176+
},
177+
"coursesTaken": ["ANTH1101", "POL2312", "MATH2142", "POL3305", "LAW2100"],
178+
"year": "Senior"
179+
},
180+
{
181+
"info": {
182+
"name": "Joe"
183+
},
184+
"coursesTaken": [],
185+
"year": "Freshman"
186+
}
187+
];
188+
189+
converter.json2csv(documents, function (err, csv) {
190+
if (!err) {
191+
return console.log(csv);
192+
}
193+
throw err;
194+
}, options);
195+
```
196+
197+
This prints out:
198+
199+
```csv
200+
info.name,year
201+
Mike,Sophomore
202+
John,Senior
203+
Joe,Freshman
204+
205+
```
206+
153207
## Milestones
154208
- Created: Apr 23, 2014
155209
- 1K Downloads/Month: January 15, 2015

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-2-csv",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"homepage": "https://github.com/mrodrig/json-2-csv",
55
"moduleType": [
66
"node"

lib/csv-2-json.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ var convertCSV = function (lines, callback) {
8989
_.each(lines, function (line) { // For each line, create the document and add it to the array of documents
9090
jsonDocs.push(createDoc(headers, line));
9191
});
92-
return _.filter(jsonDocs, function (doc) { return doc !== false; });; // Return all non 'falsey' values to filter blank lines
92+
return _.filter(jsonDocs, function (doc) { return doc !== false; }); // Return all non 'falsey' values to filter blank lines
9393
};
9494

9595
module.exports = {
@@ -109,4 +109,4 @@ module.exports = {
109109
callback(null, json); // Send the data back to the caller
110110
}
111111

112-
};
112+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "mrodrig",
33
"name": "json-2-csv",
44
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
5-
"version": "1.1.1",
5+
"version": "1.1.2",
66
"repository": {
77
"type": "git",
88
"url": "http://github.com/mrodrig/json-2-csv.git"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
info.name,year
2+
Mike,Sophomore
3+
John,Senior
4+
Joe,Freshman
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"info.name","year"
2+
"Mike","Sophomore"
3+
"John","Senior"
4+
"Joe","Freshman"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"info": {
4+
"name": "Mike"
5+
},
6+
"year": "Sophomore"
7+
},
8+
{
9+
"info": {
10+
"name": "John"
11+
},
12+
"year": "Senior"
13+
},
14+
{
15+
"info": {
16+
"name": "Joe"
17+
},
18+
"year": "Freshman"
19+
}
20+
]

test/testComma.js

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var json_regularJson = require('./JSON/regularJson'),
2121
json_noData = require('./JSON/noData.json'),
2222
json_singleDoc = require('./JSON/singleDoc.json'),
2323
json_arrayValue = require('./JSON/arrayValueDocs.json'),
24+
json_arrayValue_specificKeys = require('./JSON/arrayValueDocs_specificKeys.json'),
2425
json_sameSchemaDifferentOrdering = require('./JSON/sameSchemaDifferentOrdering'),
2526
json_differentSchemas = require('./JSON/differentSchemas'),
2627
csv_regularJson = '',
@@ -29,7 +30,8 @@ var json_regularJson = require('./JSON/regularJson'),
2930
csv_nestedQuotes = '',
3031
csv_noData = '',
3132
csv_singleDoc = '',
32-
csv_arrayValue = '';
33+
csv_arrayValue = '',
34+
csv_arrayValue_specificKeys = '';
3335

3436
var json2csvTests = function () {
3537
describe('json2csv - non-promisified', function (done) {
@@ -90,6 +92,16 @@ var json2csvTests = function () {
9092
}, options);
9193
});
9294

95+
it('should parse the specified keys to CSV', function (done) {
96+
// Create a copy so we don't modify the actual options object
97+
var opts = _.extend(JSON.parse(JSON.stringify(options)), {KEYS: ['info.name', 'year']});
98+
converter.json2csv(json_arrayValue, function (err, csv) {
99+
csv.should.equal(csv_arrayValue_specificKeys);
100+
csv.split(options.EOL).length.should.equal(5);
101+
done();
102+
}, opts);
103+
});
104+
93105
it('should parse an array of JSON documents with the same schema but different ordering of fields', function (done) {
94106
converter.json2csv(json_sameSchemaDifferentOrdering, function (err, csv) {
95107
csv.should.equal(csv_regularJson);
@@ -510,6 +522,20 @@ var json2csvTests = function () {
510522
});
511523
});
512524

525+
it('should parse the specified keys to CSV', function (done) {
526+
// Create a copy so we don't modify the actual options object
527+
var opts = _.extend(JSON.parse(JSON.stringify(options)), {KEYS: ['info.name', 'year']});
528+
converter.json2csvAsync(json_arrayValue, opts)
529+
.then(function (csv) {
530+
csv.should.equal(csv_arrayValue_specificKeys);
531+
csv.split(options.EOL).length.should.equal(5);
532+
done();
533+
})
534+
.catch(function (err) {
535+
throw err;
536+
});
537+
});
538+
513539
it('should parse an array of JSON documents with the same schema but different ordering of fields', function (done) {
514540
converter.json2csvAsync(json_sameSchemaDifferentOrdering)
515541
.then(function (csv) {
@@ -617,6 +643,15 @@ var csv2jsonTests = function () {
617643
}, options);
618644
});
619645

646+
it('should parse the specified keys to JSON', function (done) {
647+
var opts = _.extend(JSON.parse(JSON.stringify(options)), {KEYS : ['info.name', 'year']});
648+
converter.csv2jsonAsync(csv_arrayValue.replace(/,/g, options.DELIMITER.FIELD), function (err, json) {
649+
var isEqual = _.isEqual(json, json_arrayValue_specificKeys);
650+
true.should.equal(isEqual);
651+
done();
652+
}, opts);
653+
});
654+
620655
it('should throw an error about not having been passed data - 1', function (done) {
621656
converter.csv2json(null, function (err, json) {
622657
err.message.should.equal('Cannot call csv2json on null.');
@@ -876,6 +911,19 @@ var csv2jsonTests = function () {
876911
});
877912
});
878913

914+
it('should parse the specified keys to JSON', function (done) {
915+
var opts = _.extend(JSON.parse(JSON.stringify(options)), {KEYS : ['info.name', 'year']});
916+
converter.csv2jsonAsync(csv_arrayValue.replace(/,/g, options.DELIMITER.FIELD), opts)
917+
.then(function (json) {
918+
var isEqual = _.isEqual(json, json_arrayValue_specificKeys);
919+
true.should.equal(isEqual);
920+
done();
921+
})
922+
.catch(function (err) {
923+
throw err;
924+
});
925+
});
926+
879927
it('should throw an error about not having been passed data - 1', function (done) {
880928
converter.csv2jsonAsync(null, options)
881929
.then(function (json) {
@@ -1063,6 +1111,13 @@ module.exports = {
10631111
csv_arrayValue = data.toString();
10641112
callback(null);
10651113
});
1114+
},
1115+
function(callback) {
1116+
fs.readFile('test/CSV/arrayValueDocs_specificKeys.csv', function (err, data) {
1117+
if (err) callback(err);
1118+
csv_arrayValue_specificKeys = data.toString();
1119+
callback(null);
1120+
});
10661121
}
10671122
],
10681123
function(err, results) {

test/testQuoted.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var json_regularJson = require('./JSON/regularJson'),
2323
json_noData = require('./JSON/noData.json'),
2424
json_singleDoc = require('./JSON/singleDoc.json'),
2525
json_arrayValue = require('./JSON/arrayValueDocs.json'),
26+
json_arrayValue_specificKeys = require('./JSON/arrayValueDocs_specificKeys.json'),
2627
json_sameSchemaDifferentOrdering = require('./JSON/sameSchemaDifferentOrdering'),
2728
json_differentSchemas = require('./JSON/differentSchemas'),
2829
csv_regularJson = '',
@@ -32,7 +33,8 @@ var json_regularJson = require('./JSON/regularJson'),
3233
csv_nestedComma = '',
3334
csv_noData = '',
3435
csv_singleDoc = '',
35-
csv_arrayValue = '';
36+
csv_arrayValue = '',
37+
csv_arrayValue_specificKeys = '';
3638

3739
var json2csvTests = function () {
3840
describe('json2csv - non-promisified', function (done) {
@@ -101,6 +103,16 @@ var json2csvTests = function () {
101103
}, options);
102104
});
103105

106+
it('should parse the specified keys to CSV', function (done) {
107+
// Create a copy so we don't modify the actual options object
108+
var opts = _.extend(JSON.parse(JSON.stringify(options)), {KEYS: ['info.name', 'year']});
109+
converter.json2csv(json_arrayValue, function (err, csv) {
110+
csv.should.equal(csv_arrayValue_specificKeys);
111+
csv.split(options.EOL).length.should.equal(5);
112+
done();
113+
}, opts);
114+
});
115+
104116
it('should parse an array of JSON documents with the same schema but different ordering of fields', function (done) {
105117
converter.json2csv(json_sameSchemaDifferentOrdering, function (err, csv) {
106118
csv.should.equal(csv_regularJson);
@@ -258,6 +270,20 @@ var json2csvTests = function () {
258270
});
259271
});
260272

273+
it('should parse the specified keys to CSV', function (done) {
274+
// Create a copy so we don't modify the actual options object
275+
var opts = _.extend(JSON.parse(JSON.stringify(options)), {KEYS: ['info.name', 'year']});
276+
converter.json2csvAsync(json_arrayValue, opts)
277+
.then(function (csv) {
278+
csv.should.equal(csv_arrayValue_specificKeys);
279+
csv.split(options.EOL).length.should.equal(5);
280+
done();
281+
})
282+
.catch(function (err) {
283+
throw err;
284+
});
285+
});
286+
261287
it('should parse an array of JSON documents with the same schema but different ordering of fields', function (done) {
262288
converter.json2csvAsync(json_sameSchemaDifferentOrdering, options)
263289
.then(function (csv) {
@@ -366,6 +392,13 @@ module.exports = {
366392
csv_arrayValue = data.toString();
367393
callback(null);
368394
});
395+
},
396+
function(callback) {
397+
fs.readFile('test/CSV/withQuotes/arrayValueDocs_specificKeys.csv', function (err, data) {
398+
if (err) callback(err);
399+
csv_arrayValue_specificKeys = data.toString();
400+
callback(null);
401+
});
369402
}
370403
],
371404
function(err, results) {

0 commit comments

Comments
 (0)