Punie/elm-id/1.0.0

NOTE: the latest version is 1.0.3

Id

An Id type that leverages the type system (thanks to phantom types) to ensure that it refers to the right resource.

For example, if your application handles users with integer-based IDs, and articles with string-based IDs, you could define them as such:

type User
    = User
        { id : UserID
        , name : String
        }


type Article
    = Article
        { id : ArticleID
        , title : String
        }


type alias UserID =
    Id Int User


type alias ArticleID =
    Id String Article

The Id type

type Id a resource

The Id a resource type handles IDs whose representation are of type a that refers to resources of type resource.

Conversions

from : a -> Id a resource

Make an Id from its representation.

userID : Id Int User
userID =
    Id.from 5


articleID : Id String Article
articleID =
    Id.from "e4edf8a"
to : Id a resource -> a

Extract the raw representation from an Id.

Id.to userID == 5


Id.to articleID == "e4edf8a"

Serialization

encode : (a -> Value) -> Id a resource -> Value

Encode an Id.

encodeUserID : Id Int User -> Value
encodeUserID =
    Id.encode Json.Encode.int


encodeArticleID : Id String Article -> Value
encodeArticleID =
    Id.encode Json.Encode.string
decoder : Decoder a -> Decoder (Id a resource)

Decode an Id.

userIDDecoder : Decoder (Id Int User)
userIDDecoder =
    Id.decoder Json.Decode.int


articleIDDecoder : Decoder (Id String Article)
articleIDDecoder =
    Id.decoder Json.Decode.string