Merge pull request #186 from mikemorris/master

[#185,be/csv][xs]: fixing serializeCSV to pass test.
This commit is contained in:
Rufus Pollock 2012-07-15 06:36:06 -07:00
commit 29cf575491
2 changed files with 2 additions and 21 deletions

View File

@ -179,7 +179,7 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
if (field === null) {
// If field is null set to empty string
field = '';
} else if (typeof field === "string") {
} else if (typeof field === "string" && rxNeedsQuoting.test(field)) {
// Convert string to delimited string
field = delimiter + field + delimiter;
} else if (typeof field === "number") {
@ -212,25 +212,6 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
return out;
};
var rxHasComma = /^\d+$/,
rxIsFloat = /^\d*\.\d+$|^\d+\.\d*$/,
// If a string has leading or trailing space,
// contains a comma double quote or a newline
// it needs to be quoted in CSV output
rxNeedsQuoting = /^\s|\s$|,|"|\n/,
trim = (function () {
// Fx 3.1 has a native trim function, it's about 10x faster, use it if it exists
if (String.prototype.trim) {
return function (s) {
return s.trim();
};
} else {
return function (s) {
return s.replace(/^\s*/, '').replace(/\s*$/, '');
};
}
}());
var rxIsInt = /^\d+$/,
rxIsFloat = /^\d*\.\d+$|^\d+\.\d*$/,
// If a string has leading or trailing space,

View File

@ -73,7 +73,7 @@ test("serializeCSV", function() {
var array = recline.Backend.CSV.serializeCSV(csv);
var exp = '"Jones, Jay",10\n' +
'"Xyz ""ABC"" O\'Brien",11:35\n' +
'"Xyz \"ABC\" O\'Brien",11:35\n' +
'"Other, AN",12:35\n';
deepEqual(array, exp);
});