diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | gen/src/gleam@string.erl | 8 | ||||
-rw-r--r-- | gen/test/gleam@string_test.erl | 14 | ||||
-rw-r--r-- | src/gleam/string.gleam | 42 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 12 |
5 files changed, 57 insertions, 20 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a246bfa..a7c2414 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- The `string` module gains `trim_left` and `trim_right` functions. - The `string` module gains the `trim` function. ## v0.8.0 - 2020-04-28 diff --git a/gen/src/gleam@string.erl b/gen/src/gleam@string.erl index 8312797..c0557fd 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, trim/1]). +-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, trim_left/1, trim_right/1]). is_empty(Str) -> Str =:= <<""/utf8>>. @@ -68,3 +68,9 @@ erl_trim(A, B) -> trim(String) -> erl_trim(String, both). + +trim_left(String) -> + erl_trim(String, leading). + +trim_right(String) -> + erl_trim(String, trailing). diff --git a/gen/test/gleam@string_test.erl b/gen/test/gleam@string_test.erl index b1bc7b0..07f3f9b 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, trim_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, trim_left_test/0, trim_right_test/0]). length_test() -> gleam@should:equal(gleam@string:length(<<"ß↑e̊"/utf8>>), 3), @@ -104,3 +104,15 @@ trim_test() -> gleam@string:trim(<<" hats \n"/utf8>>), <<"hats"/utf8>> ). + +trim_left_test() -> + gleam@should:equal( + gleam@string:trim_left(<<" hats \n"/utf8>>), + <<"hats \n"/utf8>> + ). + +trim_right_test() -> + gleam@should:equal( + gleam@string:trim_right(<<" hats \n"/utf8>>), + <<" hats"/utf8>> + ). diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 1dd484a..2cad6f2 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -311,6 +311,8 @@ pub fn join(strings: List(String), with separator: String) -> String { // pub fn pad_right(string: String, to size: Int, with: String) {} type Direction { + Leading + Trailing Both } @@ -328,24 +330,28 @@ pub fn trim(string: String) -> String { erl_trim(string, Both) } -// TODO -// Get rid of whitespace on the left of a String. -// -// ## Examples -// > trim_left(" hats \n") -// "hats \n" -// -// -// pub fn trim_left(string: String) -> String {} -// TODO -// Get rid of whitespace on the right of a String. -// -// ## Examples -// > trim_right(" hats \n") -// " hats" -// -// -// pub fn trim_right(string: String) -> String {} +/// Get rid of whitespace on the left of a String. +/// +/// ## Examples +/// > trim_left(" hats \n") +/// "hats \n" +/// +/// +pub fn trim_left(string: String) -> String { + erl_trim(string, Leading) +} + +/// Get rid of whitespace on the right of a String. +/// +/// ## Examples +/// > trim_right(" hats \n") +/// " hats" +/// +/// +pub fn trim_right(string: String) -> String { + erl_trim(string, Trailing) +} + // TODO // /// Convert a string to a list of Graphemes. // /// diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index 9e1b0b1..01e95eb 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -115,3 +115,15 @@ pub fn trim_test() { |> string.trim() |> should.equal("hats") } + +pub fn trim_left_test() { + " hats \n" + |> string.trim_left() + |> should.equal("hats \n") +} + +pub fn trim_right_test() { + " hats \n" + |> string.trim_right() + |> should.equal(" hats") +} |