1. /**
  2. * Deduplicates time records with basic sanity checks.
  3. *
  4. * @param {Array<Object>} records An array of objects, each with a 'timestamp' property.
  5. * @returns {Array<Object>} A new array with deduplicated records.
  6. */
  7. function deduplicateTimeRecords(records) {
  8. if (!Array.isArray(records)) {
  9. return []; // Return empty array if input is not an array
  10. }
  11. const uniqueTimestamps = new Set(); // Use a Set to track unique timestamps
  12. const deduplicatedRecords = [];
  13. for (const record of records) {
  14. if (typeof record !== 'object' || record === null || !record.hasOwnProperty('timestamp')) {
  15. continue; // Skip invalid records
  16. }
  17. const timestamp = record.timestamp;
  18. // Sanity check: Ensure timestamp is a number
  19. if (typeof timestamp !== 'number') {
  20. continue; // Skip if timestamp is not a number
  21. }
  22. // Sanity check: Ensure timestamp is non-negative
  23. if (timestamp < 0) {
  24. continue; // Skip if timestamp is negative
  25. }
  26. if (!uniqueTimestamps.has(timestamp)) {
  27. uniqueTimestamps.add(timestamp);
  28. deduplicatedRecords.push(record);
  29. }
  30. }
  31. return deduplicatedRecords;
  32. }

Add your comment