all files / keystone/lib/list/ getCSV.js

13.04% Statements 3/23
0% Branches 0/18
0% Functions 0/2
13.04% Lines 3/23
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                                                                  
var listToArray = require('list-to-array');
 
/**
 * Gets the data from an Item ready to be serialised to CSV for download
 */
 
function getData (item, fields, expandRelationshipFields) {
	var data = {
		id: item.id,
	};
	if (this.autokey) {
		data[this.autokey.path] = item.get(this.autokey.path);
	}
	if (fields === undefined) {
		fields = Object.keys(this.fields);
	}
	if (fields) {
		if (typeof fields === 'string') {
			fields = listToArray(fields);
		}
		if (!Array.isArray(fields)) {
			throw new Error('List.getData: fields must be undefined, a string, or an array.');
		}
		fields.forEach(function (path) {
			var field = this.fields[path];
			if (field) {
				if (field.type === 'relationship' && expandRelationshipFields) {
					data[path] = field.getExpandedData(item);
				} else {
					data[path] = field.format(item);
				}
			} else {
				data[path] = item.get(path);
			}
			if (typeof data[path] === 'object') {
				data[path] = JSON.stringify(data[path]);
			}
		}, this);
	}
	return data;
}
 
module.exports = getData;