Chadtech/id/1.0.3

NOTE: this package is not compatible with Elm 0.19.1

Id

A simple Id type for your types that have ids.

Id

type Id
fromString : String -> Id

The only way to make an Id

Id.fromString "vq93rUv0A4"
encode : Id -> Value

Encode an Id

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


[ ("id", Id.encode id) ]
    |> Encode.object
    |> Encode.encode 0


-- {\"id\":\"hDFL0Cs2EqWJ4jc3kMtOrKdEUTWh\"} : String
decoder : Decoder Id

Decode an Id

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

A way to 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
    )

Origin

type Origin
= Local
| Remote Id

Data that comes from a remote source usually has an id, while data that is made locally doesnt. If thats your situation, using this Origin type might be a better way of representing your datas id.

type alias Car =
    { origin : Origin
    , make : String
    }


saveCar : Car -> Http.Request (Result Err ())
saveCar car =
    case car.origin of
        Id id ->
            Car.update id car


        New ->
            Car.create car