diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | src/gleam/string.gleam | 14 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 21 |
3 files changed, 36 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index dfeb517..bbfecf8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - The `iterator` module gains the `index`, `iterate`, `zip`, `scan`, `take_while`, `drop_while`, `chunk`, `sized_chunk`, `intersperse`, `any` and `all` functions. - Breaking change in `iterator.take`. Now it returns an iterator instead of a list. +- The `string` module gains the `drop_before` function. ## v0.14.0 - 2021-02-18 diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 7f7653c..1bec6a5 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -167,6 +167,20 @@ pub fn slice(from string: String, at_index idx: Int, length len: Int) -> String } } +/// Drops contents of the first string that occur before the second string. +/// If the first string does not contain the second string, the first string is returned. +/// +/// ## Examples +/// > drop_before(from: "The Lone Gunmen", before: "Lone") +/// "Lone Gunmen" +/// +pub fn drop_before(from string: String, before substring: String) -> String { + case split_once(string, substring) { + Ok(tuple(_, rest)) -> concat([substring, rest]) + Error(Nil) -> string + } +} + /// Drops *n* Graphemes from the left side of a string. /// /// ## Examples diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index 2d563b4..b0e0fe6 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -204,6 +204,27 @@ pub fn slice_test() { |> should.equal("") } +pub fn drop_before_test() { + "gleam" + |> string.drop_before("gl") + |> should.equal("gleam") + + "gleam" + |> string.drop_before("le") + |> should.equal("leam") + + string.drop_before(from: "gleam", before: "ea") + |> should.equal("eam") + + "gleam" + |> string.drop_before("") + |> should.equal("gleam") + + "gleam" + |> string.drop_before("!") + |> should.equal("gleam") +} + pub fn drop_left_test() { "gleam" |> string.drop_left(up_to: 2) |