aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/gleam/string.gleam12
-rw-r--r--test/gleam/string_test.gleam6
3 files changed, 16 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4de7ae2..b688c23 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,8 @@
- The performance of `list.flatten` has been greatly improved.
- The `string_builder` module gains the `join` function.
- The `list` module gains the `shuffle` function.
+- `string.split` will now return a list of graphemes if split on an empty
+ string (`""`).
## v0.24.0 - 2022-10-15
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index 312b81d..598868a 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -410,10 +410,14 @@ if javascript {
/// ```
///
pub fn split(x: String, on substring: String) -> List(String) {
- x
- |> string_builder.from_string
- |> string_builder.split(on: substring)
- |> list.map(with: string_builder.to_string)
+ case substring {
+ "" -> to_graphemes(x)
+ _ ->
+ x
+ |> string_builder.from_string
+ |> string_builder.split(on: substring)
+ |> list.map(with: string_builder.to_string)
+ }
}
/// Splits a `String` a single time on the given substring.
diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam
index 13069f1..f330e6d 100644
--- a/test/gleam/string_test.gleam
+++ b/test/gleam/string_test.gleam
@@ -82,6 +82,12 @@ pub fn split_test() {
"Gleam, Erlang,Elixir"
|> string.split(", ")
|> should.equal(["Gleam", "Erlang,Elixir"])
+
+ "Gleam On Beam"
+ |> string.split("")
+ |> should.equal([
+ "G", "l", "e", "a", "m", " ", "O", "n", " ", "B", "e", "a", "m",
+ ])
}
pub fn split_once_test() {