/**
* Restores data of lists for hypothesis validation.
* Logs errors encountered during the restoration process.
*
* @param {object} dataRestoreConfig - Configuration object specifying how to restore data.
* Expected properties:
* - sourceData: The source data to restore from (array of arrays).
* - targetLists: An array of target lists to restore data into.
* - listMapping: An object mapping list names to indices in sourceData.
* @returns {boolean} - True if restoration was successful, false otherwise.
*/
function restoreLists(dataRestoreConfig) {
try {
const { sourceData, targetLists, listMapping } = dataRestoreConfig;
if (!sourceData || !Array.isArray(sourceData)) {
console.error("Error: sourceData must be a non-empty array.");
return false;
}
if (!targetLists || !Array.isArray(targetLists)) {
console.error("Error: targetLists must be a non-empty array.");
return false;
}
if (!listMapping || typeof listMapping !== 'object') {
console.error("Error: listMapping must be an object.");
return false;
}
for (const listName in targetLists) {
if (targetLists.hasOwnProperty(listName)) {
const targetList = targetLists[listName];
const sourceIndex = listMapping[listName];
if (sourceIndex === undefined) {
console.error(`Error: No mapping found for list ${listName}.`);
return false;
}
if (sourceIndex < 0 || sourceIndex >= sourceData.length) {
console.error(`Error: sourceIndex ${sourceIndex} is out of bounds for sourceData.`);
return false;
}
const sourceRow = sourceData[sourceIndex];
if (!Array.isArray(sourceRow)) {
console.error(`Error: Row at index ${sourceIndex} in sourceData is not an array.`);
return false;
}
if (!Array.isArray(targetList)) {
console.error(`Error: Target list ${listName} is not an array.`);
return false;
}
if(sourceRow.length !== targetList.length){
console.error(`Error: Source row and target list have different lengths for list ${listName}.`);
return false;
}
for (let i = 0; i < sourceRow.length; i++) {
targetList[i] = sourceRow[i];
}
}
}
return true; // Restoration successful
} catch (error) {
console.error("An unexpected error occurred during data restoration:", error);
return false;
}
}
Add your comment