aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gleam/string.gleam22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index 545d1e4..3f0ebe5 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -6,6 +6,7 @@ import gleam/iterator.{Iterator}
import gleam/list
import gleam/order
import gleam/result
+import gleam/option.{None, Option, Some}
if erlang {
import gleam/dynamic.{Dynamic}
@@ -664,3 +665,24 @@ pub fn utf_codepoint(value: Int) -> Result(UtfCodepoint, Nil) {
i -> Ok(unsafe_int_to_utf_codepoint(i))
}
}
+
+/// Convert a string into an optional string where an empty string becomes `None`.
+///
+/// ## Examples
+///
+/// ```gleam
+/// > to_option("")
+/// None
+/// ```
+///
+/// ```gleam
+/// > to_option("")
+/// None
+/// ```
+///
+pub fn to_option(s: String) -> Option(String) {
+ case s {
+ "" -> None
+ _ -> Some(s)
+ }
+}