Skip to content

Elm module witch adds Record-like type with named keys and explicitly typed values

License

Notifications You must be signed in to change notification settings

Yuri2b/typed-record-elm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TypedRecord.elm

Data structure with named keys and explicitly typed values. Allow to represent JS Object like this:

user : {
  name: "Jane",
  age: 24,
  rating: 4.3,
  tags: ["customer", "vip"],
  // ugly named attribute for example
  st_adress: {
    city: "Montreal",
    state: "WA"
  }
}

via JSON decoding to this:

user =
  [
    ("name", String "Jane")
  , ("age", Int 24)
  , ("rating", Float 4.3)
  , ("tags",
      [ String "customer"
      , String "vip"
      ]
    )
  , ("address",
      [ ("city", String "Monreal")
      , ("state", String "WA")
      ]
    )
  ]

Decoder example:

import TypedRecord as TR
import Json.Decode as JD
import Json.Decode.Pipeline exposing (required)

type alias User =
  TR.TypedRecord

userDecoder : JD.Decoder User
userDecoder =
    JD.succeed (\a b c d e -> [ a, b, c, d, e ])
        |> required "name"      (TR.attrStringDecoder "name")
        |> required "age"       (TR.attrIntDecoder "age")
        |> required "rating"    (TR.attrFloatDecoder "rating")
        |> required "tags"      (TR.listStringDecoder "tags")
        |> required "st_adress" (TR.attrRecordDecoder "address" userAddressDecoder)

userAddressDecoder : JD.Decoder TypedRecord
userAddressDecoder =
  JD.succeed (\a b -> [a, b])
    |> required "city"  (TR.attrStringDecoder "city")
    |> required "state" (TR.attrStringDecoder "state")

Acccessing attributs

Allow getting attributes by its string keys, like user["name"] and user["address"]["state"] in JS

TR.getAttrByKey "name" user -- Just (String "Jane")
TR.getAttrByKey "age"  user -- Just (Int 24)
TR.getAttrByKey "tags" user -- Just ([String "customer", String "vip"])
TR.getAttrByKey "address.state"  user -- Just (String "WA")
TR.getAttrByKey "surname"  user -- Nothing

and convert these to String for HTML.text in view:

user
  |> TR.getAttrByKey "rating"
  |> TR.attrToString
  |> Maybe.withDefault "not set yet"

Sorting and Filtering

Sorting by chosen key and order. Nested attribute's keys like "address.city" are also supported

users =
  [
    [("id", Int 5), ("name", String "Daisy")]
  , [("id", Int 7), ("name", String "Ahmad")]
  , [("id", Int 2), ("name", String "Coralas")]
  ]

users
  |> TR.sortedBy ("id", "asc")
  {-- 
      [
        [("id", Int 2), ("name", String "Coralas")]
      , [("id", Int 5), ("name", String "Daisy")]
      , [("id", Int 7), ("name", String "Ahmad")]
      ] 
  --}

users
  |> TR.sortedBy ("name", "dsc")
  {-- 
      [
      , [("id", Int 5), ("name", String "Daisy")]
        [("id", Int 2), ("name", String "Coralas")]
      , [("id", Int 7), ("name", String "Ahmad")]
      ] 
  --}

Filtering by substring matching in choosen attributes. Nested attribute's keys like "address.city" are also supported

users =
  [
    [("id", Int 5), ("name", String "Daisy"), ("email", String "@mail.org")]
  , [("id", Int 8), ("name", String "Kornetty"), ("email", String "@server.org")]
  , [("id", Int 2), ("name", String "Coralas"), ("email", String "@postamt.net")]
  ]

users
  |> TR.filteredBy ["email"] "org"
  {-- 
    [
    , [("id", Int 5), ("name", String "Daisy"), ("email", String "@mail.org")]
      [("id", Int 8), ("name", String "Kornetty"), ("email", String "@server.org")]
    ] 
--}

users
  |> TR.filteredBy ["email", "name"] "net"
  {-- 
    [
      [("id", Int 8), ("name", String "Kornetty"), ("email", String "@server.org")]
    , [("id", Int 2), ("name", String "Coralas"), ("email", String "@postamt.net")]
    ] 
--}

About

Elm module witch adds Record-like type with named keys and explicitly typed values

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages