From ea2c3dc22853332e58aa52ffd6a73aa8e1427141 Mon Sep 17 00:00:00 2001 From: Seethoven <34771525+ryanryanorient@users.noreply.github.com> Date: Thu, 22 Dec 2022 16:29:57 +0800 Subject: [PATCH] Update StringUtilities.cs Fix ToCamelCase --- .../Utilities/StringUtilities.cs | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/CSharpToTypeScript.Core/Utilities/StringUtilities.cs b/src/CSharpToTypeScript.Core/Utilities/StringUtilities.cs index 71f24e9..9bf43ae 100644 --- a/src/CSharpToTypeScript.Core/Utilities/StringUtilities.cs +++ b/src/CSharpToTypeScript.Core/Utilities/StringUtilities.cs @@ -31,9 +31,38 @@ public static string EscapeQuotes(this string text, QuotationMark quotationMark) }; public static string ToCamelCase(this string text) - => !string.IsNullOrEmpty(text) ? - Regex.Replace(text, "^[A-Z]", char.ToLowerInvariant(text[0]).ToString()) - : text; + { + if (string.IsNullOrEmpty(text) || !char.IsUpper(text[0])) + { + return text; + } + + char[] chars = text.ToCharArray(); + + for (int i = 0; i < chars.Length; i++) + { + if (i == 1 && !char.IsUpper(chars[i])) + { + break; + } + + bool hasNext = (i + 1 < chars.Length); + + if (i > 0 && hasNext && !char.IsUpper(chars[i + 1])) + { + if (chars[i + 1] == ' ') + { + chars[i] = char.ToLowerInvariant(chars[i]); + } + + break; + } + + chars[i] = char.ToLowerInvariant(chars[i]); + } + + return new string(chars); + } public static string ToKebabCase(this string text) => Regex.Replace(text, "(? NewLine.Repeat(2); } -} \ No newline at end of file +}