diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/string.gleam | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index fc3071f..ac7d3aa 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -739,3 +739,26 @@ pub fn to_option(s: String) -> Option(String) { _ -> Some(s) } } + +/// Returns the first element in a grapheme and wraps it in an `Option(String)`. +/// If the `String` is empty, it returns `None`. Otherwise, it returns +/// `Some(String)`. +/// +/// ## Examples +/// +/// ```gleam +/// > first("") +/// None +/// ``` +/// +/// ```gleam +/// > first("icecream") +/// Some("i") +/// ``` +/// +pub fn first(s: String) -> Option(String) { + case length(s) { + 0 -> None + _ -> Some(slice(s, 0, 1)) + } +} |