docspell/modules/webapp/src/main/elm/Util/Maybe.elm

69 lines
1012 B
Elm
Raw Normal View History

2019-12-29 20:55:12 +00:00
module Util.Maybe exposing
( filter
, fromString
2020-01-05 22:23:28 +00:00
, isEmpty
2019-12-29 20:55:12 +00:00
, nonEmpty
, or
, withDefault
)
2019-12-29 20:55:12 +00:00
nonEmpty : Maybe a -> Bool
nonEmpty ma =
2019-12-29 20:55:12 +00:00
not (isEmpty ma)
2019-12-29 20:55:12 +00:00
isEmpty : Maybe a -> Bool
isEmpty ma =
2019-12-29 20:55:12 +00:00
ma == Nothing
2019-12-29 20:55:12 +00:00
withDefault : Maybe a -> Maybe a -> Maybe a
withDefault ma1 ma2 =
2019-12-29 20:55:12 +00:00
if isEmpty ma2 then
ma1
else
ma2
2019-12-29 20:55:12 +00:00
or : List (Maybe a) -> Maybe a
or listma =
case listma of
2019-12-29 20:55:12 +00:00
[] ->
Nothing
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
filter : (a -> Bool) -> Maybe a -> Maybe a
filter predicate ma =
let
check v =
if predicate v then
Just v
else
Nothing
in
Maybe.andThen check ma