diff options
author | inoas <mail@inoas.com> | 2022-11-05 14:59:55 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-11-06 17:08:23 +0000 |
commit | 48f322d660a3e5fe85392c6a82b1147e53c9357a (patch) | |
tree | 19b285023c09e826dacec3ade989ba239ca03d45 | |
parent | c22ffd9244f2c6bbfb560b6d637eabca08414e99 (diff) | |
download | gleam_stdlib-48f322d660a3e5fe85392c6a82b1147e53c9357a.tar.gz gleam_stdlib-48f322d660a3e5fe85392c6a82b1147e53c9357a.zip |
`string.split` will now return a list of graphemes if split on an empty
string (`""`)
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/gleam/string.gleam | 12 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 6 |
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() { |