mdgriffith/elm-codegen/2.1.0

NOTE: the latest version is 6.0.3

Elm.Case

Generate a case expression!

Here's an example for extracting a Maybe Int

Elm.Case.maybe myMaybe
    { nothing = Elm.int 0
    , just =
        ( "value"
        , \content ->
            Elm.plus (Elm.int 5) content
        )
    }

Generates

case myMaybe of
    Nothing ->
        0


    Just value ->
        value + 5
Expression
-> { nothing : Expression
, just : ( String, Expression -> Expression )
}
-> Expression
Expression
-> { err : ( String, Expression -> Expression )
, ok : ( String, Expression -> Expression )
}
-> Expression
Elm.Case.result myResult
    { ok =
        Tuple.pair "ok" <|
            \ok ->
                Elm.string "No errors"
    , err =
        Tuple.pair "err" <|
            \err ->
                err
    }

Generates

case myResult of
    Ok ok ->
        "No errors"


    Err err ->
        err
Expression
-> { empty : Expression
, nonEmpty : Expression -> Expression -> Expression
}
-> Expression

Let's unpack the first value from a list.

Elm.Case.list myList
    { empty = Elm.int 0
    , nonEmpty =
        \top remaining ->
            Elm.plus (Elm.int 5) top
    }

Generates

case myList of
    [] ->
        0


    top :: remaining ->
        top + 5

Note if you want more control over unpacking lists, check out branchList

Expression
-> { cases : List ( String, Expression )
, otherwise : Expression
}
-> Expression
Expression
-> String
-> String
-> (Expression -> Expression -> Expression)
-> Expression
Elm.Case.tuple myTuple
    "first"
    "second"
    (\one two ->
        Elm.plus (Elm.int 5) two
    )

Generates

case myTuple of
    ( first, second ) ->
        5 + second
Expression
-> String
-> String
-> String
-> (Expression -> Expression -> Expression -> Expression)
-> Expression
Elm.Case.triple myTriple
    "one"
    "two"
    "three"
    (\one two three ->
        Elm.plus (Elm.int 5) two
    )

Generates

case myTriple of
    ( one, two, three ) ->
        5 + two

Case on a Custom Type

Elm.Case.custom maybeString
    (Elm.Annotation.maybe Elm.Annotation.string)
    [ Elm.Case.branch0 "Nothing"
        (Elm.string "It's nothing, I swear!")
    , Elm.Case.branch1 "Just" ( "val", Elm.Annotation.string ) <|
        \val ->
            Elm.append (Elm.string "Actually, it's: ") val
    ]

Generates

case maybeString of
    Nothing ->
        "It's nothing, I swear!"


    Just just ->
        "Actually, it's: " ++ just
custom : Expression -> Annotation -> List Branch -> Expression
type Branch
otherwise : (Expression -> Expression) -> Branch

A catchall branch in case you want the case to be nonexhaustive.

branch0 : String -> Expression -> Branch
String
-> ( String, Annotation )
-> (Expression -> Expression)
-> Branch
String
-> ( String, Annotation )
-> ( String, Annotation )
-> (Expression -> Expression -> Expression)
-> Branch
String
-> ( String, Annotation )
-> ( String, Annotation )
-> ( String, Annotation )
-> (Expression -> Expression -> Expression -> Expression)
-> Branch
String
-> ( String, Annotation )
-> ( String, Annotation )
-> ( String, Annotation )
-> ( String, Annotation )
-> (Expression -> Expression -> Expression -> Expression -> Expression)
-> Branch
String
-> ( String, Annotation )
-> ( String, Annotation )
-> ( String, Annotation )
-> ( String, Annotation )
-> ( String, Annotation )
-> ( Expression
-> Expression
-> Expression
-> Expression
-> Expression
-> Expression
)
-> Branch
String
-> ( String, Annotation )
-> ( String, Annotation )
-> ( String, Annotation )
-> ( String, Annotation )
-> ( String, Annotation )
-> ( String, Annotation )
-> ( Expression
-> Expression
-> Expression
-> Expression
-> Expression
-> Expression
-> Expression
)
-> Branch
branchWith : String -> Int -> (List Expression -> Expression) -> Branch
branchList : Int -> (List Expression -> Expression) -> Branch