From d452f95871e847b656b48b654c6d3e499f8452b3 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Tue, 10 Dec 2024 13:45:08 +0000 Subject: Decode JS objects, maps, weakmaps --- src/gleam_stdlib_decode_ffi.mjs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/gleam_stdlib_decode_ffi.mjs b/src/gleam_stdlib_decode_ffi.mjs index 86c9c60..58291b5 100644 --- a/src/gleam_stdlib_decode_ffi.mjs +++ b/src/gleam_stdlib_decode_ffi.mjs @@ -64,5 +64,18 @@ export function dict(data) { if (data instanceof Dict) { return new Ok(data); } - return new Error(); + if (data instanceof Map || data instanceof WeakMap) { + return new Ok(Dict.fromMap(data)); + } + if (data == null) { + return new Error("Dict"); + } + if (typeof data !== "object") { + return new Error("Dict"); + } + const proto = Object.getPrototypeOf(data); + if (proto === Object.prototype || proto === null) { + return new Ok(Dict.fromObject(data)); + } + return new Error("Dict"); } -- cgit v1.2.3