/**
* Diffs two datasets of directories and returns a list of differences.
*
* @param {string[]} dataset1 - The first dataset of directories.
* @param {string[]} dataset2 - The second dataset of directories.
* @returns {object} An object containing the differences: added, removed, and common directories.
* Returns null if input validation fails.
*/
function diffDirectories(dataset1, dataset2) {
// Input validation
if (!Array.isArray(dataset1) || !Array.isArray(dataset2)) {
console.error("Error: Inputs must be arrays.");
return null;
}
if (dataset1.length === 0 && dataset2.length === 0) {
return { added: [], removed: [], common: [] }; // Both empty, nothing to diff
}
if (dataset1.length === 0) {
return { added: dataset2, removed: [], common: [] }; // dataset1 is empty, all in dataset2 are added
}
if (dataset2.length === 0) {
return { added: [], removed: dataset1, common: [] }; // dataset2 is empty, all in dataset1 are removed
}
const added = [];
const removed = [];
const common = [];
// Helper function to check if a directory exists
function directoryExists(dir) {
try {
return fs.existsSync(dir); // Use fs.existsSync for basic existence check
} catch (e) {
return false; //Handle potential errors during directory existence check
}
}
for (const dir1 of dataset1) {
if (!directoryExists(dir1)) {
removed.push(dir1);
} else {
const index = dataset2.indexOf(dir1);
if (index === -1) {
removed.push(dir1);
} else {
common.push(dir1);
}
}
}
for (const dir2 of dataset2) {
if (!directoryExists(dir2) && !removed.includes(dir2)) {
added.push(dir2);
}
}
return { added, removed, common };
}
// Dummy fs module for testing. Replace with actual fs module in a real application.
const fs = require('fs'); //Import the fs module
Add your comment