Chadtech/id/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

Id

This package exposes a really simple type called Id. Its for when your data has an id. Such as..


import Id exposing (Id)

type alias User =
    { id : Id 
    , email : String
    }

You can make Ids from strings..


import Id exposing (Id)

user : User
user = 
    { id = Id.fromString "400" 
    , email = "[email protected]"
    }

Or you can generate random Ids..


import Id exposing (Id)
import Random.Pcg as Random exposing (Seed)


user : Seed -> ( User, Seed )
user seed =
    let
        ( id, nextSeed ) =
            Random.step Id.generator seed
    in
    ( { id = id, email = "[email protected]" }
    , nextSeed
    )

Why use an Id instead of a String? Technically, both the following are possible with String ids..


coolString : User -> String
coolString user =
    user.email ++ user.id


screwUpUser : User -> User
screwUpUser user =
    { user
        | id = user.email
        , email = user.id
    }

Admittedly these errors seem implausible, but using a separate Id type eliminates them entirely. Theres really no functionality lost either, since ids arent meant to change over the life span of the data. Put another way: there really shouldnt be any functionality with ids, so using this Id type ensures your expectations.

And then of course you can decode and encode Ids too..


Encode.encode 0 (Id.encode id)
-- "\"hDFL0Cs2EqWJ4jc3kMtOrKdEUTWh\"" : String

Decode.decodeString (Decode.field "id" Id.decoder) "{\"id\":\"19\"}"
-- Ok (Id 19) : Result String Id