Browse Source

Added ToKebabCase string extension method

pull/1904/head
Halil İbrahim Kalkan 7 years ago
parent
commit
c1f56f03a2
  1. 19
      framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs
  2. 12
      framework/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs

19
framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs

@ -293,6 +293,25 @@ namespace System
: Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + " " + char.ToLowerInvariant(m.Value[1]));
}
/// <summary>
/// Converts given PascalCase/camelCase string to kebab-case.
/// </summary>
/// <param name="str">String to convert.</param>
/// <param name="useCurrentCulture">set true to use current culture. Otherwise, invariant culture will be used.</param>
public static string ToKebabCase(this string str, bool useCurrentCulture = false)
{
if (string.IsNullOrWhiteSpace(str))
{
return str;
}
str = str.ToCamelCase();
return useCurrentCulture
? Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + "-" + char.ToLower(m.Value[1]))
: Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + "-" + char.ToLowerInvariant(m.Value[1]));
}
/// <summary>
/// Converts string to enum value.
/// </summary>

12
framework/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs

@ -73,12 +73,24 @@ namespace System
"Istanbul".ToCamelCase().ShouldBe("istanbul");
}
[Fact]
public void ToKebabCase_Test()
{
(null as string).ToKebabCase().ShouldBe(null);
"helloMoon".ToKebabCase().ShouldBe("hello-moon");
"HelloWorld".ToKebabCase().ShouldBe("hello-world");
"HelloIsparta".ToKebabCase().ShouldBe("hello-isparta");
"ThisIsSampleText".ToKebabCase().ShouldBe("this-is-sample-text");
}
[Fact]
public void ToSentenceCase_Test()
{
(null as string).ToSentenceCase().ShouldBe(null);
"HelloWorld".ToSentenceCase().ShouldBe("Hello world");
"HelloIsparta".ToSentenceCase().ShouldBe("Hello isparta");
"ThisIsSampleSentence".ToSentenceCase().ShouldBe("This is sample sentence");
"thisIsSampleSentence".ToSentenceCase().ShouldBe("this is sample sentence");
}
[Fact]

Loading…
Cancel
Save