Skip to content

Instantly share code, notes, and snippets.

@jaclync
Last active May 24, 2024 06:04
Show Gist options
  • Save jaclync/3b77c8d0fdfa6481721558320b72fe38 to your computer and use it in GitHub Desktop.
Save jaclync/3b77c8d0fdfa6481721558320b72fe38 to your computer and use it in GitHub Desktop.
Test mapping to nullable or filtering + mapping
import Combine
struct PointOfSaleEntryPoint {}
let isBetaFeatureEnabled = PassthroughSubject<Bool, Never>()
let isEligibleForPOS = PassthroughSubject<Bool, Never>()
let posViewModel =
Publishers.CombineLatest(isBetaFeatureEnabled, isEligibleForPOS)
// // Map to optional value
// // Output:
// // posViewModel: nil
// // posViewModel: Optional(__lldb_expr_38.PointOfSaleEntryPoint())
// // posViewModel: nil
// .map { isBetaFeatureEnabled, isEligibleForPOS -> PointOfSaleEntryPoint? in
// if isBetaFeatureEnabled && isEligibleForPOS {
// return PointOfSaleEntryPoint()
// } else {
// return nil
// }
// }
// Filter + map
// Output:
// posViewModel: PointOfSaleEntryPoint()
.filter { isBetaFeatureEnabled, isEligibleForPOS in
isBetaFeatureEnabled && isEligibleForPOS
}
.map { _ in
PointOfSaleEntryPoint()
}
posViewModel
.sink { viewModel in
print("posViewModel: \(viewModel)")
}
// Expected: nil view model
isBetaFeatureEnabled.send(true)
isEligibleForPOS.send(false)
// Expected: non-nil view model
isEligibleForPOS.send(true)
// Expected: nil view model
isBetaFeatureEnabled.send(false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment