[#185,backend/csv][s]: add support for dataset-style objects to serializeCSV method - fixes #185.
This commit is contained in:
@@ -61,6 +61,7 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
|
|||||||
// @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 whitespace is stripped off of each non-quoted field as it is imported
|
||||||
// @param {String} [separator=','] Separator for CSV file
|
// @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):
|
||||||
// http://www.uselesscode.org/javascript/csv/
|
// http://www.uselesscode.org/javascript/csv/
|
||||||
my.parseCSV= function(s, options) {
|
my.parseCSV= function(s, options) {
|
||||||
@@ -149,20 +150,45 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
|
|||||||
return out;
|
return out;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Converts an array of arrays into a Comma Separated Values string.
|
// ### serializeCSV
|
||||||
// Each array becomes a line in the CSV.
|
//
|
||||||
|
// Convert an Object or a simple array of arrays into a Comma
|
||||||
|
// Separated Values string.
|
||||||
//
|
//
|
||||||
// Nulls are converted to empty fields and integers or floats are converted to non-quoted numbers.
|
// Nulls are converted to empty fields and integers or floats are converted to non-quoted numbers.
|
||||||
//
|
//
|
||||||
// @return The array serialized as a CSV
|
// @return The array serialized as a CSV
|
||||||
// @type String
|
// @type String
|
||||||
//
|
//
|
||||||
// @param {Array} a The array of arrays to convert
|
// @param {Object or Array} dataToSerialize The Object or array of arrays to convert. Object structure must be as follows:
|
||||||
// @param {Object} options Options for loading CSV including
|
//
|
||||||
// @param {String} [separator=','] Separator for CSV file
|
// {
|
||||||
// Heavily based on uselesscode's JS CSV parser (MIT Licensed):
|
// fields: [ {id: .., ...}, {id: ...,
|
||||||
|
// records: [ { record }, { record }, ... ]
|
||||||
|
// ... // more attributes we do not care about
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @param {Object} options Options for serializing CSV including
|
||||||
|
// @param {String} [separator=','] separator for CSV file
|
||||||
|
// @param {String} [delimiter='"'] delimiter for fields
|
||||||
|
//
|
||||||
|
// Heavily based on uselesscode's JS CSV serializer (MIT Licensed):
|
||||||
// http://www.uselesscode.org/javascript/csv/
|
// http://www.uselesscode.org/javascript/csv/
|
||||||
my.serializeCSV= function(a, options) {
|
my.serializeCSV= function(dataToSerialize, options) {
|
||||||
|
var a = null;
|
||||||
|
if (dataToSerialize instanceof Array) {
|
||||||
|
a = dataToSerialize;
|
||||||
|
} else {
|
||||||
|
a = [];
|
||||||
|
var fieldNames = _.pluck(dataToSerialize.fields, 'id');
|
||||||
|
a.push(fieldNames);
|
||||||
|
_.each(dataToSerialize.records, function(record, index) {
|
||||||
|
var tmp = _.map(fieldNames, function(fn) {
|
||||||
|
return record[fn];
|
||||||
|
});
|
||||||
|
a.push(tmp);
|
||||||
|
});
|
||||||
|
}
|
||||||
var options = options || {};
|
var options = options || {};
|
||||||
var separator = options.separator || ',';
|
var separator = options.separator || ',';
|
||||||
var delimiter = options.delimiter || '"';
|
var delimiter = options.delimiter || '"';
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ test("parseCSVdelimiter", function() {
|
|||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("serializeCSV", function() {
|
test("serializeCSV - Array", function() {
|
||||||
var csv = [
|
var csv = [
|
||||||
['Jones, Jay', 10],
|
['Jones, Jay', 10],
|
||||||
['Xyz "ABC" O\'Brien', '11:35' ],
|
['Xyz "ABC" O\'Brien', '11:35' ],
|
||||||
@@ -78,5 +78,22 @@ test("serializeCSV", function() {
|
|||||||
deepEqual(array, exp);
|
deepEqual(array, exp);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("serializeCSV - Object", function() {
|
||||||
|
var indata = {
|
||||||
|
fields: [ {id: 'name'}, {id: 'number'}],
|
||||||
|
records: [
|
||||||
|
{name: 'Jones, Jay', number: 10},
|
||||||
|
{name: 'Xyz "ABC" O\'Brien', number: '11:35' },
|
||||||
|
{name: 'Other, AN', number: '12:35' }
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
var array = recline.Backend.CSV.serializeCSV(indata);
|
||||||
|
var exp = 'name,number\n' +
|
||||||
|
'"Jones, Jay",10\n' +
|
||||||
|
'"Xyz \"ABC\" O\'Brien",11:35\n' +
|
||||||
|
'"Other, AN",12:35\n';
|
||||||
|
deepEqual(array, exp);
|
||||||
|
});
|
||||||
|
|
||||||
})(this.jQuery);
|
})(this.jQuery);
|
||||||
|
|||||||
Reference in New Issue
Block a user