diff options
author | Louis Pilfold <louis@lpil.uk> | 2022-01-01 21:39:06 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-01-01 21:39:06 +0000 |
commit | 667d0ebf55dc1a114a912a241a89087e58691512 (patch) | |
tree | cec465ce98be8e74215190be4caa5d33de7d9706 | |
parent | 1348b7f94697910853e11b847f500ff868b21fd8 (diff) | |
download | gleam_stdlib-667d0ebf55dc1a114a912a241a89087e58691512.tar.gz gleam_stdlib-667d0ebf55dc1a114a912a241a89087e58691512.zip |
tuple2 is always typed
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/gleam/dynamic.gleam | 59 | ||||
-rw-r--r-- | test/gleam/dynamic_test.gleam | 38 |
3 files changed, 25 insertions, 74 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d186c98..a1dab9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - The `dynamic.typed_list` function has been renamed to `dynamic.list`. - The `dynamic.result` function has been removed. - The `dynamic.typed_result` function has been renamed to `dynamic.result`. +- The `dynamic.any` is now available on JavaScript and has correct information + in any errors returned. ## v0.18.1 - 2021-12-19 diff --git a/src/gleam/dynamic.gleam b/src/gleam/dynamic.gleam index e75f1ad..26e61ff 100644 --- a/src/gleam/dynamic.gleam +++ b/src/gleam/dynamic.gleam @@ -486,25 +486,33 @@ if javascript { "../gleam_stdlib.mjs" "length" } -/// Checks to see if a `Dynamic` value is a 2-element tuple. -/// -/// If you do not wish to decode all the elements in the tuple use the -/// `typed_tuple2` function instead. +/// Checks to see if a `Dynamic` value is a 2 element tuple containing +/// specifically typed elements. /// /// ## Examples /// -/// > tuple2(from(#(1, 2))) -/// Ok(#(from(1), from(2))) +/// > tuple2(from(#(1, 2)), int, int) +/// Ok(#(1, 2)) +/// +/// > tuple2(from(#(1, 2.0)), int, float) +/// Ok(#(1, 2.0)) /// -/// > tuple2(from(#(1, 2, 3))) +/// > tuple2(from(#(1, 2, 3)), int, float) /// Error(DecodeError(expected: "2 element tuple", found: "3 element tuple")) /// -/// > tuple2(from("")) +/// > tuple2(from(""), int, float) /// Error(DecodeError(expected: "2 element tuple", found: "String")) /// -pub fn tuple2(from value: Dynamic) -> Result(#(Dynamic, Dynamic), DecodeError) { +pub fn tuple2( + from value: Dynamic, + first decode_first: Decoder(a), + second decode_second: Decoder(b), +) -> Result(#(a, b), DecodeError) { try _ = assert_is_tuple(value, 2) - Ok(unsafe_coerce(value)) + let #(first, second) = unsafe_coerce(value) + try a = decode_first(first) + try b = decode_second(second) + Ok(#(a, b)) } fn assert_is_tuple( @@ -534,37 +542,6 @@ fn put_expected( } } -/// Checks to see if a `Dynamic` value is a 2 element tuple containing -/// specifically typed elements. -/// -/// If you wish to decode all the elements in the list use the `typed_tuple2` -/// instead. -/// -/// ## Examples -/// -/// > typed_tuple2(from(#(1, 2)), int, int) -/// Ok(#(1, 2)) -/// -/// > typed_tuple2(from(#(1, 2.0)), int, float) -/// Ok(#(1, 2.0)) -/// -/// > typed_tuple2(from(#(1, 2, 3)), int, float) -/// Error(DecodeError(expected: "2 element tuple", found: "3 element tuple")) -/// -/// > typed_tuple2(from(""), int, float) -/// Error(DecodeError(expected: "2 element tuple", found: "String")) -/// -pub fn typed_tuple2( - from tup: Dynamic, - first decode_first: Decoder(a), - second decode_second: Decoder(b), -) -> Result(#(a, b), DecodeError) { - try #(first, second) = tuple2(tup) - try a = decode_first(first) - try b = decode_second(second) - Ok(#(a, b)) -} - /// Checks to see if the `Dynamic` value is a 3-element tuple. /// /// If you do not wish to decode all the elements in the tuple use the diff --git a/test/gleam/dynamic_test.gleam b/test/gleam/dynamic_test.gleam index 8529b9a..3a1504c 100644 --- a/test/gleam/dynamic_test.gleam +++ b/test/gleam/dynamic_test.gleam @@ -326,50 +326,22 @@ pub fn element_test() { pub fn tuple2_test() { #(1, 2) |> dynamic.from - |> dynamic.tuple2 - |> should.equal(Ok(#(dynamic.from(1), dynamic.from(2)))) - - #(1, "") - |> dynamic.from - |> dynamic.tuple2 - |> should.equal(Ok(#(dynamic.from(1), dynamic.from("")))) - - #(1, 2, 3) - |> dynamic.from - |> dynamic.tuple2 - |> should.equal(Error(DecodeError( - expected: "Tuple of 2 elements", - found: "Tuple of 3 elements", - ))) - - 1 - |> dynamic.from - |> dynamic.tuple2 - |> should.equal(Error(DecodeError( - expected: "Tuple of 2 elements", - found: "Int", - ))) -} - -pub fn typed_tuple2_test() { - #(1, 2) - |> dynamic.from - |> dynamic.typed_tuple2(dynamic.int, dynamic.int) + |> dynamic.tuple2(dynamic.int, dynamic.int) |> should.equal(Ok(#(1, 2))) #(1, "") |> dynamic.from - |> dynamic.typed_tuple2(dynamic.int, dynamic.string) + |> dynamic.tuple2(dynamic.int, dynamic.string) |> should.equal(Ok(#(1, ""))) #(1, "") |> dynamic.from - |> dynamic.typed_tuple2(dynamic.int, dynamic.int) + |> dynamic.tuple2(dynamic.int, dynamic.int) |> should.equal(Error(DecodeError(expected: "Int", found: "String"))) #(1, 2, 3) |> dynamic.from - |> dynamic.typed_tuple2(dynamic.int, dynamic.int) + |> dynamic.tuple2(dynamic.int, dynamic.int) |> should.equal(Error(DecodeError( expected: "Tuple of 2 elements", found: "Tuple of 3 elements", @@ -377,7 +349,7 @@ pub fn typed_tuple2_test() { 1 |> dynamic.from - |> dynamic.typed_tuple2(dynamic.int, dynamic.int) + |> dynamic.tuple2(dynamic.int, dynamic.int) |> should.equal(Error(DecodeError( expected: "Tuple of 2 elements", found: "Int", |