discord-bot/Clients/CompatApiClient/Formatters/NamingStyles.cs
2023-04-21 02:05:59 +05:00

50 lines
1.3 KiB
C#

using System;
using System.Text;
namespace CompatApiClient;
public static class NamingStyles
{
public static string CamelCase(string value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (value.Length > 0)
{
if (char.IsUpper(value[0]))
value = char.ToLower(value[0]) + value[1..];
}
return value;
}
public static string Dashed(string value) => Delimitied(value, '-');
public static string Underscore(string value) => Delimitied(value, '_');
private static string Delimitied(string value, char separator)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (value.Length == 0)
return value;
var hasPrefix = true;
var builder = new StringBuilder(value.Length + 3);
foreach (var c in value)
{
var ch = c;
if (char.IsUpper(ch))
{
ch = char.ToLower(ch);
if (!hasPrefix)
builder.Append(separator);
hasPrefix = true;
}
else
hasPrefix = false;
builder.Append(ch);
}
return builder.ToString();
}
}