1. /**
  2. * Restores data of lists for hypothesis validation.
  3. * Logs errors encountered during the restoration process.
  4. *
  5. * @param {object} dataRestoreConfig - Configuration object specifying how to restore data.
  6. * Expected properties:
  7. * - sourceData: The source data to restore from (array of arrays).
  8. * - targetLists: An array of target lists to restore data into.
  9. * - listMapping: An object mapping list names to indices in sourceData.
  10. * @returns {boolean} - True if restoration was successful, false otherwise.
  11. */
  12. function restoreLists(dataRestoreConfig) {
  13. try {
  14. const { sourceData, targetLists, listMapping } = dataRestoreConfig;
  15. if (!sourceData || !Array.isArray(sourceData)) {
  16. console.error("Error: sourceData must be a non-empty array.");
  17. return false;
  18. }
  19. if (!targetLists || !Array.isArray(targetLists)) {
  20. console.error("Error: targetLists must be a non-empty array.");
  21. return false;
  22. }
  23. if (!listMapping || typeof listMapping !== 'object') {
  24. console.error("Error: listMapping must be an object.");
  25. return false;
  26. }
  27. for (const listName in targetLists) {
  28. if (targetLists.hasOwnProperty(listName)) {
  29. const targetList = targetLists[listName];
  30. const sourceIndex = listMapping[listName];
  31. if (sourceIndex === undefined) {
  32. console.error(`Error: No mapping found for list ${listName}.`);
  33. return false;
  34. }
  35. if (sourceIndex < 0 || sourceIndex >= sourceData.length) {
  36. console.error(`Error: sourceIndex ${sourceIndex} is out of bounds for sourceData.`);
  37. return false;
  38. }
  39. const sourceRow = sourceData[sourceIndex];
  40. if (!Array.isArray(sourceRow)) {
  41. console.error(`Error: Row at index ${sourceIndex} in sourceData is not an array.`);
  42. return false;
  43. }
  44. if (!Array.isArray(targetList)) {
  45. console.error(`Error: Target list ${listName} is not an array.`);
  46. return false;
  47. }
  48. if(sourceRow.length !== targetList.length){
  49. console.error(`Error: Source row and target list have different lengths for list ${listName}.`);
  50. return false;
  51. }
  52. for (let i = 0; i < sourceRow.length; i++) {
  53. targetList[i] = sourceRow[i];
  54. }
  55. }
  56. }
  57. return true; // Restoration successful
  58. } catch (error) {
  59. console.error("An unexpected error occurred during data restoration:", error);
  60. return false;
  61. }
  62. }

Add your comment