Add function for Envelope
parent
447a18ab04
commit
5bd95699d2
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue