Skip to content

Commit fac7f7e

Browse files
committed
Merge pull request #3 from dillten/master
Pulling in dillten's changes to add wrap capabilities
2 parents d164805 + bd9b99b commit fac7f7e

14 files changed

+279
-4
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Created by .gitignore support plugin (hsz.mobi)
2+
### Node template
3+
# Logs
4+
logs
5+
*.log
6+
7+
# Runtime data
8+
pids
9+
*.pid
10+
*.seed
11+
12+
# Directory for instrumented libs generated by jscoverage/JSCover
13+
lib-cov
14+
15+
# Coverage directory used by tools like istanbul
16+
coverage
17+
18+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
19+
.grunt
20+
21+
# Compiled binary addons (http://nodejs.org/api/addons.html)
22+
build/Release
23+
24+
# Dependency directory
25+
# Commenting this out is preferred by some people, see
26+
# https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
27+
node_modules
28+
29+
# Users Environment Variables
30+
.lock-wscript
31+
32+
.idea
33+

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test/
2+
.git*
3+
.travis.yml

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var converter = require('json-2-csv');
3333
* `DELIMITER` - Document - Specifies the different types of delimiters
3434
* `FIELD` - String - Field Delimiter. Default: `','`
3535
* `ARRAY` - String - Array Value Delimiter. Default: `';'`
36+
* `WRAP` - String - Wrap values in the delimiter of choice (e.g. wrap values in quotes). Default: `''`
3637
* `EOL` - String - End of Line Delimiter. Default: `'\n'`
3738
* `PARSE_CSV_NUMBERS` - Boolean - Should numbers that are found in the CSV be converted to numbers? Default: `false`
3839

lib/converter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ var json2Csv = require('./json-2-csv'), // Require our json-2-csv code
88
var defaultOptions = {
99
DELIMITER : {
1010
FIELD : ',',
11-
ARRAY : ';'
11+
ARRAY : ';',
12+
WRAP : ''
1213
},
1314
EOL : '\n',
1415
PARSE_CSV_NUMBERS : false

lib/json-2-csv.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var retrieveSubHeading = function (heading, data) {
1515
if (typeof data[subKey] === 'object' && data[subKey] !== null && typeof data[subKey].length === 'undefined') { // If we have another nested document
1616
subKeys[indx] = retrieveSubHeading(newKey, data[subKey]); // Recur on the subdocument to retrieve the full key name
1717
} else {
18-
subKeys[indx] = newKey; // Set the key name since we don't have a sub document
18+
subKeys[indx] = (options.DELIMITER.WRAP || '') + (newKey || '') + (options.DELIMITER.WRAP || ''); // Set the key name since we don't have a sub document
1919
}
2020
});
2121
return subKeys.join(options.DELIMITER.FIELD); // Return the headings joined by our field delimiter
@@ -49,9 +49,9 @@ var convertData = function (data, keys) {
4949
if (typeof value === 'object' && value !== null && typeof value.length === 'undefined') { // If we have an object
5050
output.push(convertData(value, _.keys(value))); // Push the recursively generated CSV
5151
} else if (typeof value === 'object' && value !== null && typeof value.length === 'number') { // We have an array of values
52-
output.push('[' + value.join(options.DELIMITER.ARRAY) + ']');
52+
output.push((options.DELIMITER.WRAP || '') + '[' + value.join(options.DELIMITER.ARRAY) + ']' + (options.DELIMITER.WRAP || ''));
5353
} else {
54-
output.push(value); // Otherwise push the current value
54+
output.push((options.DELIMITER.WRAP || '') + (value || '') + (options.DELIMITER.WRAP || '')); // Otherwise push the current value
5555
}
5656
}
5757
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"info.name","coursesTaken","year"
2+
"Mike","[CS2500/CS2510]","Sophomore"
3+
"John","[ANTH1101/POL2312/MATH2142/POL3305/LAW2100]","Senior"
4+
"Joe","[]","Freshman"

test/CSV/withQuotes/nestedJson.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"carModel","priceRange.min","priceRange.max"
2+
"Audi","9000","11000"
3+
"BMW","14000","16000"
4+
"Mercedes","19000","21000"
5+
"Porsche","29000","31000"

test/CSV/withQuotes/nestedJson2.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"Make","Model","Year","Specifications.Mileage","Specifications.Trim"
2+
"Nissan","Murano","2013","7106","S AWD"
3+
"BMW","X5","2014","3287","M"

test/CSV/withQuotes/nestedQuotes.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"a string"
2+
"with a description"
3+
"with a description and "quotes""

test/CSV/withQuotes/noData.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

0 commit comments

Comments
 (0)