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

OR-5269 Operators:: Sequence Operators:: Finding values:: max(by:) #113

Merged
merged 1 commit into from
Aug 5, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import XCTest

- `max()` Publishes the maximum value received from the upstream publisher, after it finishes.
- https://developer.apple.com/documentation/combine/publishers/reduce/max()

- `max(by:)` Publishes the maximum value received from the upstream publisher, using the provided ordering closure.
- A closure that receives two elements and returns true if they’re in increasing order.
- https://developer.apple.com/documentation/combine/publishers/reduce/max(by:)
*/
final class SequenceFindingValuesTests: XCTestCase {
var cancellables: Set<AnyCancellable>!
Expand Down Expand Up @@ -124,4 +128,37 @@ final class SequenceFindingValuesTests: XCTestCase {
XCTAssertTrue(isFinishedCalled)
XCTAssertEqual(receivedValues, [10])
}

func testPublisherWithMaxByOperator() {
// Given: Publisher
let publisher = [
"12345",
"ab",
"hello world"
]
.compactMap { $0.data(using: .utf8) } // [Data]
.publisher // Publisher<Data, Never>

var receivedValues: [String] = []

// When: Sink(Subscription)
// Data doesn't conform to Comparable, that's why using the max(by:) operator to find the Data-object with the largest number of bytes.
// The publisher emits all its Data objects and finishes, then max(by:) finds and emits the data with the largest byte size and sink receives it.
publisher
.max(by: { $0.count < $1.count }) // Returns the maximum value (based on Data-bytes count), after upstream will finish!
.sink { [weak self] completion in
switch completion {
case .finished:
self?.isFinishedCalled = true
}
} receiveValue: { value in
let stringValue = String(data: value, encoding: .utf8)!
receivedValues.append(stringValue)
}
.store(in: &cancellables)

// Then: Receiving correct value
XCTAssertTrue(isFinishedCalled)
XCTAssertEqual(receivedValues, ["hello world"])
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
- `min()` https://github.com/crazymanish/what-matters-most/pull/110
- `min(by:)` https://github.com/crazymanish/what-matters-most/pull/111
- `max()` https://github.com/crazymanish/what-matters-most/pull/112
- `max(by:)` https://github.com/crazymanish/what-matters-most/pull/113
- [ ] Query the publisher
- [ ] Practices

Expand Down