/**
* Deduplicates time records with basic sanity checks.
*
* @param {Array<Object>} records An array of objects, each with a 'timestamp' property.
* @returns {Array<Object>} A new array with deduplicated records.
*/
function deduplicateTimeRecords(records) {
if (!Array.isArray(records)) {
return []; // Return empty array if input is not an array
}
const uniqueTimestamps = new Set(); // Use a Set to track unique timestamps
const deduplicatedRecords = [];
for (const record of records) {
if (typeof record !== 'object' || record === null || !record.hasOwnProperty('timestamp')) {
continue; // Skip invalid records
}
const timestamp = record.timestamp;
// Sanity check: Ensure timestamp is a number
if (typeof timestamp !== 'number') {
continue; // Skip if timestamp is not a number
}
// Sanity check: Ensure timestamp is non-negative
if (timestamp < 0) {
continue; // Skip if timestamp is negative
}
if (!uniqueTimestamps.has(timestamp)) {
uniqueTimestamps.add(timestamp);
deduplicatedRecords.push(record);
}
}
return deduplicatedRecords;
}
Add your comment