From 9b72b4389c561ed0b8eedb4272bcb1dec3a9dd0a Mon Sep 17 00:00:00 2001 From: Dan Halliday Date: Thu, 12 May 2022 06:21:42 +0100 Subject: [PATCH] Add `map(to:)` Operator (#126) --- CombineExt.xcodeproj/project.pbxproj | 8 +++++++ Sources/Operators/MapTo.swift | 22 +++++++++++++++++++ Tests/MapToTests.swift | 32 ++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 Sources/Operators/MapTo.swift create mode 100644 Tests/MapToTests.swift diff --git a/CombineExt.xcodeproj/project.pbxproj b/CombineExt.xcodeproj/project.pbxproj index c5581cd..0a79b37 100644 --- a/CombineExt.xcodeproj/project.pbxproj +++ b/CombineExt.xcodeproj/project.pbxproj @@ -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 */; }; @@ -110,6 +112,8 @@ /* Begin PBXFileReference section */ 1970A8A925246FBD00799AB6 /* FilterMany.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterMany.swift; sourceTree = ""; }; 1970A8B32524730400799AB6 /* FilterManyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilterManyTests.swift; sourceTree = ""; }; + 3793CBD328286BB20060441B /* MapTo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapTo.swift; sourceTree = ""; }; + 3793CBD528286BE90060441B /* MapToTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapToTests.swift; sourceTree = ""; }; 712E36C72711B79000A2AAFE /* RetryWhen.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RetryWhen.swift; sourceTree = ""; }; 7182326E26DAAF230026BAD3 /* RetryWhenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RetryWhenTests.swift; sourceTree = ""; }; BF330EF824F20032001281FC /* Timer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Timer.swift; sourceTree = ""; }; @@ -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 */, @@ -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 */, @@ -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 */, @@ -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 */, diff --git a/Sources/Operators/MapTo.swift b/Sources/Operators/MapTo.swift new file mode 100644 index 0000000..a7be8d8 --- /dev/null +++ b/Sources/Operators/MapTo.swift @@ -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(to value: Result) -> Publishers.Map { + map { _ in value } + } +} +#endif diff --git a/Tests/MapToTests.swift b/Tests/MapToTests.swift new file mode 100644 index 0000000..8361b93 --- /dev/null +++ b/Tests/MapToTests.swift @@ -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() + var result: Int? = nil + + subscription = subject + .map(to: 2) + .sink(receiveValue: { result = $0 }) + + subject.send(1) + XCTAssertEqual(result, 2) + } +} +#endif