From 90a1e2bbf602f77550b6c0fcd2c012c3642999f7 Mon Sep 17 00:00:00 2001 From: Elisabeth Rachui Date: Thu, 29 Feb 2024 15:06:51 +0100 Subject: [PATCH] #8 add a compactMap function --- .../Processed/Loadable/LoadableState.swift | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Sources/Processed/Loadable/LoadableState.swift b/Sources/Processed/Loadable/LoadableState.swift index f27ff7a..5cbdb36 100644 --- a/Sources/Processed/Loadable/LoadableState.swift +++ b/Sources/Processed/Loadable/LoadableState.swift @@ -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(_ transform: (Value) throws -> T?) rethrows -> LoadableState { + 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 {}