CSV file loading supports file separator
This commit is contained in:
@@ -2,7 +2,7 @@ this.recline = this.recline || {};
|
|||||||
this.recline.Backend = this.recline.Backend || {};
|
this.recline.Backend = this.recline.Backend || {};
|
||||||
|
|
||||||
(function($, my) {
|
(function($, my) {
|
||||||
my.loadFromCSVFile = function(file, callback) {
|
my.loadFromCSVFile = function(file, callback, options) {
|
||||||
var metadata = {
|
var metadata = {
|
||||||
id: file.name,
|
id: file.name,
|
||||||
file: file
|
file: file
|
||||||
@@ -19,7 +19,7 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
reader.readAsText(file);
|
reader.readAsText(file);
|
||||||
};
|
};
|
||||||
|
|
||||||
my.csvToDataset = function(csvString) {
|
my.csvToDataset = function(csvString, options) {
|
||||||
var out = my.parseCSV(csvString);
|
var out = my.parseCSV(csvString);
|
||||||
fields = _.map(out[0], function(cell) {
|
fields = _.map(out[0], function(cell) {
|
||||||
return { id: cell, label: cell };
|
return { id: cell, label: cell };
|
||||||
@@ -44,14 +44,19 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
// @type Array
|
// @type Array
|
||||||
//
|
//
|
||||||
// @param {String} s The string to convert
|
// @param {String} s The string to convert
|
||||||
// @param {Boolean} [trm=false] If set to True leading and trailing whitespace is stripped off of each non-quoted field as it is imported
|
// @param {Object} options Options for loading CSV including
|
||||||
//
|
// @param {Boolean} [trim=false] If set to True leading and trailing whitespace is stripped off of each non-quoted field as it is imported
|
||||||
|
// @param {String} [separator=','] Separator for CSV file
|
||||||
// Heavily based on uselesscode's JS CSV parser (MIT Licensed):
|
// Heavily based on uselesscode's JS CSV parser (MIT Licensed):
|
||||||
// thttp://www.uselesscode.org/javascript/csv/
|
// thttp://www.uselesscode.org/javascript/csv/
|
||||||
my.parseCSV= function(s, trm) {
|
my.parseCSV= function(s, options) {
|
||||||
// Get rid of any trailing \n
|
// Get rid of any trailing \n
|
||||||
s = chomp(s);
|
s = chomp(s);
|
||||||
|
|
||||||
|
var options = options || {};
|
||||||
|
var trm = options.trim;
|
||||||
|
var separator = options.separator || ',';
|
||||||
|
|
||||||
var cur = '', // The character we are currently processing.
|
var cur = '', // The character we are currently processing.
|
||||||
inQuote = false,
|
inQuote = false,
|
||||||
fieldQuoted = false,
|
fieldQuoted = false,
|
||||||
@@ -85,7 +90,7 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
cur = s.charAt(i);
|
cur = s.charAt(i);
|
||||||
|
|
||||||
// If we are at a EOF or EOR
|
// If we are at a EOF or EOR
|
||||||
if (inQuote === false && (cur === ',' || cur === "\n")) {
|
if (inQuote === false && (cur === separator || cur === "\n")) {
|
||||||
field = processField(field);
|
field = processField(field);
|
||||||
// Add the current field to the current row
|
// Add the current field to the current row
|
||||||
row.push(field);
|
row.push(field);
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ test("parseCSV", function() {
|
|||||||
var csv = '"Jones, Jay", 10\n' +
|
var csv = '"Jones, Jay", 10\n' +
|
||||||
'"Xyz ""ABC"" O\'Brien", 11:35\n' +
|
'"Xyz ""ABC"" O\'Brien", 11:35\n' +
|
||||||
'"Other, AN", 12:35\n';
|
'"Other, AN", 12:35\n';
|
||||||
var array = recline.Backend.parseCSV(csv, true);
|
var array = recline.Backend.parseCSV(csv, {trim : true});
|
||||||
deepEqual(exp, array);
|
deepEqual(exp, array);
|
||||||
|
|
||||||
var csv = 'Name, Value\n' +
|
var csv = 'Name, Value\n' +
|
||||||
@@ -29,4 +29,19 @@ test("parseCSV", function() {
|
|||||||
equal(dataset.currentDocuments.length, 3);
|
equal(dataset.currentDocuments.length, 3);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("parseCSVsemicolon", function() {
|
||||||
|
var csv = '"Jones; Jay";10\n' +
|
||||||
|
'"Xyz ""ABC"" O\'Brien";11:35\n' +
|
||||||
|
'"Other; AN";12:35\n';
|
||||||
|
|
||||||
|
var array = recline.Backend.parseCSV(csv, {separator : ';'});
|
||||||
|
var exp = [
|
||||||
|
['Jones; Jay', 10],
|
||||||
|
['Xyz "ABC" O\'Brien', '11:35' ],
|
||||||
|
['Other; AN', '12:35' ]
|
||||||
|
];
|
||||||
|
deepEqual(exp, array);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
})(this.jQuery);
|
})(this.jQuery);
|
||||||
|
|||||||
Reference in New Issue
Block a user