PumpHistoryEvent+Duplicates.swift 614 B

12345678910111213141516171819
  1. import Foundation
  2. extension Array where Element == PumpHistoryEvent {
  3. /// Removes duplicate PumpSuspend events from the array
  4. /// - Returns: A new array with duplicate suspend events removed
  5. func removingDuplicateSuspendResumeEvents() -> [PumpHistoryEvent] {
  6. var seenSuspendResume = Set<Date>()
  7. return filter { event in
  8. if event.type != .pumpSuspend, event.type != .pumpResume {
  9. return true
  10. }
  11. // Make suspend/resume events unique by timestamp
  12. return seenSuspendResume.insert(event.timestamp).inserted
  13. }
  14. }
  15. }