[#190,backend/csv][s]: rename CSV options from delimiter to quotechar and separator to delimiter - fixes #190.

This commit is contained in:
Rufus Pollock
2012-09-09 15:20:37 +01:00
parent 6e49f53a63
commit 9948c501cf
2 changed files with 26 additions and 21 deletions

View File

@@ -59,8 +59,13 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
// //
// @param {String} s The string to convert // @param {String} s The string to convert
// @param {Object} options Options for loading CSV including // @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 {Boolean} [trim=false] If set to True leading and trailing
// @param {String} [separator=','] Separator for CSV file // whitespace is stripped off of each non-quoted field as it is imported
// @param {String} [delimiter=','] A one-character string used to separate
// fields. It defaults to ','
// @param {String} [quotechar='"'] A one-character string used to quote
// fields containing special characters, such as the delimiter or
// quotechar, or which contain new-line characters. It defaults to '"'
// //
// Heavily based on uselesscode's JS CSV parser (MIT Licensed): // Heavily based on uselesscode's JS CSV parser (MIT Licensed):
// http://www.uselesscode.org/javascript/csv/ // http://www.uselesscode.org/javascript/csv/
@@ -70,8 +75,8 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
var options = options || {}; var options = options || {};
var trm = (options.trim === false) ? false : true; var trm = (options.trim === false) ? false : true;
var separator = options.separator || ','; var delimiter = options.delimiter || ',';
var delimiter = options.delimiter || '"'; var quotechar = options.quotechar || '"';
var cur = '', // The character we are currently processing. var cur = '', // The character we are currently processing.
inQuote = false, inQuote = false,
@@ -106,7 +111,7 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
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 === separator || cur === "\n")) { if (inQuote === false && (cur === delimiter || 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);
@@ -119,8 +124,8 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
field = ''; field = '';
fieldQuoted = false; fieldQuoted = false;
} else { } else {
// If it's not a delimiter, add it to the field buffer // If it's not a quotechar, add it to the field buffer
if (cur !== delimiter) { if (cur !== quotechar) {
field += cur; field += cur;
} else { } else {
if (!inQuote) { if (!inQuote) {
@@ -128,9 +133,9 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
inQuote = true; inQuote = true;
fieldQuoted = true; fieldQuoted = true;
} else { } else {
// Next char is delimiter, this is an escaped delimiter // Next char is quotechar, this is an escaped quotechar
if (s.charAt(i + 1) === delimiter) { if (s.charAt(i + 1) === quotechar) {
field += delimiter; field += quotechar;
// Skip the next char // Skip the next char
i += 1; i += 1;
} else { } else {
@@ -168,9 +173,9 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
// ... // more attributes we do not care about // ... // more attributes we do not care about
// } // }
// //
// @param {Object} options Options for serializing CSV including // @param {object} options Options for serializing the CSV file including
// @param {String} [separator=','] separator for CSV file // delimiter and quotechar (see parseCSV options parameter above for
// @param {String} [delimiter='"'] delimiter for fields // details on these).
// //
// Heavily based on uselesscode's JS CSV serializer (MIT Licensed): // Heavily based on uselesscode's JS CSV serializer (MIT Licensed):
// http://www.uselesscode.org/javascript/csv/ // http://www.uselesscode.org/javascript/csv/
@@ -190,8 +195,8 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
}); });
} }
var options = options || {}; var options = options || {};
var separator = options.separator || ','; var delimiter = options.delimiter || ',';
var delimiter = options.delimiter || '"'; var quotechar = options.quotechar || '"';
var cur = '', // The character we are currently processing. var cur = '', // The character we are currently processing.
field = '', // Buffer for building up the current field field = '', // Buffer for building up the current field
@@ -207,7 +212,7 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
field = ''; field = '';
} else if (typeof field === "string" && rxNeedsQuoting.test(field)) { } else if (typeof field === "string" && rxNeedsQuoting.test(field)) {
// Convert string to delimited string // Convert string to delimited string
field = delimiter + field + delimiter; field = quotechar + field + quotechar;
} else if (typeof field === "number") { } else if (typeof field === "number") {
// Convert number to string // Convert number to string
field = field.toString(10); field = field.toString(10);
@@ -228,7 +233,7 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
row = ''; row = '';
} else { } else {
// Add the current field to the current row // Add the current field to the current row
row += field + separator; row += field + delimiter;
} }
// Flush the field buffer // Flush the field buffer
field = ''; field = '';

View File

@@ -34,12 +34,12 @@ test("parseCSV", function() {
deepEqual(row, {Name: 'Jones, Jay', Value: 10}); deepEqual(row, {Name: 'Jones, Jay', Value: 10});
}); });
test("parseCSVsemicolon", function() { test("parseCSV - semicolon", 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.CSV.parseCSV(csv, {separator : ';'}); var array = recline.Backend.CSV.parseCSV(csv, {delimiter : ';'});
var exp = [ var exp = [
['Jones; Jay', 10], ['Jones; Jay', 10],
['Xyz "ABC" O\'Brien', '11:35' ], ['Xyz "ABC" O\'Brien', '11:35' ],
@@ -49,12 +49,12 @@ test("parseCSVsemicolon", function() {
}); });
test("parseCSVdelimiter", function() { test("parseCSV - quotechar", 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.CSV.parseCSV(csv, {delimiter:"'"}); var array = recline.Backend.CSV.parseCSV(csv, {quotechar:"'"});
var exp = [ var exp = [
["Jones, Jay", 10], ["Jones, Jay", 10],
["Xyz \"ABC\" O'Brien", "11:35" ], ["Xyz \"ABC\" O'Brien", "11:35" ],