Skip to content

Commit

Permalink
Merge pull request #9 from tinora/main
Browse files Browse the repository at this point in the history
#8 add a compactMap function
  • Loading branch information
SwiftedMind authored Feb 29, 2024
2 parents 1942a18 + 90a1e2b commit 90bad98
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Sources/Processed/Loadable/LoadableState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ extension LoadableState {
case .loaded(let data): try .loaded(transform(data))
}
}

// MARK: - compactMap

/// Transforms the current `LoadableState` with a specified value type to a new `LoadableState`
/// with a different value type.
///
/// This method applies a transformation function to the loaded value of the current state,
/// if available, and returns a new `LoadableState` instance with that transformed value.
/// If the current state is `absent`, `loading`, or `error`, it returns the same state
/// without applying the transformation.
///
/// - Parameter transform: A closure that takes the current value of type `Value` and returns a
/// new value of type `T?`. If the result of the transform is nil `absent` will be returned as resulting state.
/// The closure can throw an error, in which case the method will rethrow it.
/// - Returns: A new `LoadableState` instance with the transformed value of type `T`,
/// or the same state if the current state is `absent`, `loading`, or `error`.
/// - Throws: Rethrows any error that the `transform` closure might throw.
public func compactMap<T>(_ transform: (Value) throws -> T?) rethrows -> LoadableState<T> {
switch self {
case .absent: return .absent
case .loading: return .loading
case .error(let error): return .error(error)
case .loaded(let value):
if let transformed = try transform(value) {
return .loaded(transformed)
}
return .absent
}
}
}

extension LoadableState: Sendable where Value: Sendable {}
Expand Down

0 comments on commit 90bad98

Please sign in to comment.