Skip to content

Commit

Permalink
Allow generating lowercase kebab with KebabCaseParameterTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSmoke committed Sep 18, 2024
1 parent 694a408 commit d69d634
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace ClickView.GoodStuff.AspNetCore.Routing;
/// <summary>
/// Parameter transformer which converts route parameters to kebab-case
/// </summary>
public class KebabCaseParameterTransformer : IOutboundParameterTransformer
public partial class KebabCaseParameterTransformer(bool lowercase = false) : IOutboundParameterTransformer
{
private static readonly Regex ReplacerRegex = new("([a-z])([A-Z])", RegexOptions.Compiled);
private static readonly Regex ReplacerRegex = KebabRegex();

/// <inheritdoc />
public string? TransformOutbound(object? value)
Expand All @@ -19,6 +19,12 @@ public class KebabCaseParameterTransformer : IOutboundParameterTransformer
return null;

// Slugify value
return ReplacerRegex.Replace(str, "$1-$2");
var kebabCase = ReplacerRegex.Replace(str, "$1-$2");

// ToLower?
return lowercase ? kebabCase.ToLower() : kebabCase;
}

[GeneratedRegex("([a-z])([A-Z])", RegexOptions.Compiled)]
private static partial Regex KebabRegex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,23 @@ namespace ClickView.GoodStuff.AspNetCore.Tests.Routing;

public class KebabCaseParameterTransformerTests
{
[Theory]
[InlineData("hello", "Hello")]
[InlineData("hello-world", "HelloWorld")]
[InlineData("hello-hello-hello", "HelloHelloHello")]
[InlineData("lowercasewords", "lowercasewords")]
public void TransformOutbound_Lowercase(string expectedValue, object? inputValue)
{
var transformer = new KebabCaseParameterTransformer(true);

Assert.Equal(expectedValue, transformer.TransformOutbound(inputValue));
}

[Theory]
[InlineData("Hello", "Hello")]
[InlineData("Hello-World", "HelloWorld")]
[InlineData("Hello-Hello-Hello", "HelloHelloHello")]
[InlineData("lowercasewords", "lowercasewords")]
public void TransformOutbound(string expectedValue, object? inputValue)
{
var transformer = new KebabCaseParameterTransformer();
Expand Down

0 comments on commit d69d634

Please sign in to comment.