/**
* Exports dataset results for dry-run scenarios, supporting older versions.
*
* @param {object} results - The results object containing the dataset data.
* @param {string} format - The desired output format ('json', 'csv', 'txt').
* @param {string} filename - The name of the output file.
* @param {object} options - Optional configuration options.
* @param {string} options.version - The version of the API to support (e.g., 'v1', 'v2').
*/
function exportDryRunData(results, format, filename, options = {}) {
const version = options.version || 'v1'; // Default to v1 if version not specified
if (!results || typeof results !== 'object') {
console.error("Invalid results object provided.");
return;
}
if (!format || !['json', 'csv', 'txt'].includes(format)) {
console.error("Invalid format specified. Supported formats: json, csv, txt");
return;
}
try {
let data;
switch (format) {
case 'json':
data = JSON.stringify(results, null, 2); // Pretty print JSON
break;
case 'csv':
data = results.map(row => Object.values(row).join(',')).join('\n'); // Convert object to CSV
break;
case 'txt':
data = JSON.stringify(results, null, 2); // Serialize to string
break;
default:
console.error("Unsupported format.");
return;
}
const fs = require('fs'); // Node.js file system module
fs.writeFileSync(filename, data); // Write data to file
console.log(`Dry-run data exported to ${filename} in ${format} format (Version ${version}).`);
} catch (error) {
console.error("Error exporting data:", error);
}
}
Add your comment