Skip to content

Commit 8157295

Browse files
HugoSayfreak4pc
authored andcommitted
added unwrap operator to SharedSequence (#184)
1 parent ed163b4 commit 8157295

File tree

13 files changed

+131
-5
lines changed

13 files changed

+131
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ master
77
- added `mapMany` operator
88
- added `toSortedArray` operator
99
- rolled `map(to:)` back to `mapTo(_:)` for `SharedSequenceConvertibleType`
10+
- added `unwrap()` operator for SharedSequence
1011

1112
3.3.0
1213
-----

Playground/RxSwiftExtPlayground.playground/Pages/unwrap.xcplaygroundpage/Contents.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,25 @@ example("unwrap and filter out nil values") {
5050
})
5151
}
5252

53+
example("unwrap and filter out nil values for a Driver") {
54+
55+
let numbers = [1, 2, nil, Int?(4)]
56+
let numbersSubject = BehaviorSubject<Int?>(value: nil)
57+
58+
numbersSubject
59+
.asDriver(onErrorJustReturn: nil)
60+
.unwrap()
61+
.asObservable()
62+
.toArray()
63+
.subscribe(onNext: { result in
64+
// look types on the right panel ===>
65+
numbers
66+
result
67+
print("unwrap() transformed \(numbers) to \(result)")
68+
})
69+
70+
Observable.from(numbers)
71+
.bind(to: numbersSubject)
72+
}
73+
5374
//: [Next](@next)

RxSwiftExt.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
/* Begin PBXBuildFile section */
2525
188C6DA31C47B4240092101A /* RxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 188C6DA21C47B4240092101A /* RxSwift.framework */; };
26+
1958B5F1216768D900CAF1D3 /* unwrap+SharedSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1958B5F0216768D900CAF1D3 /* unwrap+SharedSequence.swift */; };
27+
1958B5F621676ECB00CAF1D3 /* unrwapTests+SharedSequence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1958B5F521676ECB00CAF1D3 /* unrwapTests+SharedSequence.swift */; };
2628
1A8741AC20745A91004BB762 /* UIViewPropertyAnimatorTests+Rx.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A8741AB20745A91004BB762 /* UIViewPropertyAnimatorTests+Rx.swift */; };
2729
1AA8395B207451D6001C49ED /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AA8395A207451D5001C49ED /* RxCocoa.framework */; };
2830
3D11958B1FCAD9AE0095134B /* and.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DBDE5FB1FBBAE3900DF47F9 /* and.swift */; };
@@ -262,6 +264,8 @@
262264
/* Begin PBXFileReference section */
263265
188C6D911C47B2B20092101A /* RxSwiftExt.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RxSwiftExt.framework; sourceTree = BUILT_PRODUCTS_DIR; };
264266
188C6DA21C47B4240092101A /* RxSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxSwift.framework; path = Carthage/Build/iOS/RxSwift.framework; sourceTree = SOURCE_ROOT; };
267+
1958B5F0216768D900CAF1D3 /* unwrap+SharedSequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "unwrap+SharedSequence.swift"; sourceTree = "<group>"; };
268+
1958B5F521676ECB00CAF1D3 /* unrwapTests+SharedSequence.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "unrwapTests+SharedSequence.swift"; sourceTree = "<group>"; };
265269
1A8741AB20745A91004BB762 /* UIViewPropertyAnimatorTests+Rx.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIViewPropertyAnimatorTests+Rx.swift"; sourceTree = "<group>"; };
266270
1AA8395A207451D5001C49ED /* RxCocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxCocoa.framework; path = Carthage/Build/iOS/RxCocoa.framework; sourceTree = "<group>"; };
267271
3D638DE71DC2B2D40089A590 /* RxSwiftExtTests-iOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "RxSwiftExtTests-iOS.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -453,6 +457,7 @@
453457
5386076E1E6F1C0A000361DE /* mapTo+RxCocoa.swift */,
454458
538607EF1E6F589E000361DE /* not+RxCocoa.swift */,
455459
4A73956B206D501300E2BE2D /* UIViewPropertyAnimator+Rx.swift */,
460+
1958B5F0216768D900CAF1D3 /* unwrap+SharedSequence.swift */,
456461
);
457462
path = RxCocoa;
458463
sourceTree = "<group>";
@@ -464,6 +469,7 @@
464469
538607711E6F1CFB000361DE /* MapToTests+RxCocoa.swift */,
465470
53C79D5F1E6F5AAB00CD9B6A /* NotTests+RxCocoa.swift */,
466471
1A8741AB20745A91004BB762 /* UIViewPropertyAnimatorTests+Rx.swift */,
472+
1958B5F521676ECB00CAF1D3 /* unrwapTests+SharedSequence.swift */,
467473
);
468474
name = RxCocoa;
469475
path = Tests/RxCocoa;
@@ -981,6 +987,7 @@
981987
538607AE1E6F334B000361DE /* ignore.swift in Sources */,
982988
BF79DA0A206C145D008AA708 /* withUnretained.swift in Sources */,
983989
BF515CE01F3F370600492640 /* curry.swift in Sources */,
990+
1958B5F1216768D900CAF1D3 /* unwrap+SharedSequence.swift in Sources */,
984991
5A1DDEBF1ED58F8600F2E4B1 /* pausableBuffered.swift in Sources */,
985992
BF515CE21F3F371600492640 /* fromAsync.swift in Sources */,
986993
DC612872209E80810053CBB7 /* mapMany.swift in Sources */,
@@ -1013,6 +1020,7 @@
10131020
538607E51E6F36A9000361DE /* IgnoreWhenTests.swift in Sources */,
10141021
538607E11E6F36A9000361DE /* CatchErrorJustCompleteTests.swift in Sources */,
10151022
538607ED1E6F36A9000361DE /* WeakTarget.swift in Sources */,
1023+
1958B5F621676ECB00CAF1D3 /* unrwapTests+SharedSequence.swift in Sources */,
10161024
C4D2154220118FB9009804AE /* Observable+OfTypeTests.swift in Sources */,
10171025
780CB21D20A0EE8300FD3F39 /* MapManyTests.swift in Sources */,
10181026
538607E41E6F36A9000361DE /* IgnoreTests.swift in Sources */,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

