From e1d480c9beb8063cd1f7637dd2c66afef2d8fbae Mon Sep 17 00:00:00 2001 From: Andre Hayter Date: Wed, 12 Jul 2017 16:56:37 -0400 Subject: [PATCH 1/3] fix for internally quoted values --- js/csvjson/csv2json.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) 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 From 10958c45217e6a0a20979ec735bf82459eb52014 Mon Sep 17 00:00:00 2001 From: Andre Hayter Date: Wed, 12 Jul 2017 17:13:59 -0400 Subject: [PATCH 2/3] add option to collapse json output --- application/views/csv2json_view.php | 10 +++++++--- js/src/csv2json.js | 20 ++++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/application/views/csv2json_view.php b/application/views/csv2json_view.php index 3dafaf1..587be98 100644 --- a/application/views/csv2json_view.php +++ b/application/views/csv2json_view.php @@ -4,7 +4,7 @@

Convert your CSV or TSV formatted data to JSON. Optionally transpose your data, or output an object instead of an array. Copy/paste or upload your Excel data to convert it to JSON.

- +
@@ -40,7 +40,7 @@ Clear
- +
@@ -62,10 +62,14 @@ -   +   +   +
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, From 997ed392aee15ad4cf2b5499a3d37fc7ad957395 Mon Sep 17 00:00:00 2001 From: Andre Hayter Date: Wed, 12 Jul 2017 17:18:55 -0400 Subject: [PATCH 3/3] update gitignore --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d740c49..dcb0333 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,8 @@ application/cache/* application/logs/* !application/logs/index.html -!application/logs/.htaccess \ No newline at end of file +!application/logs/.htaccess + +# Intellij IDEA +.idea +*.iml \ No newline at end of file