diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-08-06 14:11:26 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-08-06 14:11:26 +0100 |
commit | 91c0357c6ca3092525b798ad7dfee79fc308b2b9 (patch) | |
tree | f2060d1e2b44836ded8e312f973ff96477cbcd1c | |
parent | 4bc5ec9bcca768c52b07b7398b385a574d0a6b15 (diff) | |
download | gleam_stdlib-91c0357c6ca3092525b798ad7dfee79fc308b2b9.tar.gz gleam_stdlib-91c0357c6ca3092525b798ad7dfee79fc308b2b9.zip |
Fix JS versions hidden by eq error
-rw-r--r-- | src/gleam/float.gleam | 27 | ||||
-rw-r--r-- | src/gleam/list.gleam | 64 | ||||
-rw-r--r-- | src/gleam/should.gleam | 8 | ||||
-rw-r--r-- | src/gleam/string_builder.gleam | 2 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 13 | ||||
-rw-r--r-- | test/gleam/float_test.gleam | 20 | ||||
-rw-r--r-- | test/gleam/list_test.gleam | 43 | ||||
-rw-r--r-- | test/gleam/queue_test.gleam | 11 |
8 files changed, 121 insertions, 67 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index b3f0df2..8fdb79e 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -1,8 +1,5 @@ import gleam/order.{Order} - -if erlang { - import gleam/string_builder -} +import gleam/string_builder /// Attempts to parse a string as a float, returning `Error(Nil)` if it was not /// possible. @@ -28,18 +25,16 @@ if javascript { "../gleam_stdlib.js" "parse_float" } -if erlang { - /// Returns the string representation of the provided float. - /// - /// ## Examples - /// > to_string(2.3) - /// "2.3" - /// - pub fn to_string(f: Float) -> String { - f - |> string_builder.from_float - |> string_builder.to_string - } +/// Returns the string representation of the provided float. +/// +/// ## Examples +/// > to_string(2.3) +/// "2.3" +/// +pub fn to_string(f: Float) -> String { + f + |> string_builder.from_float + |> string_builder.to_string } /// Restricts a Float between a lower and upper bound diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 341b4a1..414f64e 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -480,10 +480,10 @@ if javascript { do_append_acc(reverse(first), second) } - fn do_append_acc(remaining: List(a), second: List(a)) -> List(a) { - case remaining { + fn do_append_acc(first: List(a), second: List(a)) -> List(a) { + case first { [] -> second - [item, ..rest] -> do_append(rest, [item, ..second]) + [item, ..rest] -> do_append_acc(rest, [item, ..second]) } } } @@ -1377,37 +1377,41 @@ pub fn take_while( do_take_while(list, predicate, []) } -fn do_chunk( - list: List(a), - f: fn(a) -> key, - previous_key: key, - current_chunk: List(a), - acc: List(List(a)), -) -> List(List(a)) { - case list { - [] -> reverse([reverse(current_chunk), ..acc]) - [head, ..tail] -> { - let key = f(head) - case key == previous_key { - False -> do_chunk(tail, f, key, [head], [reverse(current_chunk), ..acc]) - True -> do_chunk(tail, f, key, [head, ..current_chunk], acc) +if erlang { + // TODO: JavaScript fix + fn do_chunk( + list: List(a), + f: fn(a) -> key, + previous_key: key, + current_chunk: List(a), + acc: List(List(a)), + ) -> List(List(a)) { + case list { + [] -> reverse([reverse(current_chunk), ..acc]) + [head, ..tail] -> { + let key = f(head) + case key == previous_key { + False -> + do_chunk(tail, f, key, [head], [reverse(current_chunk), ..acc]) + True -> do_chunk(tail, f, key, [head, ..current_chunk], acc) + } } } } -} -/// Returns a list of chunks in which -/// the result of calling `f` on each element is the same. -/// -/// ## Examples -/// -/// > [1, 2, 2, 3, 4, 4, 6, 7, 7] |> chunk(by: fn(n) { n % 2 }) -/// [[1], [2, 2], [3], [4, 4, 6], [7, 7]] -/// -pub fn chunk(in list: List(a), by f: fn(a) -> key) -> List(List(a)) { - case list { - [] -> [] - [head, ..tail] -> do_chunk(tail, f, f(head), [head], []) + /// Returns a list of chunks in which + /// the result of calling `f` on each element is the same. + /// + /// ## Examples + /// + /// > [1, 2, 2, 3, 4, 4, 6, 7, 7] |> chunk(by: fn(n) { n % 2 }) + /// [[1], [2, 2], [3], [4, 4, 6], [7, 7]] + /// + pub fn chunk(in list: List(a), by f: fn(a) -> key) -> List(List(a)) { + case list { + [] -> [] + [head, ..tail] -> do_chunk(tail, f, f(head), [head], []) + } } } diff --git a/src/gleam/should.gleam b/src/gleam/should.gleam index c6c8992..3566dc2 100644 --- a/src/gleam/should.gleam +++ b/src/gleam/should.gleam @@ -35,9 +35,9 @@ if javascript { _ -> crash(string.concat([ "\n", - stringify(b), - "\nshould equal \n", stringify(a), + "\nshould equal \n", + stringify(b), "\n", ])) } @@ -49,9 +49,9 @@ if javascript { _ -> crash(string.concat([ "\n", - stringify(b), - "\nshould not equal \n", stringify(a), + "\nshould not equal \n", + stringify(b), ])) } } diff --git a/src/gleam/string_builder.gleam b/src/gleam/string_builder.gleam index ac6817f..0d464ca 100644 --- a/src/gleam/string_builder.gleam +++ b/src/gleam/string_builder.gleam @@ -164,7 +164,7 @@ if erlang { if javascript { external fn do_from_float(Float) -> StringBuilder = - "../gleam_stdlib.js" "to_string" + "../gleam_stdlib.js" "float_to_string" } /// Converts a builder to a new builder where the contents have been diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index a60b361..95fb526 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -36,8 +36,17 @@ export function parse_float(value) { } } -export function to_string(int) { - return int.toString(); +export function to_string(term) { + return term.toString(); +} + +export function float_to_string(float) { + let string = float.toString(); + if (string.indexOf(".") >= 0) { + return string; + } else { + return string + ".0"; + } } export function int_to_base_string(int, base) { diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 774f448..9eff3a3 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -16,7 +16,7 @@ pub fn parse_test() { "-1.23" |> float.parse - |> should.equal(Ok(1.23)) + |> should.equal(Ok(-1.23)) "5.0" |> float.parse @@ -39,17 +39,17 @@ pub fn parse_test() { |> should.equal(Error(Nil)) } -if erlang { - pub fn to_string_test() { - 123.0 - |> float.to_string - |> should.equal("123.0") +pub fn to_string_test() { + 123.0 + |> float.to_string + |> should.equal("123.0") - -8.1 - |> float.to_string - |> should.equal("-8.1") - } + -8.1 + |> float.to_string + |> should.equal("-8.1") +} +if erlang { pub fn clamp_test() { float.clamp(1.4, min: 1.3, max: 1.5) |> should.equal(1.4) diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam index ee33e88..4c45d6e 100644 --- a/test/gleam/list_test.gleam +++ b/test/gleam/list_test.gleam @@ -22,6 +22,13 @@ pub fn length_test() { pub fn reverse_test() { list.reverse([]) |> should.equal([]) + + list.reverse([1]) + |> should.equal([1]) + + list.reverse([1, 2]) + |> should.equal([2, 1]) + list.reverse([1, 2, 3, 4, 5]) |> should.equal([5, 4, 3, 2, 1]) } @@ -149,6 +156,27 @@ pub fn new_test() { pub fn append_test() { list.append([1], [2, 3]) |> should.equal([1, 2, 3]) + + list.append([1, 2], []) + |> should.equal([1, 2]) + + list.append([], [1, 2]) + |> should.equal([1, 2]) + + list.append([1, 2], [3, 4]) + |> should.equal([1, 2, 3, 4]) + + list.append([1, 2, 3], []) + |> should.equal([1, 2, 3]) + + list.append([1, 2, 3], [4]) + |> should.equal([1, 2, 3, 4]) + + list.append([1, 2, 3, 4], [5]) + |> should.equal([1, 2, 3, 4, 5]) + + list.append([], []) + |> should.equal([]) } pub fn flatten_test() { @@ -612,10 +640,17 @@ pub fn take_while_test() { |> should.equal([1, 2]) } -pub fn chunk_test() { - [1, 2, 2, 3, 4, 4, 6, 7, 7] - |> list.chunk(by: fn(n) { n % 2 }) - |> should.equal([[1], [2, 2], [3], [4, 4, 6], [7, 7]]) +if erlang { + // TODO: JavaScript fix + pub fn chunk_test() { + [1, 2, 3] + |> list.chunk(by: fn(n) { n % 2 }) + |> should.equal([[1], [2], [3]]) + + [1, 2, 2, 3, 4, 4, 6, 7, 7] + |> list.chunk(by: fn(n) { n % 2 }) + |> should.equal([[1], [2, 2], [3], [4, 4, 6], [7, 7]]) + } } pub fn sized_chunk_test() { diff --git a/test/gleam/queue_test.gleam b/test/gleam/queue_test.gleam index ae5dfeb..511b828 100644 --- a/test/gleam/queue_test.gleam +++ b/test/gleam/queue_test.gleam @@ -3,11 +3,22 @@ import gleam/int import gleam/list import gleam/should import gleam/pair +import gleam/io pub fn from_and_to_list_test() { queue.from_list([]) |> should.equal(queue.new()) + [1] + |> queue.from_list + |> queue.to_list + |> should.equal([1]) + + [1, 2] + |> queue.from_list + |> queue.to_list + |> should.equal([1, 2]) + [1, 2, 3] |> queue.from_list |> queue.to_list |