diff options
author | Louis Pilfold <louis@lpil.uk> | 2024-12-10 13:45:08 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-12-22 10:56:21 +0000 |
commit | d452f95871e847b656b48b654c6d3e499f8452b3 (patch) | |
tree | 8b86e5aa031e569f797822692afb6ecb456426b6 /src | |
parent | f5585adfb4a969d9167817041af0e8c4334e84ec (diff) | |
download | gleam_stdlib-d452f95871e847b656b48b654c6d3e499f8452b3.tar.gz gleam_stdlib-d452f95871e847b656b48b654c6d3e499f8452b3.zip |
Decode JS objects, maps, weakmaps
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam_stdlib_decode_ffi.mjs | 15 |
1 files changed, 14 insertions, 1 deletions
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"); } |