diff options
author | Louis Pilfold <louis@lpil.uk> | 2022-01-01 16:21:34 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-01-01 16:21:34 +0000 |
commit | 9f723ceaa9264e70ca6238282294ba2cf2e3e739 (patch) | |
tree | 8f271790aa9a4bec72e45756d813922a93494d1f | |
parent | a4dcfaba11a73fe38595eedfc7a1ce542cdc17d9 (diff) | |
download | gleam_json-9f723ceaa9264e70ca6238282294ba2cf2e3e739.tar.gz gleam_json-9f723ceaa9264e70ca6238282294ba2cf2e3e739.zip |
Array functions
-rw-r--r-- | src/gleam/json.gleam | 19 | ||||
-rw-r--r-- | test/gleam_json_test.gleam | 14 |
2 files changed, 21 insertions, 12 deletions
diff --git a/src/gleam/json.gleam b/src/gleam/json.gleam index dcaf02e..5e44f71 100644 --- a/src/gleam/json.gleam +++ b/src/gleam/json.gleam @@ -1,4 +1,5 @@ import gleam/map +import gleam/list import gleam/result import gleam/option.{None, Option, Some} import gleam/dynamic.{Dynamic} @@ -37,17 +38,13 @@ pub external fn bool(input: Bool) -> Json = pub external fn int(input: Int) -> Json = "thoas_encode" "integer" -type Null { - Null -} - // TODO: document // TODO: test pub external fn null() -> Json = "thoas_encode" "null" // TODO: document -pub fn nullable(input: Option(a), the inner_type: fn(a) -> Json) -> Json { +pub fn nullable(from input: Option(a), of inner_type: fn(a) -> Json) -> Json { case input { Some(value) -> inner_type(value) None -> null() @@ -61,7 +58,13 @@ pub external fn object(entries: List(#(String, Json))) -> Json = // TODO: document // TODO: test -// TODO: rename to array -// TODO: make into a mapping function? -pub external fn list(entries: List(Json)) -> Json = +pub fn array(from entries: List(a), of inner_type: fn(a) -> Json) -> Json { + entries + |> list.map(inner_type) + |> preprocessed_array +} + +// TODO: document +// TODO: test +pub external fn preprocessed_array(from: List(Json)) -> Json = "thoas_encode" "non_recursive_array" diff --git a/test/gleam_json_test.gleam b/test/gleam_json_test.gleam index dae51b6..8fe256d 100644 --- a/test/gleam_json_test.gleam +++ b/test/gleam_json_test.gleam @@ -41,18 +41,24 @@ pub fn encode_object_test() { |> should_encode("{\"foo\":5}") } -pub fn encode_list_test() { - json.list([json.int(5), json.int(6)]) +pub fn encode_array_test() { + [5, 6, 1, 4] + |> json.array(of: json.int) + |> should_encode("[5,6,1,4]") +} + +pub fn encode_preprocessed_array_test() { + json.preprocessed_array([json.int(5), json.int(6)]) |> should_encode("[5,6]") } pub fn encode_nullable_some_test() { - json.nullable(Some(5), the: json.int) + json.nullable(Some(5), of: json.int) |> should_encode("5") } pub fn encode_nullable_none_test() { - json.nullable(None, the: json.int) + json.nullable(None, of: json.int) |> should_encode("null") } |