diff options
-rw-r--r-- | gen/src/gleam@string.erl | 5 | ||||
-rw-r--r-- | gen/test/gleam@int_test.erl | 8 | ||||
-rw-r--r-- | gen/test/gleam@string_test.erl | 8 | ||||
-rw-r--r-- | src/gleam/string.gleam | 6 | ||||
-rw-r--r-- | src/gleam_stdlib.erl | 4 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 6 |
6 files changed, 28 insertions, 9 deletions
diff --git a/gen/src/gleam@string.erl b/gen/src/gleam@string.erl index b8ee4e6..d5488f1 100644 --- a/gen/src/gleam@string.erl +++ b/gen/src/gleam@string.erl @@ -1,7 +1,7 @@ -module(gleam@string). -compile(no_auto_import). --export([is_empty/1, length/1, reverse/1, replace/3, lowercase/1, uppercase/1, compare/2, contains/2, split/2, append/2, concat/1, repeat/2, join/2]). +-export([is_empty/1, length/1, reverse/1, replace/3, lowercase/1, uppercase/1, compare/2, contains/2, split/2, append/2, concat/1, repeat/2, join/2, trim/1]). is_empty(Str) -> Str =:= <<""/utf8>>. @@ -62,3 +62,6 @@ join(Strings, Separator) -> gleam@iodata:to_string( gleam@iodata:from_strings(gleam@list:intersperse(Strings, Separator)) ). + +trim(A) -> + gleam_stdlib:string_trim(A). diff --git a/gen/test/gleam@int_test.erl b/gen/test/gleam@int_test.erl index d1f17b0..a81c427 100644 --- a/gen/test/gleam@int_test.erl +++ b/gen/test/gleam@int_test.erl @@ -1,14 +1,14 @@ -module(gleam@int_test). -compile(no_auto_import). --export([to_string/0, parse/0, to_base_string/0, compare_test/0, min_test/0, max_test/0, is_even_test/0, is_odd_test/0]). +-export([to_string_test/0, parse_test/0, to_base_string_test/0, compare_test/0, min_test/0, max_test/0, is_even_test/0, is_odd_test/0]). -to_string() -> +to_string_test() -> gleam@should:equal(gleam@int:to_string(123), <<"123"/utf8>>), gleam@should:equal(gleam@int:to_string(-123), <<"-123"/utf8>>), gleam@should:equal(gleam@int:to_string(123), <<"123"/utf8>>). -parse() -> +parse_test() -> gleam@should:equal(gleam@int:parse(<<"123"/utf8>>), {ok, 123}), gleam@should:equal(gleam@int:parse(<<"-123"/utf8>>), {ok, -123}), gleam@should:equal(gleam@int:parse(<<"0123"/utf8>>), {ok, 123}), @@ -16,7 +16,7 @@ parse() -> gleam@should:equal(gleam@int:parse(<<"what"/utf8>>), {error, nil}), gleam@should:equal(gleam@int:parse(<<"1.23"/utf8>>), {error, nil}). -to_base_string() -> +to_base_string_test() -> gleam@should:equal(gleam@int:to_base_string(100, 16), <<"64"/utf8>>), gleam@should:equal(gleam@int:to_base_string(-100, 16), <<"-64"/utf8>>). diff --git a/gen/test/gleam@string_test.erl b/gen/test/gleam@string_test.erl index c6e423e..b1bc7b0 100644 --- a/gen/test/gleam@string_test.erl +++ b/gen/test/gleam@string_test.erl @@ -1,7 +1,7 @@ -module(gleam@string_test). -compile(no_auto_import). --export([length_test/0, lowercase_test/0, uppercase_test/0, reverse_test/0, split_test/0, replace_test/0, append_test/0, compare_test/0, contains_test/0, concat_test/0, repeat_test/0, join_test/0]). +-export([length_test/0, lowercase_test/0, uppercase_test/0, reverse_test/0, split_test/0, replace_test/0, append_test/0, compare_test/0, contains_test/0, concat_test/0, repeat_test/0, join_test/0, trim_test/0]). length_test() -> gleam@should:equal(gleam@string:length(<<"ß↑e̊"/utf8>>), 3), @@ -98,3 +98,9 @@ join_test() -> gleam@string:join([<<"Hello"/utf8>>, <<"world!"/utf8>>], <<"-"/utf8>>), <<"Hello-world!"/utf8>> ). + +trim_test() -> + gleam@should:equal( + gleam@string:trim(<<" hats \n"/utf8>>), + <<"hats"/utf8>> + ). diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 8c270bc..bde93e2 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -309,7 +309,7 @@ pub fn join(strings: List(String), with separator: String) -> String { // // // pub fn pad_right(string: String, to size: Int, with: String) {} -// TODO + // Get rid of whitespace on both sides of a String. // // ## Examples @@ -317,7 +317,9 @@ pub fn join(strings: List(String), with separator: String) -> String { // "hats" // // -// pub fn trim(string: String) -> String {} +pub external fn trim(String) -> String = + "gleam_stdlib" "string_trim" + // TODO // Get rid of whitespace on the left of a String. // diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index 49a7e2a..54fcdaa 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -7,7 +7,7 @@ iodata_append/2, iodata_prepend/2, identity/1, decode_int/1, decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1, decode_atom/1, decode_list/1, decode_field/2, decode_element/2, parse_int/1, parse_float/1, compare_strings/2, - string_contains/2]). + string_contains/2, string_trim/1]). should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual). should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual). @@ -127,3 +127,5 @@ string_contains(Haystack, Needle) -> _ -> true end. + +string_trim(String) -> string:trim(String, both). diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index 15ace70..9e1b0b1 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -109,3 +109,9 @@ pub fn join_test() { |> string.join(with: "-") |> should.equal("Hello-world!") } + +pub fn trim_test() { + " hats \n" + |> string.trim() + |> should.equal("hats") +} |