diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-09-09 20:01:04 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-09-09 20:01:04 +0100 |
commit | 749ac8290d1e3d09e2287056f618d6df91f4e01b (patch) | |
tree | b88abc0b15c54708d376bf35c0b8b1575dcb7d0b /src | |
parent | ae269282095ba1180952dd1eab58830bf5a19ed2 (diff) | |
download | gleam_stdlib-749ac8290d1e3d09e2287056f618d6df91f4e01b.tar.gz gleam_stdlib-749ac8290d1e3d09e2287056f618d6df91f4e01b.zip |
JS dynamic bool
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/dynamic.gleam | 35 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 8 |
2 files changed, 30 insertions, 13 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index d4afca0..86d1b6e 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -159,21 +159,32 @@ if javascript { "../gleam_stdlib.js" "decode_float" } +/// Checks to see whether a Dynamic value is an bool, and return the bool if +/// it is. +/// +/// ## Examples +/// +/// > bool(from(True)) +/// Ok(True) +/// +/// > bool(from(123)) +/// Error(DecodeError(expected: "bool", found: "Int")) +/// +pub fn bool(from data: Dynamic) -> Result(Bool, DecodeError) { + decode_bool(data) +} + if erlang { - /// Checks to see whether a Dynamic value is an bool, and return the bool if - /// it is. - /// - /// ## Examples - /// - /// > bool(from(True)) - /// Ok(True) - /// - /// > bool(from(123)) - /// Error(DecodeError(expected: "bool", found: "Int")) - /// - pub external fn bool(from: Dynamic) -> Result(Bool, DecodeError) = + external fn decode_bool(Dynamic) -> Result(Bool, DecodeError) = "gleam_stdlib" "decode_bool" +} +if javascript { + external fn decode_bool(Dynamic) -> Result(Bool, DecodeError) = + "../gleam_stdlib.js" "decode_bool" +} + +if erlang { /// Checks to see whether a Dynamic value is a list, and return the list if it /// is. /// diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index cfe26a2..b8feecf 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -476,8 +476,11 @@ function classify_dynamic(data) { return "List"; } else if (Number.isInteger(data)) { return "Int"; + } else if (typeof data === "number") { + return "Float"; } else { - return typeof data; + let type = typeof data; + return type.charAt(0).toUpperCase() + type.slice(1); } } @@ -498,3 +501,6 @@ export function decode_int(data) { export function decode_float(data) { return typeof data === "number" ? new Ok(data) : decoder_error("Float", data); } +export function decode_bool(data) { + return typeof data === "boolean" ? new Ok(data) : decoder_error("Bool", data); +} |