Browse Source

Closes #162

pull/164/head
Sebastian Stehle 9 years ago
parent
commit
1c3bd4174f
  1. 26
      src/Squidex.Infrastructure/StringExtensions.cs
  2. 16
      tests/Squidex.Infrastructure.Tests/StringExtensionsTests.cs

26
src/Squidex.Infrastructure/StringExtensions.cs

@ -326,14 +326,36 @@ namespace Squidex.Infrastructure
public static string ToPascalCase(this string value)
{
return string.Concat(value.Split(new[] { '-', '_', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(c => char.ToUpper(c[0]) + c.Substring(1)));
var sb = new StringBuilder();
foreach (var part in value.Split(new[] { '-', '_', ' ' }))
{
if (part.Length < 2)
{
sb.Append(part.ToUpper());
}
else
{
sb.Append(char.ToUpper(part[0]));
sb.Append(part.Substring(1));
}
}
return sb.ToString();
}
public static string ToCamelCase(this string value)
{
value = value.ToPascalCase();
return char.ToLower(value[0]) + value.Substring(1);
if (value.Length < 2)
{
return value.ToLower();
}
else
{
return char.ToLower(value[0]) + value.Substring(1);
}
}
public static string Simplify(this string value, ISet<char> preserveHash = null, bool singleCharDiactric = false, char separator = '-')

16
tests/Squidex.Infrastructure.Tests/StringExtensionsTests.cs

@ -23,6 +23,9 @@ namespace Squidex.Infrastructure
}
[Theory]
[InlineData("", "")]
[InlineData("m", "M")]
[InlineData("m-y", "MY")]
[InlineData("my", "My")]
[InlineData("myProperty ", "MyProperty")]
[InlineData("my property", "MyProperty")]
@ -34,7 +37,10 @@ namespace Squidex.Infrastructure
}
[Theory]
[InlineData("", "")]
[InlineData("M", "m")]
[InlineData("My", "my")]
[InlineData("M-y", "mY")]
[InlineData("MyProperty ", "myProperty")]
[InlineData("My property", "myProperty")]
[InlineData("My_property", "myProperty")]
@ -105,9 +111,9 @@ namespace Squidex.Infrastructure
}
[Theory]
[InlineData("http://squidex.io/base/", "path/to/res", false, "http://squidex.io/base/path/to/res")]
[InlineData("http://squidex.io/base/", "path/to/res", true, "http://squidex.io/base/path/to/res/")]
[InlineData("http://squidex.io/base/", "/path/to/res", true, "http://squidex.io/base/path/to/res/")]
[InlineData("http://squidex.io/base/", "path/to/res", false, "http://squidex.io/base/path/to/res")]
[InlineData("http://squidex.io/base/", "path/to/res", true, "http://squidex.io/base/path/to/res/")]
[InlineData("http://squidex.io/base/", "/path/to/res", true, "http://squidex.io/base/path/to/res/")]
public void Should_provide_full_url_without_query_or_fragment(string baseUrl, string path, bool trailingSlash, string output)
{
var result = baseUrl.BuildFullUrl(path, trailingSlash);
@ -117,8 +123,8 @@ namespace Squidex.Infrastructure
[Theory]
[InlineData("http://squidex.io/base/", "path/to/res?query=1", false, "http://squidex.io/base/path/to/res?query=1")]
[InlineData("http://squidex.io/base/", "path/to/res#query=1", true, "http://squidex.io/base/path/to/res#query=1")]
[InlineData("http://squidex.io/base/", "path/to/res;query=1", true, "http://squidex.io/base/path/to/res;query=1")]
[InlineData("http://squidex.io/base/", "path/to/res#query=1", true, "http://squidex.io/base/path/to/res#query=1")]
[InlineData("http://squidex.io/base/", "path/to/res;query=1", true, "http://squidex.io/base/path/to/res;query=1")]
public void Should_provide_full_url_wit_query_or_fragment(string baseUrl, string path, bool trailingSlash, string output)
{
var result = baseUrl.BuildFullUrl(path, trailingSlash);

Loading…
Cancel
Save