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 | |
parent | c45c1038f8b12acf7d2010eba070ad3dc54765a2 (diff) | |
download | gleam_stdlib-014cf4a208b254d2172f318147edccc70fc1e49d.tar.gz gleam_stdlib-014cf4a208b254d2172f318147edccc70fc1e49d.zip |
JS dynamic int and float
-rw-r--r-- | src/gleam/dynamic.gleam | 70 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 8 | ||||
-rw-r--r-- | test/gleam/dynamic_test.gleam | 75 |
3 files changed, 101 insertions, 52 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); +} diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam index df92ffb..0d8dd09 100644 --- a/test/gleam/dynamic_test.gleam +++ b/test/gleam/dynamic_test.gleam @@ -67,51 +67,70 @@ if erlang { } } +pub fn int_test() { + 1 + |> dynamic.from + |> dynamic.int + |> should.equal(Ok(1)) + + 2 + |> dynamic.from + |> dynamic.int + |> should.equal(Ok(2)) + + [] + |> dynamic.from + |> dynamic.int + |> should.equal(Error(DecodeError(expected: "Int", found: "List"))) +} + +pub fn float_test() { + 1.0 + |> dynamic.from + |> dynamic.float + |> should.equal(Ok(1.0)) + + 2.2 + |> dynamic.from + |> dynamic.float + |> should.equal(Ok(2.2)) + + [] + |> dynamic.from + |> dynamic.float + |> should.equal(Error(DecodeError(expected: "Float", found: "List"))) +} + if erlang { - pub fn int_test() { + pub fn float_on_js_is_also_int_test() { 1 |> dynamic.from - |> dynamic.int - |> should.equal(Ok(1)) - - 2 - |> dynamic.from - |> dynamic.int - |> should.equal(Ok(2)) + |> dynamic.float + |> should.equal(Error(DecodeError(expected: "Float", found: "Int"))) 1.0 |> dynamic.from |> dynamic.int |> should.equal(Error(DecodeError(expected: "Int", found: "Float"))) - - [] - |> dynamic.from - |> dynamic.int - |> should.equal(Error(DecodeError(expected: "Int", found: "List"))) } +} - pub fn float_test() { - 1.0 - |> dynamic.from - |> dynamic.float - |> should.equal(Ok(1.0)) - - 2.2 - |> dynamic.from - |> dynamic.float - |> should.equal(Ok(2.2)) - +if javascript { + pub fn float_on_js_is_also_int_test() { 1 |> dynamic.from |> dynamic.float - |> should.equal(Error(DecodeError(expected: "Float", found: "Int"))) + |> should.equal(Ok(1.0)) - [] + 1.0 |> dynamic.from - |> dynamic.float - |> should.equal(Error(DecodeError(expected: "Float", found: "List"))) + |> dynamic.int + |> should.equal(Ok(1)) } +} +if erlang { + // TODO: remove pub fn thunk_test() { fn() { 1 } |> dynamic.from |