aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-12-04 19:54:03 +0100
committerLouis Pilfold <louis@lpil.uk>2022-12-22 20:44:33 +0000
commit9cd78407f0402d7408dee4da7a5c405141579a32 (patch)
tree4e456d4b2ec17ce972b4261c7f64073e01a067af
parent663729b8a619d1076fda8953abdb06070a786269 (diff)
downloadgleam_stdlib-9cd78407f0402d7408dee4da7a5c405141579a32.tar.gz
gleam_stdlib-9cd78407f0402d7408dee4da7a5c405141579a32.zip
tests
-rw-r--r--src/gleam/string.gleam21
-rw-r--r--test/gleam/string_test.gleam17
2 files changed, 38 insertions, 0 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index 5369b32..e2377e2 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -900,6 +900,27 @@ pub fn utf_codepoint(value: Int) -> Result(UtfCodepoint, Nil) {
}
}
+/// Converts a string to a list of UtfCodepoint integers.
+///
+/// ## Examples
+///
+/// ```gleam
+/// > "abc"
+/// > |> string.to_ints
+/// [97, 98, 99]
+/// ```
+///
+/// ```gleam
+/// > "🐍"
+/// > |> string.to_ints
+/// [128013]
+/// ```
+///
+/// ```gleam
+/// > [utf_codepoint(128013)] |> from_utf_codepoints |> to_ints
+/// "🐍"
+/// ```
+///
pub fn to_ints(s: String) -> List(Int) {
to_utf_codepoints(s)
|> list.map(utf_codepoint_to_int)
diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam
index 37671d3..5aad968 100644
--- a/test/gleam/string_test.gleam
+++ b/test/gleam/string_test.gleam
@@ -511,6 +511,10 @@ pub fn bit_string_utf_codepoint_test() {
}
pub fn to_ints_test() {
+ ""
+ |> string.to_ints
+ |> should.equal([])
+
"abc"
|> string.to_ints
|> should.equal([97, 98, 99])
@@ -518,6 +522,19 @@ pub fn to_ints_test() {
"🐍"
|> string.to_ints
|> should.equal([128013])
+
+ {
+ assert Ok(cp) = string.utf_codepoint(128013)
+ [cp]
+ }
+ |> string.from_utf_codepoints
+ |> string.to_ints
+ |> should.equal([128013])
+
+ string.to_utf_codepoints("🐍")
+ |> string.from_utf_codepoints
+ |> string.to_ints
+ "🐍"
}
pub fn to_option_test() {