Orasund / elm-handlebars / Handlebars.Value

Represents Json values inside the Handelbar context.


type Value
    = StringValue String
    | BooleanValue Basics.Bool
    | ArrayValue (Array Value)
    | ObjectValue (Dict String Value)

Handlebar values

undefined =
    BoolenValue False

null =
    BooleanValue False

int =
    StringValue (String.fromInt int)

float =
    StringValue (String.fromFloat float)

get : Handlebars.Path.Path -> Value -> Maybe Value

Get a value by the path.

Use @key to get the key of a value.

import Dict

object : Value
object = Dict.fromList [ ( "name", StringValue "Jack" ) ] |> ObjectValue

object
    |> get [ "name" ]
    --> Just (StringValue "Jack")

object
    |> get [ "name", "@key" ]
    --> Just (StringValue "name")

[ ( "name"
  , [ ( "@key", StringValue "impossible State" ) ] |> Dict.fromList |> ObjectValue
  )
]
    |> Dict.fromList
    |> ObjectValue
    |> get [ "name", "@key" ]
    --> Just (StringValue "name")

object
    |> get [ "@key" ]
    --> Nothing

object
    |> get []
    --> Just object

Use @index to get the index

import Array

jack : Value
jack = StringValue "Jack"

gill : Value
gill = StringValue "Gill"

array : Value
array =
    [ jack, gill ]
    |> Array.fromList
    |> ArrayValue

array
    |> get []
    --> Just array

array
    |> get ["0"]
    --> Just jack

array
    |> get ["0","@index"]
    --> Just (StringValue "0")

array
    |> get ["0","@first"]
    --> Just (BooleanValue True)

array
    |> get ["0","@last"]
    --> Just (BooleanValue False)

array
    |> get ["1"]
    --> Just gill

array
    |> get ["1","@index"]
    --> Just (StringValue "1")

array
    |> get ["1","@last"]
    --> Just (BooleanValue True)

array
    |> get ["0","@last"]
    --> Just (BooleanValue False)

array
    |> get ["-1"]
    --> Nothing

Array.empty
    |> ArrayValue
    |> get ["0"]
    --> Nothing

Array.empty
    |> ArrayValue
    |> get ["0","@index"]
    --> Nothing

Array.empty
    |> ArrayValue
    |> get ["0","@first"]
    --> Nothing

Array.empty
    |> ArrayValue
    |> get ["0","@last"]
    --> Nothing

For Strings and Boolean only an empty path is allowed.

jack
    |> get ["something"]
    --> Nothing

jack
    |> get []
    --> Just jack

isValid : Value
isValid =
    BooleanValue True

isValid
    |> get ["something"]
    --> Nothing

isValid
    |> get []
    --> Just isValid

fromJson : Json.Encode.Value -> Value

convert Json into Handlebar values