forked from CombineCommunity/CombineExt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
map(to:)
Operator (CombineCommunity#126)
- Loading branch information
1 parent
36e8564
commit 9b72b43
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |