From 5bd95699d236d3d3279300cd9d6b0efcf0a732eb Mon Sep 17 00:00:00 2001 From: Bram Date: Mon, 18 Dec 2023 17:19:12 +0100 Subject: [PATCH] Add function for Envelope --- src/Internal/Values/Envelope.elm | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Internal/Values/Envelope.elm b/src/Internal/Values/Envelope.elm index 08e9973..6d302d0 100644 --- a/src/Internal/Values/Envelope.elm +++ b/src/Internal/Values/Envelope.elm @@ -1,6 +1,6 @@ module Internal.Values.Envelope exposing ( Envelope, init - , map, mapMaybe + , map, mapMaybe, mapList , Settings, mapSettings, extractSettings , mapContext , getContent, extract @@ -18,7 +18,7 @@ settings that can be adjusted manually. ## Manipulate -@docs map, mapMaybe +@docs map, mapMaybe, mapList ## Settings @@ -247,6 +247,26 @@ mapSettings f (Envelope data) = } +{-| Map the contents of a function, where the result is wrapped in a `List` +type. This can be useful when you are mapping to a list of individual values +that you would all like to see enveloped. + + type alias User = + { name : String, age : Int } + + type alias Company = + { name : String, employees : List User } + + getEmployees : Envelope Company -> List (Envelope User) + getEmployees envelope = + mapList .employees envelope + +-} +mapList : (a -> List b) -> Envelope a -> List (Envelope b) +mapList f = + map f >> toList + + {-| Map the contents of a function, where the result is wrapped in a `Maybe` type. This can be useful when you are not guaranteed to find the value you're looking for. @@ -267,6 +287,13 @@ mapMaybe f = map f >> toMaybe +toList : Envelope (List a) -> List (Envelope a) +toList (Envelope data) = + List.map + (\content -> map (always content) (Envelope data)) + data.content + + toMaybe : Envelope (Maybe a) -> Maybe (Envelope a) toMaybe (Envelope data) = Maybe.map