Skip to content

Commit

Permalink
Array+Identifiable: Add first(by:) and last(by:) to help find ide…
Browse files Browse the repository at this point in the history
…ntifiable elements (#17)
  • Loading branch information
zweigraf authored Sep 9, 2020
1 parent 0bc9405 commit 94a7ea1
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Sources/FoundationExtensions/Collection/Array+Identifiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,20 @@ extension Array where Element: Identifiable {
self[index] = element
return true
}

/// Finds the first element with the given ID in receiving array.
/// - Parameter id: ID of the element to find.
/// - Returns: Returns the first element with the given ID, or nil if an element with the given ID could
/// not be found.
public func first(by id: Element.ID) -> Element? {
first(where: { $0.id == id })
}

/// Finds the last element with the given ID in receiving array.
/// - Parameter id: ID of the element to find.
/// - Returns: Returns the last element with the given ID, or nil if an element with the given ID could
/// not be found.
public func last(by id: Element.ID) -> Element? {
last(where: { $0.id == id })
}
}

0 comments on commit 94a7ea1

Please sign in to comment.