Add ifEmpty operators on Maybe

This commit is contained in:
Pietro Caselani 2017-11-02 12:39:35 -02:00 committed by Krunoslav Zaher
parent ec03e1b594
commit 877750b052
2 changed files with 79 additions and 0 deletions

View File

@ -241,4 +241,40 @@ public extension PrimitiveSequenceType where TraitType == MaybeTrait {
-> Maybe<R> {
return Maybe<R>(raw: primitiveSequence.source.flatMap(selector))
}
/**
Emits elements from the source observable sequence, or a default element if the source observable sequence is empty.
- seealso: [DefaultIfEmpty operator on reactivex.io](http://reactivex.io/documentation/operators/defaultifempty.html)
- parameter default: Default element to be sent if the source does not emit any elements
- returns: An observable sequence which emits default element end completes in case the original sequence is empty
*/
public func ifEmpty(default: ElementType) -> Maybe<ElementType> {
return Maybe(raw: primitiveSequence.source.ifEmpty(default: `default`))
}
/**
Returns the elements of the specified sequence or `switchTo` sequence if the sequence is empty.
- seealso: [DefaultIfEmpty operator on reactivex.io](http://reactivex.io/documentation/operators/defaultifempty.html)
- parameter switchTo: Observable sequence being returned when source sequence is empty.
- returns: Observable sequence that contains elements from switchTo sequence if source is empty, otherwise returns source sequence elements.
*/
public func ifEmpty(switchTo other: Maybe<ElementType>) -> Maybe<ElementType> {
return Maybe(raw: primitiveSequence.source.ifEmpty(switchTo: other.primitiveSequence.source))
}
/**
Returns the elements of the specified sequence or `switchTo` sequence if the sequence is empty.
- seealso: [DefaultIfEmpty operator on reactivex.io](http://reactivex.io/documentation/operators/defaultifempty.html)
- parameter switchTo: Observable sequence being returned when source sequence is empty.
- returns: Observable sequence that contains elements from switchTo sequence if source is empty, otherwise returns source sequence elements.
*/
public func ifEmpty(switchTo other: Single<ElementType>) -> Single<ElementType> {
return Single(raw: primitiveSequence.source.ifEmpty(switchTo: other.primitiveSequence.source))
}
}

View File

@ -585,6 +585,49 @@ extension MaybeTest {
.completed(200)
])
}
func test_ifEmptyDefault() {
let scheduler = TestScheduler(initialClock: 0)
let res = scheduler.start {
(Maybe<Int>.empty().ifEmpty(default: 5).asObservable())
}
XCTAssertEqual(res.events, [
next(200, 5),
completed(200)
])
}
func test_ifEmptySwitchToMaybe() {
let scheduler = TestScheduler(initialClock: 0)
let source = Maybe<Int>.empty()
let switchSource = Maybe.just(10)
let res = scheduler.start {
(source.ifEmpty(switchTo: switchSource).asObservable())
}
XCTAssertEqual(res.events, [
next(200, 10),
completed(200)
])
}
func test_ifEmptySwitchToSingle() {
let scheduler = TestScheduler(initialClock: 0)
let source = Maybe<Int>.empty()
let switchSource = Single.just(10)
let res = scheduler.start {
(source.ifEmpty(switchTo: switchSource).asObservable())
}
XCTAssertEqual(res.events, [
next(200, 10),
completed(200)
])
}
}
extension MaybeTest {