Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add map(to:) operator #601

Merged
merged 12 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# master
*Please add new entries at the top.*

1. New operator `map(value:)` (#601, kudos to @ra1028)

# 3.1.0
1. Fixed `schedule(after:interval:leeway:)` being cancelled when the returned `Disposable` is not retained. (#584, kudos to @jjoelson)

Expand Down
10 changes: 10 additions & 0 deletions Sources/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ extension PropertyProtocol {
public func map<U>(_ transform: @escaping (Value) -> U) -> Property<U> {
return lift { $0.map(transform) }
}

/// Map the current value and all susequent values to a new constant property.
///
/// - parameters:
/// - value: A new value.
///
/// - returns: A property that holds a mapped value from `self`.
public func map<U>(value: U) -> Property<U> {
return lift { $0.map(value: value) }
}

/// Maps the current value and all subsequent values to a new property
/// by applying a key path.
Expand Down
10 changes: 10 additions & 0 deletions Sources/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,16 @@ extension Signal {
public func map<U>(_ transform: @escaping (Value) -> U) -> Signal<U, Error> {
return flatMapEvent(Signal.Event.map(transform))
}

/// Map each value in the signal to a new constant value.
///
/// - parameters:
/// - value: A new value.
///
/// - returns: A signal that will send new values.
public func map<U>(value: U) -> Signal<U, Error> {
return map { _ in value }
}

/// Map each value in the signal to a new value by applying a key path.
///
Expand Down
11 changes: 11 additions & 0 deletions Sources/SignalProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,17 @@ extension SignalProducer {
public func map<U>(_ transform: @escaping (Value) -> U) -> SignalProducer<U, Error> {
return core.flatMapEvent(Signal.Event.map(transform))
}

/// Map each value in the producer to a new constant value.
///
/// - parameters:
/// - value: A new value.
///
/// - returns: A signal producer that, when started, will send a mapped
/// value of `self`.
public func map<U>(value: U) -> SignalProducer<U, Error> {
return lift { $0.map(value: value) }
}

/// Map each value in the producer to a new value by applying a key path.
///
Expand Down
12 changes: 10 additions & 2 deletions Tests/ReactiveSwiftTests/PropertySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -732,13 +732,21 @@ class PropertySpec: QuickSpec {
describe("map") {
it("should transform the current value and all subsequent values") {
let property = MutableProperty(1)
let mappedProperty = property
.map { $0 + 1 }
let mappedProperty = property.map { $0 + 1 }
expect(mappedProperty.value) == 2

property.value = 2
expect(mappedProperty.value) == 3
}

it("should transform the current value and all subsequent values to a constant value") {
let property = MutableProperty("foo")
let mappedProperty = property.map(value: 1)
expect(mappedProperty.value) == 1

property.value = "foobar"
expect(mappedProperty.value) == 1
}

it("should work with key paths") {
let property = MutableProperty("foo")
Expand Down
19 changes: 19 additions & 0 deletions Tests/ReactiveSwiftTests/SignalProducerLiftingSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ class SignalProducerLiftingSpec: QuickSpec {
observer.send(value: 1)
expect(lastValue) == "2"
}

it("should raplace the values of the signal to constant new value") {
let (producer, observer) = SignalProducer<String, NoError>.pipe()
let mappedProducer = producer.map(value: 1)

var lastValue: Int?

mappedProducer.startWithValues {
lastValue = $0
}

expect(lastValue).to(beNil())

observer.send(value: "foo")
expect(lastValue) == 1

observer.send(value: "foobar")
expect(lastValue) == 1
}
}

describe("mapError") {
Expand Down
24 changes: 21 additions & 3 deletions Tests/ReactiveSwiftTests/SignalSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,9 @@ class SignalSpec: QuickSpec {
expect(lastValue) == "2"
}

it("should support key paths") {
it("should replace the values of the signal to constant new value") {
let (signal, observer) = Signal<String, NoError>.pipe()
let mappedSignal = signal.map(\String.count)
let mappedSignal = signal.map(value: 1)

var lastValue: Int?
mappedSignal.observeValues {
Expand All @@ -518,8 +518,26 @@ class SignalSpec: QuickSpec {
expect(lastValue).to(beNil())

observer.send(value: "foo")
expect(lastValue) == 3
expect(lastValue) == 1

observer.send(value: "foobar")
expect(lastValue) == 1
}

it("should support key paths") {
let (signal, observer) = Signal<String, NoError>.pipe()
let mappedSignal = signal.map(\String.count)

var lastValue: Int?
mappedSignal.observeValues {
lastValue = $0
}

expect(lastValue).to(beNil())

observer.send(value: "foo")
expect(lastValue) == 3

observer.send(value: "foobar")
expect(lastValue) == 6
}
Expand Down