aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDanny Martini <despair.blue@gmail.com>2023-04-29 15:23:27 +0200
committerGitHub <noreply@github.com>2023-04-29 14:23:27 +0100
commit8924398359342ad33e99d707a3c8a7fb44d99da0 (patch)
treeb87de09c589474d8d23226d07b740f0cc7da65c7 /src
parent13f8dc62e08fb499e600a4bcacfae7a29008dbfa (diff)
downloadgleam_stdlib-8924398359342ad33e99d707a3c8a7fb44d99da0.tar.gz
gleam_stdlib-8924398359342ad33e99d707a3c8a7fb44d99da0.zip
fix decode_map throwing when `null` is being passed in (#432)
Diffstat (limited to 'src')
-rw-r--r--src/gleam_stdlib.erl1
-rw-r--r--src/gleam_stdlib.mjs18
2 files changed, 15 insertions, 4 deletions
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));