[whitespace][xs]: .
This commit is contained in:
@@ -35,128 +35,128 @@ this.recline.Backend = this.recline.Backend || {};
|
|||||||
return dataset;
|
return dataset;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Converts a Comma Separated Values string into an array of arrays.
|
// Converts a Comma Separated Values string into an array of arrays.
|
||||||
// Each line in the CSV becomes an array.
|
// Each line in the CSV becomes an array.
|
||||||
//
|
//
|
||||||
// Empty fields are converted to nulls and non-quoted numbers are converted to integers or floats.
|
// Empty fields are converted to nulls and non-quoted numbers are converted to integers or floats.
|
||||||
//
|
//
|
||||||
// @return The CSV parsed as an array
|
// @return The CSV parsed as an array
|
||||||
// @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 {Boolean} [trm=false] If set to True leading and trailing whitespace is stripped off of each non-quoted field as it is imported
|
||||||
//
|
//
|
||||||
// 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, trm) {
|
||||||
// Get rid of any trailing \n
|
// Get rid of any trailing \n
|
||||||
s = chomp(s);
|
s = chomp(s);
|
||||||
|
|
||||||
var cur = '', // The character we are currently processing.
|
var cur = '', // The character we are currently processing.
|
||||||
inQuote = false,
|
inQuote = false,
|
||||||
fieldQuoted = false,
|
fieldQuoted = false,
|
||||||
field = '', // Buffer for building up the current field
|
field = '', // Buffer for building up the current field
|
||||||
row = [],
|
row = [],
|
||||||
out = [],
|
out = [],
|
||||||
i,
|
i,
|
||||||
processField;
|
processField;
|
||||||
|
|
||||||
processField = function (field) {
|
processField = function (field) {
|
||||||
if (fieldQuoted !== true) {
|
if (fieldQuoted !== true) {
|
||||||
// If field is empty set to null
|
// If field is empty set to null
|
||||||
if (field === '') {
|
if (field === '') {
|
||||||
field = null;
|
field = null;
|
||||||
// If the field was not quoted and we are trimming fields, trim it
|
// If the field was not quoted and we are trimming fields, trim it
|
||||||
} else if (trm === true) {
|
} else if (trm === true) {
|
||||||
field = trim(field);
|
field = trim(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert unquoted numbers to their appropriate types
|
// Convert unquoted numbers to their appropriate types
|
||||||
if (rxIsInt.test(field)) {
|
if (rxIsInt.test(field)) {
|
||||||
field = parseInt(field, 10);
|
field = parseInt(field, 10);
|
||||||
} else if (rxIsFloat.test(field)) {
|
} else if (rxIsFloat.test(field)) {
|
||||||
field = parseFloat(field, 10);
|
field = parseFloat(field, 10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return field;
|
return field;
|
||||||
};
|
};
|
||||||
|
|
||||||
for (i = 0; i < s.length; i += 1) {
|
for (i = 0; i < s.length; i += 1) {
|
||||||
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 === ',' || 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);
|
||||||
// If this is EOR append row to output and flush row
|
// If this is EOR append row to output and flush row
|
||||||
if (cur === "\n") {
|
if (cur === "\n") {
|
||||||
out.push(row);
|
out.push(row);
|
||||||
row = [];
|
row = [];
|
||||||
}
|
}
|
||||||
// Flush the field buffer
|
// Flush the field buffer
|
||||||
field = '';
|
field = '';
|
||||||
fieldQuoted = false;
|
fieldQuoted = false;
|
||||||
} else {
|
} else {
|
||||||
// If it's not a ", add it to the field buffer
|
// If it's not a ", add it to the field buffer
|
||||||
if (cur !== '"') {
|
if (cur !== '"') {
|
||||||
field += cur;
|
field += cur;
|
||||||
} else {
|
} else {
|
||||||
if (!inQuote) {
|
if (!inQuote) {
|
||||||
// We are not in a quote, start a quote
|
// We are not in a quote, start a quote
|
||||||
inQuote = true;
|
inQuote = true;
|
||||||
fieldQuoted = true;
|
fieldQuoted = true;
|
||||||
} else {
|
} else {
|
||||||
// Next char is ", this is an escaped "
|
// Next char is ", this is an escaped "
|
||||||
if (s.charAt(i + 1) === '"') {
|
if (s.charAt(i + 1) === '"') {
|
||||||
field += '"';
|
field += '"';
|
||||||
// Skip the next char
|
// Skip the next char
|
||||||
i += 1;
|
i += 1;
|
||||||
} else {
|
} else {
|
||||||
// It's not escaping, so end quote
|
// It's not escaping, so end quote
|
||||||
inQuote = false;
|
inQuote = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the last field
|
// Add the last field
|
||||||
field = processField(field);
|
field = processField(field);
|
||||||
row.push(field);
|
row.push(field);
|
||||||
out.push(row);
|
out.push(row);
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
};
|
};
|
||||||
|
|
||||||
var rxIsInt = /^\d+$/,
|
var rxIsInt = /^\d+$/,
|
||||||
rxIsFloat = /^\d*\.\d+$|^\d+\.\d*$/,
|
rxIsFloat = /^\d*\.\d+$|^\d+\.\d*$/,
|
||||||
// If a string has leading or trailing space,
|
// If a string has leading or trailing space,
|
||||||
// contains a comma double quote or a newline
|
// contains a comma double quote or a newline
|
||||||
// it needs to be quoted in CSV output
|
// it needs to be quoted in CSV output
|
||||||
rxNeedsQuoting = /^\s|\s$|,|"|\n/,
|
rxNeedsQuoting = /^\s|\s$|,|"|\n/,
|
||||||
trim = (function () {
|
trim = (function () {
|
||||||
// Fx 3.1 has a native trim function, it's about 10x faster, use it if it exists
|
// Fx 3.1 has a native trim function, it's about 10x faster, use it if it exists
|
||||||
if (String.prototype.trim) {
|
if (String.prototype.trim) {
|
||||||
return function (s) {
|
return function (s) {
|
||||||
return s.trim();
|
return s.trim();
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
return function (s) {
|
return function (s) {
|
||||||
return s.replace(/^\s*/, '').replace(/\s*$/, '');
|
return s.replace(/^\s*/, '').replace(/\s*$/, '');
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
|
||||||
function chomp(s) {
|
function chomp(s) {
|
||||||
if (s.charAt(s.length - 1) !== "\n") {
|
if (s.charAt(s.length - 1) !== "\n") {
|
||||||
// Does not end with \n, just return string
|
// Does not end with \n, just return string
|
||||||
return s;
|
return s;
|
||||||
} else {
|
} else {
|
||||||
// Remove the \n
|
// Remove the \n
|
||||||
return s.substring(0, s.length - 1);
|
return s.substring(0, s.length - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}(jQuery, this.recline.Backend));
|
}(jQuery, this.recline.Backend));
|
||||||
|
|||||||
Reference in New Issue
Block a user