diff options
author | Erik Terpstra <erterpstra@gmail.com> | 2020-05-22 14:56:05 +0200 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-05-22 19:46:45 +0100 |
commit | c4470122c11d3b50234556f914927822455d47df (patch) | |
tree | 6a655bee54eafb5595379a4e512a830b8bedc2b3 | |
parent | 088ed8c9fcaaab0a434a29eb26175d4452f4b179 (diff) | |
download | gleam_stdlib-c4470122c11d3b50234556f914927822455d47df.tar.gz gleam_stdlib-c4470122c11d3b50234556f914927822455d47df.zip |
string.to_graphemes
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | src/gleam/string.gleam | 20 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 14 |
3 files changed, 28 insertions, 10 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e4d91dd..e424e0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,8 @@ - Created the `io` module with `print` function. - The `result` module gains the `nil_error` function. - The `string` module gains `trim`, `trim_left`, `trim_right`, `starts_with`, - `ends_with`, `slice`, `pad_left`, `pad_right` `drop_left`, `drop_right` and - `pop_grapheme` functions. + `ends_with`, `slice`, `pad_left`, `pad_right` `drop_left`, `drop_right`, + `pop_grapheme` and `to_graphemes' 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. diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index cf2a585..a6c7944 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -400,14 +400,6 @@ pub fn trim_right(string: String) -> String { erl_trim(string, Trailing) } -// TODO -// /// Convert a string to a list of Graphemes. -// /// -// /// > to_graphemes("abc") -// ['a','b','c'] -// -// /// -// pub fn to_graphemes(string: String) -> List(String) {} /// Split a non-empty string into its head and tail. This lets you /// pattern match on strings exactly as you would with lists. /// @@ -420,3 +412,15 @@ pub fn trim_right(string: String) -> String { /// pub external fn pop_grapheme(string: String) -> Option(tuple(String, String)) = "gleam_stdlib" "string_pop_grapheme" + +/// Convert a string to a list of Graphemes. +/// +/// > to_graphemes("abc") +/// ["a", "b", "c"] +/// +pub fn to_graphemes(string: String) -> List(String) { + case pop_grapheme(string) { + Ok(tuple(grapheme, rest)) -> [grapheme, ..to_graphemes(rest)] + _ -> [] + } +} diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index c8eae63..2fbb84f 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -267,3 +267,17 @@ pub fn pop_grapheme_test() { |> string.pop_grapheme() |> should.equal(Error(Nil)) } + +pub fn to_graphemes_test() { + "abc" + |> string.to_graphemes() + |> should.equal(["a", "b", "c"]) + + "a" + |> string.to_graphemes() + |> should.equal(["a"]) + + "" + |> string.to_graphemes() + |> should.equal([]) +} |