From 397f64e5aa7dfb1c805015f5beb004df4e87c7c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Mon, 9 Sep 2024 18:40:50 +0200 Subject: [PATCH] Improve automatic downcasing of errors (#2777) --- lib/livebook/utils.ex | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/livebook/utils.ex b/lib/livebook/utils.ex index ddd942e25..2401124e7 100644 --- a/lib/livebook/utils.ex +++ b/lib/livebook/utils.ex @@ -320,6 +320,8 @@ defmodule Livebook.Utils do @doc """ Changes the first letter in the given string to lower case. + If the second letter is uppercase, the first letter case is kept. + ## Examples iex> Livebook.Utils.downcase_first("Sippin tea") @@ -328,6 +330,9 @@ defmodule Livebook.Utils do iex> Livebook.Utils.downcase_first("Short URL") "short URL" + iex> Livebook.Utils.downcase_first("URL invalid") + "URL invalid" + iex> Livebook.Utils.downcase_first("") "" @@ -335,7 +340,17 @@ defmodule Livebook.Utils do @spec downcase_first(String.t()) :: String.t() def downcase_first(string) do {first, rest} = String.split_at(string, 1) - String.downcase(first) <> rest + + should_downcase? = + if second = String.at(rest, 0) do + second == String.downcase(second) + end + + if should_downcase? do + String.downcase(first) <> rest + else + string + end end @doc """