aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gleam/dynamic.gleam45
-rw-r--r--src/gleam_stdlib.js4
2 files changed, 31 insertions, 18 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam
index 5dd0feb..4cc71a4 100644
--- a/src/gleam/dynamic.gleam
+++ b/src/gleam/dynamic.gleam
@@ -5,9 +5,9 @@ import gleam/int
import gleam/option
import gleam/result
import gleam/string_builder
+import gleam/map.{Map}
if erlang {
- import gleam/map.{Map}
import gleam/option.{Option}
}
@@ -783,26 +783,35 @@ pub fn typed_tuple6(
Ok(#(a, b, c, d, e, f))
}
+/// Checks to see if the Dynamic value is map.
+///
+/// ## Examples
+///
+/// > import gleam/map
+/// > map(from(map.new()))
+/// Ok(map.new())
+///
+/// > map(from(1))
+/// Error(DecodeError(expected: "Map", found: "Int"))
+///
+/// > map(from(""))
+/// Error(DecodeError(expected: "Map", found: "String"))
+///
+pub fn map(from value: Dynamic) -> Result(Map(Dynamic, Dynamic), DecodeError) {
+ decode_map(value)
+}
+
if erlang {
- /// Checks to see if the Dynamic value is map.
- ///
- /// ## Examples
- ///
- /// > import gleam/map
- /// > map(from(map.new()))
- /// Ok(map.new())
- ///
- /// > map(from(1))
- /// Error(DecodeError(expected: "Map", found: "Int"))
- ///
- /// > map(from(""))
- /// Error(DecodeError(expected: "Map", found: "String"))
- ///
- pub external fn map(
- from: Dynamic,
- ) -> Result(Map(Dynamic, Dynamic), DecodeError) =
+ external fn decode_map(Dynamic) -> Result(Map(Dynamic, Dynamic), DecodeError) =
"gleam_stdlib" "decode_map"
+}
+if javascript {
+ external fn decode_map(Dynamic) -> Result(Map(Dynamic, Dynamic), DecodeError) =
+ "../gleam_stdlib.js" "decode_map"
+}
+
+if erlang {
/// Joins multiple decoders into one. When run they will each be tried in turn
/// until one succeeds, or they all fail.
///
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js
index 500066d..4a3026c 100644
--- a/src/gleam_stdlib.js
+++ b/src/gleam_stdlib.js
@@ -531,3 +531,7 @@ export function tuple_get(data, index) {
export function decode_list(data) {
return List.isList(data) ? new Ok(data) : decoder_error("List", data);
}
+
+export function decode_map(data) {
+ return data instanceof Map ? new Ok(data) : decoder_error("Map", data);
+}