This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

ModularDesign.Helpers

Some generic helper functions for use with the Modular Design framework

Lists

wrapList : a -> List a

Return a one-item list containing the argument

("key", "value") |> wrapList   --> [ ("key", "value") ]

Error Handling

thenTry : (a -> Result x b) -> Result x a -> Result x b

Same as Result.andThen, but flips the order of the arguments, allowing for cleaner syntax when used with the |> operator.

formInput
  |> getInputAt "birthYear"
  |> thenTry readIntInput
  != -1

Type Conversion

toBool : String -> Result String Bool

Convert a boolean string to a Bool, ignoring case

toBool "true"     --> Ok True
toBool "True"     --> Ok True
toBool "false"    --> Ok False
toBool "False"    --> Ok False
toBool "blah"     --> Err "String argument must be 'true' or 'false' (case ignored)"
module ModularDesign.Helpers exposing
  ( wrapList, thenTry, toBool )


{-|

## Some generic helper functions for use with the Modular Design framework

# Lists
@docs wrapList

# Error Handling
@docs thenTry

# Type Conversion
@docs toBool

-}


import String
import Result


{-| Return a one-item list containing the argument

    ("key", "value") |> wrapList   --> [ ("key", "value") ]
-}
wrapList : a -> List a
wrapList a =
  [ a ]


{-| Same as
[`Result.andThen`](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#andThen),
but flips the order of the arguments, allowing for cleaner syntax when used with
the `|>` operator.

    formInput
      |> getInputAt "birthYear"
      |> thenTry readIntInput
      != -1
-}
thenTry : (a -> Result x b) -> Result x a -> Result x b
thenTry callback result =
  Result.andThen result callback


{-| Convert a boolean string to a `Bool`, ignoring case

    toBool "true"     --> Ok True
    toBool "True"     --> Ok True
    toBool "false"    --> Ok False
    toBool "False"    --> Ok False
    toBool "blah"     --> Err "String argument must be 'true' or 'false' (case ignored)"
-}
toBool : String -> Result String Bool
toBool boolString =
  case boolString |> String.toLower of
    "true" ->
      Ok True

    "false" ->
      Ok False

    _ ->
      Err "String argument must be 'true' or 'false' (case ignored)"