aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Attard <robert.attard@mail.mcgill.ca>2021-04-08 14:38:23 -0400
committerLouis Pilfold <louis@lpil.uk>2021-04-09 10:43:35 +0100
commit593eeae98d21da64d0e6a956413d3874fe762369 (patch)
treeda24a4edc5b1003d36cd9678086ed29f3be60349
parentb9efa53231bfd3e4929208839f6fe913f4e1f69f (diff)
downloadgleam_stdlib-593eeae98d21da64d0e6a956413d3874fe762369.tar.gz
gleam_stdlib-593eeae98d21da64d0e6a956413d3874fe762369.zip
rename string.drop_before to string.crop; use erl_contains instead of split_once and then concat
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam/string.gleam12
-rw-r--r--test/gleam/string_test.gleam12
3 files changed, 13 insertions, 13 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index bbfecf8..663be69 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,7 +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.
+- The `string` module gains the `crop` function.
## v0.14.0 - 2021-02-18
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index 1bec6a5..8bef422 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -171,14 +171,14 @@ pub fn slice(from string: String, at_index idx: Int, length len: Int) -> 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")
+/// > crop(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
- }
+pub fn crop(from string: String, before substring: String) -> String {
+ string
+ |> erl_contains(substring)
+ |> dynamic.string()
+ |> result.unwrap(string)
}
/// Drops *n* Graphemes from the left side of a string.
diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam
index b0e0fe6..3b1db57 100644
--- a/test/gleam/string_test.gleam
+++ b/test/gleam/string_test.gleam
@@ -204,24 +204,24 @@ pub fn slice_test() {
|> should.equal("")
}
-pub fn drop_before_test() {
+pub fn crop_test() {
"gleam"
- |> string.drop_before("gl")
+ |> string.crop("gl")
|> should.equal("gleam")
"gleam"
- |> string.drop_before("le")
+ |> string.crop("le")
|> should.equal("leam")
- string.drop_before(from: "gleam", before: "ea")
+ string.crop(from: "gleam", before: "ea")
|> should.equal("eam")
"gleam"
- |> string.drop_before("")
+ |> string.crop("")
|> should.equal("gleam")
"gleam"
- |> string.drop_before("!")
+ |> string.crop("!")
|> should.equal("gleam")
}