diff --git a/src/backend.csv.js b/src/backend.csv.js index 5776eb71..7de225fe 100644 --- a/src/backend.csv.js +++ b/src/backend.csv.js @@ -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 || '"'; diff --git a/test/backend.csv.test.js b/test/backend.csv.test.js index 839c3cfe..a2b3ca96 100644 --- a/test/backend.csv.test.js +++ b/test/backend.csv.test.js @@ -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);