const fs = require('fs'); const d3 = require('d3'); // Read the CSV file const csvFilePath = 'gaza-death-tolls.csv'; try { // Read the CSV file content const csvContent = fs.readFileSync(csvFilePath, 'utf8'); // Parse the CSV using D3 const data = d3.csvParse(csvContent); console.log('CSV Validation Results:'); console.log('======================'); // Basic validation checks console.log(`✓ File successfully parsed`); console.log(`✓ Number of rows: ${data.length}`); if (data.length > 0) { console.log(`✓ Number of columns: ${data.columns.length}`); console.log(`✓ Column names: ${data.columns.join(', ')}`); // Check for empty rows const emptyRows = data.filter(row => Object.values(row).every(value => value === '' || value === null || value === undefined) ); if (emptyRows.length > 0) { console.log(`⚠ Warning: ${emptyRows.length} empty rows found`); } else { console.log(`✓ No empty rows found`); } // Check for missing values in each column data.columns.forEach(column => { const rowsWithMissingValues = data.filter(row => row[column] === '' || row[column] === null || row[column] === undefined ); if (rowsWithMissingValues.length > 0) { console.log(`⚠ Warning: Column "${column}" has ${rowsWithMissingValues.length} missing values`); console.log(`Rows with missing "${column}" values:`); rowsWithMissingValues.forEach((row, index) => { console.log(` Missing value row ${index + 1}:`, JSON.stringify(row, null, 2)); }); } else { console.log(`✓ Column "${column}" has no missing values`); } }); // Display first few rows as sample console.log('\nSample data (first 3 rows):'); console.log('============================'); data.slice(0, 3).forEach((row, index) => { console.log(`Row ${index + 1}:`, row); }); } else { console.log('⚠ Warning: CSV file appears to be empty or contains no data rows'); } } catch (error) { console.error('❌ Error validating CSV file:'); if (error.code === 'ENOENT') { console.error(`File not found: ${csvFilePath}`); console.error('Please ensure the file exists in the current directory.'); } else if (error.message.includes('CSV')) { console.error('CSV parsing error:', error.message); } else { console.error('Unexpected error:', error.message); } process.exit(1); }