Skip to content

Instantly share code, notes, and snippets.

@woodycatliu
Created April 27, 2023 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodycatliu/02380a5709576d6e21527371912bf0c3 to your computer and use it in GitHub Desktop.
Save woodycatliu/02380a5709576d6e21527371912bf0c3 to your computer and use it in GitHub Desktop.
Combine.Publisher.Filter extension
extension Publisher {
public func filterTo(_ isIncluded: @escaping (Self.Output) -> Bool) -> AnyPublisher<Output, Failure> {
self.filter(isIncluded).eraseToAnyPublisher()
}
public func filterTo(equalTo value: Output) -> AnyPublisher<Output, Failure> where Output: AnyObject&Equatable {
return self.filterTo { [weak value] output in
value == output
}
}
public func filterTo<Value: AnyObject&Equatable>(equalTo value: Value, _ keyPath: KeyPath<Output, Value>) -> AnyPublisher<Output, Failure> {
let filtor: (Output) -> Bool = { [weak value] output in
return output[keyPath: keyPath] == value
}
return self.filter(filtor).eraseToAnyPublisher()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment