2019-12-29 20:55:12 +00:00
|
|
|
module Util.Maybe exposing
|
2020-06-14 12:38:56 +00:00
|
|
|
( filter
|
|
|
|
, fromString
|
2020-01-05 22:23:28 +00:00
|
|
|
, isEmpty
|
2019-12-29 20:55:12 +00:00
|
|
|
, nonEmpty
|
|
|
|
, or
|
|
|
|
, withDefault
|
|
|
|
)
|
2019-07-22 22:53:30 +00:00
|
|
|
|
2019-12-29 20:55:12 +00:00
|
|
|
|
|
|
|
nonEmpty : Maybe a -> Bool
|
2019-07-22 22:53:30 +00:00
|
|
|
nonEmpty ma =
|
2019-12-29 20:55:12 +00:00
|
|
|
not (isEmpty ma)
|
|
|
|
|
2019-07-22 22:53:30 +00:00
|
|
|
|
2019-12-29 20:55:12 +00:00
|
|
|
isEmpty : Maybe a -> Bool
|
2019-07-22 22:53:30 +00:00
|
|
|
isEmpty ma =
|
2019-12-29 20:55:12 +00:00
|
|
|
ma == Nothing
|
2019-07-22 22:53:30 +00:00
|
|
|
|
2019-12-29 20:55:12 +00:00
|
|
|
|
|
|
|
withDefault : Maybe a -> Maybe a -> Maybe a
|
2019-07-22 22:53:30 +00:00
|
|
|
withDefault ma1 ma2 =
|
2019-12-29 20:55:12 +00:00
|
|
|
if isEmpty ma2 then
|
|
|
|
ma1
|
|
|
|
|
|
|
|
else
|
|
|
|
ma2
|
2019-07-22 22:53:30 +00:00
|
|
|
|
2019-12-29 20:55:12 +00:00
|
|
|
|
|
|
|
or : List (Maybe a) -> Maybe a
|
2019-07-22 22:53:30 +00:00
|
|
|
or listma =
|
|
|
|
case listma of
|
2019-12-29 20:55:12 +00:00
|
|
|
[] ->
|
|
|
|
Nothing
|
|
|
|
|
2019-07-22 22:53:30 +00:00
|
|
|
el :: els ->
|
|
|
|
case el of
|
2019-12-29 20:55:12 +00:00
|
|
|
Just _ ->
|
|
|
|
el
|
|
|
|
|
|
|
|
Nothing ->
|
|
|
|
or els
|
2020-01-05 22:23:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
fromString : String -> Maybe String
|
|
|
|
fromString str =
|
|
|
|
let
|
|
|
|
s =
|
|
|
|
String.trim str
|
|
|
|
in
|
|
|
|
if s == "" then
|
|
|
|
Nothing
|
|
|
|
|
|
|
|
else
|
|
|
|
Just str
|
2020-06-14 12:38:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
filter : (a -> Bool) -> Maybe a -> Maybe a
|
|
|
|
filter predicate ma =
|
2020-08-07 22:06:23 +00:00
|
|
|
let
|
|
|
|
check v =
|
2020-06-14 12:38:56 +00:00
|
|
|
if predicate v then
|
|
|
|
Just v
|
|
|
|
|
|
|
|
else
|
|
|
|
Nothing
|
2020-08-07 22:06:23 +00:00
|
|
|
in
|
|
|
|
Maybe.andThen check ma
|