@@ -40,7 +40,7 @@
Clear
-
+
diff --git a/js/csvjson/csv2json.js b/js/csvjson/csv2json.js
index 2003534..e4fa7a0 100644
--- a/js/csvjson/csv2json.js
+++ b/js/csvjson/csv2json.js
@@ -9,19 +9,19 @@
* - separator: Optional. Character which acts as separator. If omitted,
* will attempt to detect comma (,), semi-colon (;) or tab (\t).
*
- * Dependencies:
+ * Dependencies:
* - underscore (http://underscorejs.org/)
* - underscore.string (https://github.com/epeli/underscore.string)
*
* Copyright (c) 2014 Martin Drapeau
*
*/
-
+
var errorDetectingSeparator = "We could not detect the separator.",
errorEmpty = "Please upload a file or type in something.",
errorEmptyHeader = "Could not detect header. Ensure first row cotains your column headers.",
separators = [",", ";", "\t"];
-
+
function detectSeparator(csv) {
var counts = {},
sepMax;
@@ -103,6 +103,7 @@
} else {
// We found a non-quoted value.
+ // but escape any internal quotes
strMatchedValue = arrMatches[ 3 ];
}
@@ -116,11 +117,11 @@
// Return the parsed data.
return( arrData );
}
-
+
function convert(csv, options) {
options || (options = {});
if (csv.length == 0) throw errorEmpty;
-
+
var separator = options.separator || detectSeparator(csv);
if (!separator) throw errorDetectingSeparator;
@@ -134,13 +135,13 @@
keys = _.map(keys, function(key) {
return _(key).chain().trim().trim('"').value();
});
-
+
var json = options.hash ? {} : [];
for (var l = 0; l < a.length; l++) {
var row = {},
hashKey;
for (var i = 0; i < keys.length; i++) {
- var value = _(a[l][i]).chain().trim().trim('"').value(),
+ var value = _(a[l][i]).chain().trim().value(),
number = value === "" ? NaN : value - 0;
if (options.hash && i == 0)
hashKey = value;
@@ -152,11 +153,11 @@
else
json.push(row);
}
-
+
return json;
};
-
+
this.CSVJSON || (this.CSVJSON = {});
this.CSVJSON.csv2json = convert;
-
+
}).call(this);
\ No newline at end of file
diff --git a/js/src/csv2json.js b/js/src/csv2json.js
index 29b8863..244593d 100644
--- a/js/src/csv2json.js
+++ b/js/src/csv2json.js
@@ -4,7 +4,7 @@
* Copyright (c) 2014 Martin Drapeau
*/
APP.csv2json = function() {
-
+
var uploadUrl = '/csv2json/upload',
sepMap = {
comma: ',',
@@ -15,25 +15,27 @@ APP.csv2json = function() {
$separator = $('select[name=separator]'),
$parseNumbers = $('input[type=checkbox][name=parseNumbers]'),
$transpose = $('input[type=checkbox][name=transpose]'),
+ $collapse = $('input[type=checkbox][name=collapse]'),
$output = $('input[type=radio][name=output]'),
$csv = $('#csv'),
$json = $('#json'),
$clear = $('#clear, a.clear'),
$convert = $('#convert, a.convert');
-
+
$convert.click(function(e) {
e.preventDefault();
-
+
var csv = _.trim($csv.val()),
separator = $separator.find('option:selected').val(),
options = {
transpose: $transpose.is(':checked'),
+ collapse: $collapse.is(':checked'),
hash: $output.filter(':checked').val() == 'hash',
- parseNumbers: $parseNumbers.is(':checked')
+ parseNumbers: $parseNumbers.is(':checked'),
},
json;
if (separator != 'auto') options.separator = sepMap[separator];
-
+
try {
console.log(options);
json = CSVJSON.csv2json(csv, options);
@@ -41,11 +43,13 @@ APP.csv2json = function() {
APP.reportError($json, error);
return false;
}
-
- var result = JSON.stringify(json, null, 2);
+
+ var result = options.collapse
+ ? JSON.stringify(json)
+ : JSON.stringify(json, null, 2);
$json.removeClass('error').val(result);
});
-
+
APP.start({
$convert: $convert,
$clear: $clear,