Skip to content

Commit

Permalink
refactor: improve the performance of ToKebab method (#6)
Browse files Browse the repository at this point in the history
* ⚡ refactor: improve performance for to-kebab regex

* improve

* remove unused code
  • Loading branch information
capdiem authored Jan 9, 2024
1 parent 165dbba commit e5c988b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion BemIt/BemIt.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
<Description>Helps you organize and write CSS using BEM, especially in Blazor.</Description>
<PackageId>BemIt</PackageId>
Expand Down
13 changes: 11 additions & 2 deletions BemIt/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@

namespace BemIt;

public static class StringExtensions
public static partial class StringExtensions
{
public static string ToKebab(this string name)
{
var split = new Regex("(?<!^)(?=[A-Z])").Split(name).Select(s => s.Trim('-'));
#if NET7_0_OR_GREATER
var split = MyRegex().Split(name).Select(s => s.Trim('-'));
#else
var split = Regex.Split(name, "(?<!^)(?=[A-Z])").Select(s => s.Trim('-'));
#endif
return string.Join("-", split).ToLowerInvariant();
}
#if NET7_0_OR_GREATER

[GeneratedRegex("(?<!^)(?=[A-Z])")]
private static partial Regex MyRegex();
#endif
}

0 comments on commit e5c988b

Please sign in to comment.