diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-09-09 19:52:22 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-09-09 19:52:22 +0100 |
commit | 014cf4a208b254d2172f318147edccc70fc1e49d (patch) | |
tree | b9bb3d9d0b07447b3f9f9623d657e35065748202 /src | |
parent | c45c1038f8b12acf7d2010eba070ad3dc54765a2 (diff) | |
download | gleam_stdlib-014cf4a208b254d2172f318147edccc70fc1e49d.tar.gz gleam_stdlib-014cf4a208b254d2172f318147edccc70fc1e49d.zip |
JS dynamic int and float
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/dynamic.gleam | 70 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 8 |
2 files changed, 54 insertions, 24 deletions
diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index ccc621e..afea004 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -109,35 +109,57 @@ if javascript { "../gleam_stdlib.js" "decode_string" } +/// Checks to see whether a Dynamic value is an int, and return the int if it +/// is. +/// +/// ## Examples +/// +/// > int(from(123)) +/// Ok(123) +/// +/// > int(from("Hello")) +/// Error(DecodeError(expected: "Int", found: "String")) +/// +pub fn int(from data: Dynamic) -> Result(Int, DecodeError) { + decode_int(data) +} + if erlang { - /// Checks to see whether a Dynamic value is an int, and return the int if it - /// is. - /// - /// ## Examples - /// - /// > int(from(123)) - /// Ok(123) - /// - /// > int(from("Hello")) - /// Error(DecodeError(expected: "Int", found: "String")) - /// - pub external fn int(from: Dynamic) -> Result(Int, DecodeError) = + external fn decode_int(Dynamic) -> Result(Int, DecodeError) = "gleam_stdlib" "decode_int" +} - /// Checks to see whether a Dynamic value is an float, and return the float if - /// it is. - /// - /// ## Examples - /// - /// > float(from(2.0)) - /// Ok(2.0) - /// - /// > float(from(123)) - /// Error(DecodeError(expected: "Float", found: "Int")) - /// - pub external fn float(from: Dynamic) -> Result(Float, DecodeError) = +if javascript { + external fn decode_int(Dynamic) -> Result(Int, DecodeError) = + "../gleam_stdlib.js" "decode_int" +} + +/// Checks to see whether a Dynamic value is an float, and return the float if +/// it is. +/// +/// ## Examples +/// +/// > float(from(2.0)) +/// Ok(2.0) +/// +/// > float(from(123)) +/// Error(DecodeError(expected: "Float", found: "Int")) +/// +pub fn float(from data: Dynamic) -> Result(Float, DecodeError) { + decode_float(data) +} + +if erlang { + external fn decode_float(Dynamic) -> Result(Float, DecodeError) = "gleam_stdlib" "decode_float" +} +if javascript { + external fn decode_float(Dynamic) -> Result(Float, DecodeError) = + "../gleam_stdlib.js" "decode_float" +} + +if erlang { /// Checks to see whether a Dynamic value is an bool, and return the bool if /// it is. /// diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index 75b0882..cfe26a2 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -490,3 +490,11 @@ export function decode_string(data) { ? new Ok(data) : decoder_error("String", data); } + +export function decode_int(data) { + return Number.isInteger(data) ? new Ok(data) : decoder_error("Int", data); +} + +export function decode_float(data) { + return typeof data === "number" ? new Ok(data) : decoder_error("Float", data); +} |