jmpavlick/bimap - version: 1.2.0

for more information visit the package's GitHub page

Package contains the following modules:

Bimap: Simple bidirectional mapping between Strings and custom types

This package provides a type-safe data structure and helper functions for mapping between a String and a custom type, whose values don't take any parameters.

Think of it as sugar around the general idea of an "enum" in other languages.

Supports:

import Bimap exposing (Bimap)

type Response
    = Yes
    | No

bimap = Bimap Response
bimap =
    Bimap.init
        (\yes no value ->
            case value of
                Yes ->
                    yes

                No ->
                    no
        )
        |> Bimap.variant "Yes" Yes
        |> Bimap.variant "No" No
        |> Bimap.build


Bimap.toString bimap Yes --> "Yes"

Bimap.fromString bimap "No" --> Just No

Bimap.fromString bimap "Maybe" --> Nothing

Inspired by miniBill/elm-codec. Useful whenever you need to map between Strings and custom types, but JSON values aren't involved.