[#185,backend/csv][s]: add support for dataset-style objects to serializeCSV method - fixes #185.
This commit is contained in:
parent
c879ca6df9
commit
6e49f53a63
@ -59,8 +59,9 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
|
||||
//
|
||||
// @param {String} s The string to convert
|
||||
// @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
|
||||
// @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):
|
||||
// http://www.uselesscode.org/javascript/csv/
|
||||
my.parseCSV= function(s, options) {
|
||||
@ -149,20 +150,45 @@ this.recline.Backend.CSV = this.recline.Backend.CSV || {};
|
||||
return out;
|
||||
};
|
||||
|
||||
// Converts an array of arrays into a Comma Separated Values string.
|
||||
// Each array becomes a line in the CSV.
|
||||
// ### serializeCSV
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// @return The array serialized as a CSV
|
||||
// @type String
|
||||
//
|
||||
// @param {Array} a The array of arrays to convert
|
||||
// @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):
|
||||
// @param {Object or Array} dataToSerialize The Object or array of arrays to convert. Object structure must be as follows:
|
||||
//
|
||||
// {
|
||||
// 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/
|
||||
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 separator = options.separator || ',';
|
||||
var delimiter = options.delimiter || '"';
|
||||
|
||||
@ -64,7 +64,7 @@ test("parseCSVdelimiter", function() {
|
||||
|
||||
});
|
||||
|
||||
test("serializeCSV", function() {
|
||||
test("serializeCSV - Array", function() {
|
||||
var csv = [
|
||||
['Jones, Jay', 10],
|
||||
['Xyz "ABC" O\'Brien', '11:35' ],
|
||||
@ -78,5 +78,22 @@ test("serializeCSV", function() {
|
||||
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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user