Skip to content

Commit 9b72b43

Browse files
authored
Add map(to:) Operator (#126)
1 parent 36e8564 commit 9b72b43

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

CombineExt.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
/* Begin PBXBuildFile section */
2525
1970A8AA25246FBD00799AB6 /* FilterMany.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1970A8A925246FBD00799AB6 /* FilterMany.swift */; };
2626
1970A8B42524730500799AB6 /* FilterManyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1970A8B32524730400799AB6 /* FilterManyTests.swift */; };
27+
3793CBD428286BB20060441B /* MapTo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3793CBD328286BB20060441B /* MapTo.swift */; };
28+
3793CBD728286C090060441B /* MapToTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3793CBD528286BE90060441B /* MapToTests.swift */; };
2729
712E36C82711B79000A2AAFE /* RetryWhen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 712E36C72711B79000A2AAFE /* RetryWhen.swift */; };
2830
7182326F26DAAF230026BAD3 /* RetryWhenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7182326E26DAAF230026BAD3 /* RetryWhenTests.swift */; };
2931
BF330EF624F1FFFE001281FC /* CombineSchedulers in Frameworks */ = {isa = PBXBuildFile; productRef = BF330EF524F1FFFE001281FC /* CombineSchedulers */; };
@@ -110,6 +112,8 @@
110112
/* Begin PBXFileReference section */
111113
1970A8A925246FBD00799AB6 /* FilterMany.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterMany.swift; sourceTree = "<group>"; };
112114
1970A8B32524730400799AB6 /* FilterManyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterManyTests.swift; sourceTree = "<group>"; };
115+
3793CBD328286BB20060441B /* MapTo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapTo.swift; sourceTree = "<group>"; };
116+
3793CBD528286BE90060441B /* MapToTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapToTests.swift; sourceTree = "<group>"; };
113117
712E36C72711B79000A2AAFE /* RetryWhen.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RetryWhen.swift; sourceTree = "<group>"; };
114118
7182326E26DAAF230026BAD3 /* RetryWhenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RetryWhenTests.swift; sourceTree = "<group>"; };
115119
BF330EF824F20032001281FC /* Timer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timer.swift; sourceTree = "<group>"; };
@@ -251,6 +255,7 @@
251255
OBJ_23 /* FlatMapLatest.swift */,
252256
OBJ_24 /* MapMany.swift */,
253257
D836234724EA9446002353AC /* MergeMany.swift */,
258+
3793CBD328286BB20060441B /* MapTo.swift */,
254259
OBJ_25 /* Materialize.swift */,
255260
C387777B24E6BBE900FAD2D8 /* Nwise.swift */,
256261
OBJ_26 /* Partition.swift */,
@@ -300,6 +305,7 @@
300305
OBJ_47 /* DematerializeTests.swift */,
301306
OBJ_48 /* FlatMapLatestTests.swift */,
302307
OBJ_49 /* MapManyTests.swift */,
308+
3793CBD528286BE90060441B /* MapToTests.swift */,
303309
1970A8B32524730400799AB6 /* FilterManyTests.swift */,
304310
OBJ_50 /* MaterializeTests.swift */,
305311
D836234924EA9888002353AC /* MergeManyTests.swift */,
@@ -573,6 +579,7 @@
573579
OBJ_135 /* PrefixDurationTests.swift in Sources */,
574580
OBJ_136 /* RemoveAllDuplicatesTests.swift in Sources */,
575581
OBJ_137 /* ReplaySubjectTests.swift in Sources */,
582+
3793CBD728286C090060441B /* MapToTests.swift in Sources */,
576583
OBJ_138 /* SetOutputTypeTests.swift in Sources */,
577584
OBJ_139 /* ShareReplayTests.swift in Sources */,
578585
BFADDC8B25BCE91E00465E9B /* FlatMapBatchesTests.swift in Sources */,
@@ -590,6 +597,7 @@
590597
OBJ_79 /* DemandBuffer.swift in Sources */,
591598
OBJ_80 /* Sink.swift in Sources */,
592599
OBJ_81 /* Optional.swift in Sources */,
600+
3793CBD428286BB20060441B /* MapTo.swift in Sources */,
593601
C387777C24E6BBE900FAD2D8 /* Nwise.swift in Sources */,
594602
OBJ_82 /* Event.swift in Sources */,
595603
OBJ_83 /* ObjectOwnership.swift in Sources */,

Sources/Operators/MapTo.swift

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// MapTo.swift
3+
// CombineExt
4+
//
5+
// Created by Dan Halliday on 08/05/2022.
6+
// Copyright © 2022 Combine Community. All rights reserved.
7+
//
8+
9+
#if canImport(Combine)
10+
import Combine
11+
12+
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
13+
public extension Publisher {
14+
/// Replace each upstream value with a constant.
15+
///
16+
/// - Parameter value: The constant with which to replace each upstream value.
17+
/// - Returns: A new publisher wrapping the upstream, but with output type `Result`.
18+
func map<Result>(to value: Result) -> Publishers.Map<Self, Result> {
19+
map { _ in value }
20+
}
21+
}
22+
#endif

Tests/MapToTests.swift

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// MapToTests.swift
3+
// CombineExt
4+
//
5+
// Created by Dan Halliday on 08/05/2022.
6+
// Copyright © 2022 Combine Community. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
#if !os(watchOS)
12+
import XCTest
13+
import Combine
14+
import CombineExt
15+
16+
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
17+
final class MapToTests: XCTestCase {
18+
private var subscription: AnyCancellable!
19+
20+
func testMapToConstantValue() {
21+
let subject = PassthroughSubject<Int, Never>()
22+
var result: Int? = nil
23+
24+
subscription = subject
25+
.map(to: 2)
26+
.sink(receiveValue: { result = $0 })
27+
28+
subject.send(1)
29+
XCTAssertEqual(result, 2)
30+
}
31+
}
32+
#endif

0 commit comments

Comments
 (0)