wechat/object

object module provides core javascript object model it is like a gleam/json object with functions

Examples

object.literal([#("s", "a string"), #("ss", "another string")])
|> object.set("i", 42)
|> object.set("f", fn(i) { i + 1 })

is exactly like

{
  s: 'a string',
  ss: 'another string',
  i: 42,
  f(i) { i + 1 }
}

Types

JsObject models a javascript object

pub type JsObject {
  JsObject
}

Constructors

  • JsObject

most simple callbacks are of this type

pub type WechatCallback =
  fn() -> Nil

error constructors

pub type WechatError {
  NilError(n: Nil)
  WechatError(e: String)
  WechatDecodeError(es: DecodeErrors)
}

Constructors

  • NilError(n: Nil)
  • WechatError(e: String)
  • WechatDecodeError(es: DecodeErrors)

most APIs returns either this or a Promise of this type

pub type WechatResult =
  Result(JsObject, WechatError)

Functions

pub fn bool(o: JsObject) -> Result(Bool, WechatError)

convert to bool with gleam/dynamic

pub fn call(o: JsObject, v: a) -> Result(JsObject, WechatError)

execute synchronously with prameter v if this object is a function

pub fn dict(o: JsObject) -> Dict(String, JsObject)

convert to gleam/dict

pub fn dynamic(o: JsObject) -> Dynamic

convert to gleam/dynamic

pub fn exist(o: JsObject, k: a) -> Bool

check if a key k exists

pub fn field(
  o: JsObject,
  name a: a,
  of b: fn(Dynamic) -> Result(b, List(DecodeError)),
) -> Result(b, WechatError)

retrieve and convert a sub object with key a of type t using the decoder of type Decoder(t), thanks to gleam/dynamic

pub fn float(o: JsObject) -> Result(Float, WechatError)

convert to float with gleam/dynamic

pub fn get(o: JsObject, k: a) -> Result(JsObject, WechatError)

alias for path

pub fn get_kv(
  o: JsObject,
  key k: String,
) -> Result(JsObject, WechatError)

retrive a sub javascript object by key k as if it were a dict(String, JsObject)

pub fn int(o: JsObject) -> Result(Int, WechatError)

convert to int with gleam/dynamic

pub fn json(o: JsObject) -> Json

convert to gleam/json

pub fn list(
  o: JsObject,
  of f: fn(Dynamic) -> Result(a, List(DecodeError)),
) -> Result(List(a), WechatError)

convert to list(t) with gleam/dynamic

pub fn literal(ls: List(#(a, b))) -> JsObject

convenient constructor if the (k, v) pairs are of same type since gleam is strongly typed

pub fn merge(o: JsObject, n: JsObject) -> JsObject

merge two javascript objects

pub fn new() -> JsObject

constructor of an empty javascript object

pub fn path(o: JsObject, k: a) -> Result(JsObject, WechatError)

retrieve a sub javascript object by key k

pub fn paths(
  o: JsObject,
  path p: String,
) -> Result(JsObject, WechatError)

retrive recursively the sub javascript object by path

Examples

let f = fn(x) { x + 1 }
let b = object.literal([#("a", f)])
object.literal([#("b", b)])
|> object.paths("b.a")
|> result.map(type_of)
|> should.equal(Ok(javascript.FunctionType))
pub fn set(o: JsObject, k: a, v: b) -> JsObject

set a key k with value v

Examples

object.new()
|> object.set("a", 1)
|> object.get("a")
|> result.try(object.int)
|> should.equal(Ok(1))
pub fn string(o: JsObject) -> Result(String, WechatError)

convert to string with gleam/dynamic

pub fn stringify(o: JsObject) -> String

JSON.stringify for the javascript object

Search Document