aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md7
-rw-r--r--src/gleam/string.gleam61
-rw-r--r--test/gleam/string_test.gleam32
3 files changed, 72 insertions, 28 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8ed8c3b..43c84e0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,8 +4,9 @@
- Created the `io` module with `print` function.
- The `result` module gains the `nil_error` function.
-- The `string` module gains the `trim`, `trim_left`, `trim_right`, `starts_with`,
- `ends_with`, `slice`, `pad_left` and `pad_right` functions.
+- The `string` module gains `trim`, `trim_left`, `trim_right`, `starts_with`,
+ `ends_with`, `slice`, `pad_left`, `pad_right` `drop_left` and `drop_right`
+ functions.
- `uri` module created with `parse`, `parse_query`, `path_segments`,
`query_to_string` and `to_string`.
- The `dynamic` module gains the `map`, `opaque_list`, `tuple2`, and `tuple2_of` functions.
@@ -53,7 +54,7 @@
- The `pair.Pair` type has been replaced with a 2 element anonymous struct.
- The `triple` module has been removed.
- The `string` module gains the `compare` function.
-- The `float` module gains the `max`, and `min` functions.
+- The `float` module gains the `max`, and `min` functions.
- The `int` module gains the `max`, and `min` functions.
- The `Any` type and module have been renamed to `Dynamic`.
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index fc58329..25122b9 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -150,36 +150,47 @@ pub fn slice(
at_index idx: Int,
length len: Int,
) -> String {
- case idx < 0 {
- True -> {
- let translated_idx = length(string) + idx
- case translated_idx < 0 {
- True -> ""
- False -> erl_slice(string, translated_idx, len)
+ case len < 0 {
+ True -> ""
+ False -> case idx < 0 {
+ True -> {
+ let translated_idx = length(string) + idx
+ case translated_idx < 0 {
+ True -> ""
+ False -> erl_slice(string, translated_idx, len)
+ }
}
+ False -> erl_slice(string, idx, len)
}
- False -> erl_slice(string, idx, len)
}
}
-// TODO
-// Drop *n* Graphemes from the left side of a
-//
-// ## Examples
-// > drop_left(from: "The Lone Gunmen", up_to: 2)
-// "e Lone Gunmen"
-//
-//
-// pub fn drop_left(from string: String, up_to num_graphemes: Int) -> String {}
-// TODO
-// Drop *n* Graphemes from the right side of a
-//
-// ## Examples
-// > drop_right(from: "Cigarette Smoking Man", up_to: 2)
-// "Cigarette Smoking M"
-//
-//
-// pub fn drop_right(from string: String, up_to num_graphemes: Int) -> String {}
+/// Drop *n* Graphemes from the left side of a string.
+///
+/// ## Examples
+/// > drop_left(from: "The Lone Gunmen", up_to: 2)
+/// "e Lone Gunmen"
+///
+pub fn drop_left(from string: String, up_to num_graphemes: Int) -> String {
+ case num_graphemes < 0 {
+ True -> string
+ False -> slice(string, num_graphemes, length(string) - num_graphemes)
+ }
+}
+
+/// Drop *n* Graphemes from the right side of a string.
+///
+/// ## Examples
+/// > drop_right(from: "Cigarette Smoking Man", up_to: 2)
+/// "Cigarette Smoking M"
+///
+pub fn drop_right(from string: String, up_to num_graphemes: Int) -> String {
+ case num_graphemes < 0 {
+ True -> string
+ False -> slice(string, 0, length(string) - num_graphemes)
+ }
+}
+
external fn erl_contains(String, String) -> Dynamic =
"string" "find"
diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam
index 05c939c..0695749 100644
--- a/test/gleam/string_test.gleam
+++ b/test/gleam/string_test.gleam
@@ -184,6 +184,38 @@ pub fn slice_test() {
"gleam"
|> string.slice(at_index: -12, length: 2)
|> should.equal("")
+
+ "gleam"
+ |> string.slice(at_index: 2, length: -3)
+ |> should.equal("")
+}
+
+pub fn drop_left_test() {
+ "gleam"
+ |> string.drop_left(up_to: 2)
+ |> should.equal("eam")
+
+ "gleam"
+ |> string.drop_left(up_to: 6)
+ |> should.equal("")
+
+ "gleam"
+ |> string.drop_left(up_to: -2)
+ |> should.equal("gleam")
+}
+
+pub fn drop_right_test() {
+ "gleam"
+ |> string.drop_right(up_to: 2)
+ |> should.equal("gle")
+
+ "gleam"
+ |> string.drop_right(up_to: 5)
+ |> should.equal("")
+
+ "gleam"
+ |> string.drop_right(up_to: -2)
+ |> should.equal("gleam")
}
pub fn pad_left_test() {