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 Property.filter #586

Merged
merged 2 commits into from
Feb 9, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# master
*Please add new entries at the top.*

1. New property operator: `filter` (#586, kudos to @iv-mexx)
1. New operator `merge(with:)` (#600, kudos to @ra1028)
1. New operator `map(value:)` (#601, kudos to @ra1028)

Expand Down
14 changes: 14 additions & 0 deletions Sources/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ extension PropertyProtocol {
return lift { $0.map(keyPath) }
}

/// Passes only the values of the property that pass the given predicate
/// to a new property.
///
/// - parameters:
/// - initial: A `Property` always needs a `value`. The initial `value` is necessary in case the
/// predicate excludes the first (or all) `value`s of this `Property`
/// - predicate: A closure that accepts value and returns `Bool` denoting
/// whether current `value` of this `Property` has passed the test.
///
/// - returns: A property that holds only values from `self` passing the given predicate.
public func filter(initial: Value, _ predicate: @escaping (Value) -> Bool) -> Property<Value> {
return Property(initial: initial, then: self.producer.filter(predicate))
}

/// Combines the current value and the subsequent values of two `Property`s in
/// the manner described by `Signal.combineLatest(with:)`.
///
Expand Down
37 changes: 37 additions & 0 deletions Tests/ReactiveSwiftTests/PropertySpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,43 @@ class PropertySpec: QuickSpec {
}
}

describe("filter") {
it("should only receive values that pass the predicate") {
let property = MutableProperty(1)
let filteredProperty = property
.filter(initial: 0) { $0 > 0 }
expect(filteredProperty.value) == 1

property.value = 0
expect(filteredProperty.value) == 1

property.value = 2
expect(filteredProperty.value) == 2

property.value = -5
expect(filteredProperty.value) == 2

property.value = 3
expect(filteredProperty.value) == 3
}

it("should behave correctly if the filter excludes the initial value") {
let property = MutableProperty(1)
let filteredProperty = property
.filter(initial: 0) { $0 < 0 }
expect(filteredProperty.value) == 0

property.value = 2
expect(filteredProperty.value) == 0

property.value = -2
expect(filteredProperty.value) == -2

property.value = 0
expect(filteredProperty.value) == -2
}
}

describe("combineLatest") {
var property: MutableProperty<String>!
var otherProperty: MutableProperty<String>!
Expand Down