Source/RxCocoa/mapTo+RxCocoa.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension SharedSequenceConvertibleType {
1818
public func mapTo<R>(_ value: R) -> SharedSequence<SharingStrategy, R> {
1919
return map { _ in value }
2020
}
21-
21+
2222
@available(*, deprecated, renamed: "mapTo(_:)")
2323
public func map<R>(to value: R) -> SharedSequence<SharingStrategy, R> {
2424
return map { _ in value }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// unwrap+SharedSequence.swift
3+
// RxSwiftExt
4+
//
5+
// Created by Hugo Saynac on 05/10/2018.
6+
// Copyright © 2018 RxSwift Community. All rights reserved.
7+
//
8+
9+
import RxCocoa
10+
11+
extension SharedSequence {
12+
13+
/**
14+
Takes a SharedSequence of optional elements and returns a SharedSequence of non-optional elements, filtering out any nil values.
15+
16+
- returns: A SharedSequence of non-optional elements
17+
*/
18+
19+
public func unwrap<T>() -> SharedSequence<S, T> where E == T? {
20+
return self.filter { $0 != nil }.map { $0! }
21+
}
22+
}

Source/RxSwift/retryWithBehavior.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Specifies how observable sequence will be repeated in case of an error
1717
Delay will be incremented by multiplier after each iteration (multiplier = 0.5 means 50% increment)
1818
- CustomTimerDelayed: Will be repeated specified number of times. Delay will be calculated by custom closure
1919
*/
20+
2021
public enum RepeatBehavior {
2122
case immediate (maxCount: UInt)
2223
case delayed (maxCount: UInt, time: Double)

Tests/RxCocoa/MapToTests+RxCocoa.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class MapToCocoaTests: XCTestCase {
3939
}
4040

4141
func testReplaceWithResultValues() {
42-
//test elements values and type
42+
// test elements values and type
4343
let correctValues = [
4444
next(0, "candy"),
4545
next(0, "candy"),
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// UnwrapTests.swift
3+
// RxSwiftExt
4+
//
5+
// Created by Hugo Saynac on 05/10/2018.
6+
// Copyright © 2018 RxSwift Community. All rights reserved.
7+
//
8+
9+
import XCTest
10+
11+
import RxSwift
12+
import RxSwiftExt
13+
import RxTest
14+
15+
class UnwrapSharedStrategyTests: XCTestCase {
16+
let numbers: [Int?] = [1, nil, Int?(3), 4]
17+
private var observer: TestableObserver<Int>!
18+
private let numbersSubject = BehaviorSubject<Int?>(value: nil)
19+
20+
override func setUp() {
21+
super.setUp()
22+
23+
let scheduler = TestScheduler(initialClock: 0)
24+
observer = scheduler.createObserver(Int.self)
25+
26+
_ = numbersSubject
27+
.asDriver(onErrorJustReturn: nil)
28+
.unwrap()
29+
.asObservable()
30+
.subscribe(observer)
31+
32+
_ = Observable.from(numbers)
33+
.bind(to: numbersSubject)
34+
35+
scheduler.start()
36+
}
37+
38+
func testUnwrapFilterNil() {
39+
//test results count
40+
print(observer.events)
41+
XCTAssertEqual(
42+
observer.events.count,
43+
numbers.count
44+
)
45+
}
46+
47+
func testUnwrapResultValues() {
48+
// test elements values and type
49+
let correctValues = Recorded.events(
50+
.next(0, 1),
51+
.next(0, 3),
52+
.next(0, 4),
53+
.completed(0)
54+
)
55+
XCTAssertEqual(observer.events, correctValues)
56+
}
57+
}

0 commit comments

Comments
 (0)