Skip to content

Commit 54508cb

Browse files
committed
migrated path lib to a new module, work on json-2-csv, updated coverage, rev is up to 1.2.0
1 parent ef4bc3e commit 54508cb

File tree

8 files changed

+18
-184
lines changed

8 files changed

+18
-184
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ $ npm run coverage
144144

145145
Current Coverage is:
146146
```
147-
Statements : 94.94% ( 150/158 )
148-
Branches : 90.43% ( 85/94 )
149-
Functions : 100% ( 33/33 )
150-
Lines : 97.22% ( 140/144 )
147+
Statements : 94.49% ( 120/127 )
148+
Branches : 90.24% ( 74/82 )
149+
Functions : 100% ( 30/30 )
150+
Lines : 97.39% ( 112/115 )
151151
```
152152

153153
## Features

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.3",
3+
"version": "1.2.0",
44
"homepage": "https://github.com/mrodrig/json-2-csv",
55
"moduleType": [
66
"node"

lib/csv-2-json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var _ = require('underscore'),
4-
path = require('./path'),
4+
path = require('doc-path'),
55
constants = require('./constants');
66

77
var options = {}; // Initialize the options - this will be populated when the csv2json function is called.

lib/json-2-csv.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var _ = require('underscore'),
44
constants = require('./constants'),
5+
path = require('doc-path'),
56
Promise = require('bluebird');
67

78
var options = {}; // Initialize the options - this will be populated when the json2csv function is called.
@@ -76,21 +77,11 @@ var generateDocumentHeading = function(heading, data) {
7677
* @returns {Array}
7778
*/
7879
var convertData = function (data, keys) {
79-
var output = [], // Array of CSV representing converted docs
80-
value; // Temporary variable to store the current data
81-
82-
_.each(keys, function (key) { // For each key
83-
var indexOfPeriod = _.indexOf(key, '.');
84-
if (indexOfPeriod > -1) {
85-
var pathPrefix = key.slice(0, indexOfPeriod),
86-
pathRemainder = key.slice(indexOfPeriod+1);
87-
output.push(convertData(data[pathPrefix], [pathRemainder]));
88-
} else if (keys.indexOf(key) > -1) { // If the keys contain the current key, then process the data
89-
value = data[key]; // Set the current data that we are looking at
90-
output.push(convertField(value, output));
91-
}
92-
});
93-
return output; // Return the data joined by our field delimiter
80+
// Reduce each key in the data to its CSV value
81+
return _.reduce(keys, function (output, key) {
82+
// Add the CSV representation of the data at the key in the document to the output array
83+
return output.concat(convertField(path.evaluatePath(data, key)));
84+
}, []);
9485
};
9586

9687
/**
@@ -117,9 +108,9 @@ var convertField = function (value) {
117108
*/
118109
var generateCsv = function (data, headingKeys) {
119110
// Reduce each JSON document in data to a CSV string and append it to the CSV accumulator
120-
return Promise.resolve([headingKeys, _.reduce(data, function (csv, doc) {
121-
return csv += _.flatten(convertData(doc, headingKeys)).join(options.DELIMITER.FIELD) + options.EOL;
122-
}, '')]);
111+
return [headingKeys].concat(_.reduce(data, function (csv, doc) {
112+
return csv += convertData(doc, headingKeys).join(options.DELIMITER.FIELD) + options.EOL;
113+
}, ''));
123114
};
124115

125116
module.exports = {

lib/path.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 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.3",
5+
"version": "1.2.0",
66
"repository": {
77
"type": "git",
88
"url": "http://github.com/mrodrig/json-2-csv.git"
@@ -25,6 +25,7 @@
2525
],
2626
"dependencies": {
2727
"underscore": "~1.8.3",
28+
"doc-path": "~1.0.0",
2829
"bluebird": "~2.9.24"
2930
},
3031
"devDependencies": {

test/testPath.js

Lines changed: 0 additions & 117 deletions
This file was deleted.

test/tests.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
var json2csvTests = require('./testJson2Csv'),
2-
csv2jsonTests = require('./testCsv2Json'),
3-
pathTests = require('./testPath');
2+
csv2jsonTests = require('./testCsv2Json');
43

54
describe('json-2-csv Module', function() {
65
json2csvTests.runTests();
76
csv2jsonTests.runTests();
8-
pathTests.runTests();
97
});

0 commit comments

Comments
 (0)