Skip to content

Commit

Permalink
Add map(to:) Operator (CombineCommunity#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
danhalliday authored May 12, 2022
1 parent 36e8564 commit 9b72b43
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CombineExt.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
/* Begin PBXBuildFile section */
1970A8AA25246FBD00799AB6 /* FilterMany.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1970A8A925246FBD00799AB6 /* FilterMany.swift */; };
1970A8B42524730500799AB6 /* FilterManyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1970A8B32524730400799AB6 /* FilterManyTests.swift */; };
3793CBD428286BB20060441B /* MapTo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3793CBD328286BB20060441B /* MapTo.swift */; };
3793CBD728286C090060441B /* MapToTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3793CBD528286BE90060441B /* MapToTests.swift */; };
712E36C82711B79000A2AAFE /* RetryWhen.swift in Sources */ = {isa = PBXBuildFile; fileRef = 712E36C72711B79000A2AAFE /* RetryWhen.swift */; };
7182326F26DAAF230026BAD3 /* RetryWhenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7182326E26DAAF230026BAD3 /* RetryWhenTests.swift */; };
BF330EF624F1FFFE001281FC /* CombineSchedulers in Frameworks */ = {isa = PBXBuildFile; productRef = BF330EF524F1FFFE001281FC /* CombineSchedulers */; };
Expand Down Expand Up @@ -110,6 +112,8 @@
/* Begin PBXFileReference section */
1970A8A925246FBD00799AB6 /* FilterMany.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterMany.swift; sourceTree = "<group>"; };
1970A8B32524730400799AB6 /* FilterManyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterManyTests.swift; sourceTree = "<group>"; };
3793CBD328286BB20060441B /* MapTo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapTo.swift; sourceTree = "<group>"; };
3793CBD528286BE90060441B /* MapToTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapToTests.swift; sourceTree = "<group>"; };
712E36C72711B79000A2AAFE /* RetryWhen.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RetryWhen.swift; sourceTree = "<group>"; };
7182326E26DAAF230026BAD3 /* RetryWhenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RetryWhenTests.swift; sourceTree = "<group>"; };
BF330EF824F20032001281FC /* Timer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -251,6 +255,7 @@
OBJ_23 /* FlatMapLatest.swift */,
OBJ_24 /* MapMany.swift */,
D836234724EA9446002353AC /* MergeMany.swift */,
3793CBD328286BB20060441B /* MapTo.swift */,
OBJ_25 /* Materialize.swift */,
C387777B24E6BBE900FAD2D8 /* Nwise.swift */,
OBJ_26 /* Partition.swift */,
Expand Down Expand Up @@ -300,6 +305,7 @@
OBJ_47 /* DematerializeTests.swift */,
OBJ_48 /* FlatMapLatestTests.swift */,
OBJ_49 /* MapManyTests.swift */,
3793CBD528286BE90060441B /* MapToTests.swift */,
1970A8B32524730400799AB6 /* FilterManyTests.swift */,
OBJ_50 /* MaterializeTests.swift */,
D836234924EA9888002353AC /* MergeManyTests.swift */,
Expand Down Expand Up @@ -573,6 +579,7 @@
OBJ_135 /* PrefixDurationTests.swift in Sources */,
OBJ_136 /* RemoveAllDuplicatesTests.swift in Sources */,
OBJ_137 /* ReplaySubjectTests.swift in Sources */,
3793CBD728286C090060441B /* MapToTests.swift in Sources */,
OBJ_138 /* SetOutputTypeTests.swift in Sources */,
OBJ_139 /* ShareReplayTests.swift in Sources */,
BFADDC8B25BCE91E00465E9B /* FlatMapBatchesTests.swift in Sources */,
Expand All @@ -590,6 +597,7 @@
OBJ_79 /* DemandBuffer.swift in Sources */,
OBJ_80 /* Sink.swift in Sources */,
OBJ_81 /* Optional.swift in Sources */,
3793CBD428286BB20060441B /* MapTo.swift in Sources */,
C387777C24E6BBE900FAD2D8 /* Nwise.swift in Sources */,
OBJ_82 /* Event.swift in Sources */,
OBJ_83 /* ObjectOwnership.swift in Sources */,
Expand Down
22 changes: 22 additions & 0 deletions Sources/Operators/MapTo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// MapTo.swift
// CombineExt
//
// Created by Dan Halliday on 08/05/2022.
// Copyright © 2022 Combine Community. All rights reserved.
//

#if canImport(Combine)
import Combine

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public extension Publisher {
/// Replace each upstream value with a constant.
///
/// - Parameter value: The constant with which to replace each upstream value.
/// - Returns: A new publisher wrapping the upstream, but with output type `Result`.
func map<Result>(to value: Result) -> Publishers.Map<Self, Result> {
map { _ in value }
}
}
#endif
32 changes: 32 additions & 0 deletions Tests/MapToTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// MapToTests.swift
// CombineExt
//
// Created by Dan Halliday on 08/05/2022.
// Copyright © 2022 Combine Community. All rights reserved.
//

import Foundation

#if !os(watchOS)
import XCTest
import Combine
import CombineExt

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
final class MapToTests: XCTestCase {
private var subscription: AnyCancellable!

func testMapToConstantValue() {
let subject = PassthroughSubject<Int, Never>()
var result: Int? = nil

subscription = subject
.map(to: 2)
.sink(receiveValue: { result = $0 })

subject.send(1)
XCTAssertEqual(result, 2)
}
}
#endif

0 comments on commit 9b72b43

Please sign in to comment.