From 8924398359342ad33e99d707a3c8a7fb44d99da0 Mon Sep 17 00:00:00 2001 From: Danny Martini Date: Sat, 29 Apr 2023 15:23:27 +0200 Subject: fix decode_map throwing when `null` is being passed in (#432) --- src/gleam_stdlib.erl | 1 + src/gleam_stdlib.mjs | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index e1fdbb1..33c9609 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -41,6 +41,7 @@ decode_error_msg(Expected, Data) when is_binary(Expected) -> decode_error(Expected, Got) when is_binary(Expected) andalso is_binary(Got) -> {error, [{decode_error, Expected, Got, []}]}. +classify_dynamic(nil) -> <<"Nil">>; classify_dynamic(X) when is_atom(X) -> <<"Atom">>; classify_dynamic(X) when is_binary(X) -> <<"String">>; classify_dynamic(X) when is_bitstring(X) -> <<"BitString">>; diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 632c668..2b0a6e1 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -392,11 +392,11 @@ export function regex_scan(regex, string) { const submatches = []; for (let n = match.length - 1; n > 0; n--) { if (match[n]) { - submatches[n-1] = new Some(match[n]) - continue + submatches[n - 1] = new Some(match[n]); + continue; } - if(submatches.length > 0) { - submatches[n-1] = new None() + if (submatches.length > 0) { + submatches[n - 1] = new None(); } } return new RegexMatch(content, List.fromArray(submatches)); @@ -564,6 +564,10 @@ export function classify_dynamic(data) { return "Map"; } else if (typeof data === "number") { return "Float"; + } else if (data === null) { + return "Null"; + } else if (data === undefined) { + return "Nil"; } else { let type = typeof data; return type.charAt(0).toUpperCase() + type.slice(1); @@ -633,6 +637,12 @@ export function decode_map(data) { if (data instanceof PMap) { return new Ok(PMap.fromMap(data)); } + if (data == null) { + return decoder_error("Map", data); + } + if (typeof data !== "object") { + return decoder_error("Map", data); + } const proto = Object.getPrototypeOf(data); if (proto === Object.prototype || proto === null) { return new Ok(PMap.fromObject(data)); -- cgit v1.2.3