fix: fixed replacements so they are applied first to allow "C#" to become "c-sharp"

This commit is contained in:
Dylan R. E. Moonfire 2022-06-05 14:03:28 -05:00
parent 1df8a6f56b
commit 6aa13be4a2
4 changed files with 130 additions and 14 deletions

View file

@ -41,6 +41,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{47
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CopyFiles", "examples\CopyFiles\CopyFiles.csproj", "{2C92A626-7A14-4FDB-906B-E7FA5FF18CC1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nitride.Slugs.Tests", "tests\Nitride.Slugs.Tests\Nitride.Slugs.Tests.csproj", "{C49E07D0-CD32-4332-90FA-07494195CAC4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -246,6 +248,18 @@ Global
{2C92A626-7A14-4FDB-906B-E7FA5FF18CC1}.Release|x64.Build.0 = Release|Any CPU
{2C92A626-7A14-4FDB-906B-E7FA5FF18CC1}.Release|x86.ActiveCfg = Release|Any CPU
{2C92A626-7A14-4FDB-906B-E7FA5FF18CC1}.Release|x86.Build.0 = Release|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Debug|x64.ActiveCfg = Debug|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Debug|x64.Build.0 = Debug|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Debug|x86.ActiveCfg = Debug|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Debug|x86.Build.0 = Debug|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Release|Any CPU.Build.0 = Release|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Release|x64.ActiveCfg = Release|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Release|x64.Build.0 = Release|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Release|x86.ActiveCfg = Release|Any CPU
{C49E07D0-CD32-4332-90FA-07494195CAC4}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D480943C-764D-4A8A-B546-642ED10586BB} = {570184FD-ECE4-4EC8-86E1-C1265E17D647}
@ -264,5 +278,6 @@ Global
{F940CD28-6867-4E5B-BC27-F476B3BEB49A} = {251D9C68-34EB-439D-B167-688BCC47DA17}
{29743817-A401-458F-9DD0-AF3579965953} = {251D9C68-34EB-439D-B167-688BCC47DA17}
{2C92A626-7A14-4FDB-906B-E7FA5FF18CC1} = {47461A29-E502-4B0E-AAF5-D87C4B93AB6D}
{C49E07D0-CD32-4332-90FA-07494195CAC4} = {251D9C68-34EB-439D-B167-688BCC47DA17}
EndGlobalSection
EndGlobal

View file

@ -12,30 +12,41 @@ namespace Nitride.Slugs;
/// </summary>
public class SimpleSlugConverter : ISlugConverter, IEnumerable<string>
{
private readonly List<(string, string)> replacements;
private readonly List<(string, string, StringComparison)> replacements;
public SimpleSlugConverter()
{
this.replacements = new List<(string, string)>();
this.replacements = new List<(string, string, StringComparison)>();
}
public SimpleSlugConverter(IDictionary<string, string> replacements)
public SimpleSlugConverter(
IDictionary<string, string> replacements,
StringComparison comparison = StringComparison.InvariantCultureIgnoreCase)
: this()
{
foreach (KeyValuePair<string, string> pair in replacements)
{
this.Add(pair.Key, pair.Value);
this.Add(pair.Key, pair.Value, comparison);
}
}
/// <summary>
/// Adds a replacement for the resulting slug.
/// Adds a replacement for the resulting slug. This is applied before the final
/// conversion
/// into a slug but after any other added replacements.
/// </summary>
/// <param name="search">The text to search for.</param>
/// <param name="replace">The replacement string.</param>
public void Add(string search, string replace)
/// <param name="comparison">
/// The comparison to use, defaults to invariant ignore
/// case.
/// </param>
public void Add(
string search,
string replace,
StringComparison comparison = StringComparison.InvariantCultureIgnoreCase)
{
this.replacements.Add((search, replace));
this.replacements.Add((search, replace, comparison));
}
/// <inheritdoc />
@ -53,16 +64,17 @@ public class SimpleSlugConverter : ISlugConverter, IEnumerable<string>
throw new ArgumentException("Cannot have a blank or null input", nameof(input));
}
// Create a slug..
// We need to do the replacements before we slugify.
// Perform any additional replacements.
foreach ((string search, string replace, StringComparison comparison) in this.replacements)
{
input = input.Replace(search, replace, comparison);
}
// Create a slug.
var helper = new SlugHelper();
string output = helper.GenerateSlug(input);
// Perform any additional replacements.
foreach ((string search, string replace) in this.replacements)
{
output = output.Replace(search, replace);
}
return output;
}

View file

@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Nitride.IO\Nitride.IO.csproj" />
<ProjectReference Include="..\..\src\Nitride.Slugs\Nitride.Slugs.csproj" />
<ProjectReference Include="..\Nitride.Tests\Nitride.Tests.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Gallium" Version="1.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="JunitXml.TestLogger" Version="3.0.114" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Zio" Version="0.15.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>

View file

@ -0,0 +1,59 @@
using System.Collections.Generic;
using Nitride.Tests;
using Xunit;
using Xunit.Abstractions;
namespace Nitride.Slugs.Tests;
/// <summary>
/// Tests the functionality of the WriteFiles().
/// </summary>
public class UnicodeNormalizingSlugConverterTest : NitrideTestBase
{
public UnicodeNormalizingSlugConverterTest(ITestOutputHelper output)
: base(output)
{
}
[Fact]
public void DefaultConversions()
{
var slug = new UnicodeNormalizingSlugConverter();
var expected = new List<string>();
var actual = new List<string>();
this.Test(slug, expected, actual, "One", "one");
this.Test(slug, expected, actual, "Two-Words", "two-words");
this.Test(slug, expected, actual, "Rutejìmo", "rutejimo");
this.Test(slug, expected, actual, "C#", "c");
Assert.Equal(expected, actual);
}
[Fact]
public void ReplacementConversions()
{
var slug = new UnicodeNormalizingSlugConverter(
new Dictionary<string, string>
{
["#"] = "-sharp",
});
var expected = new List<string>();
var actual = new List<string>();
this.Test(slug, expected, actual, "One", "one");
this.Test(slug, expected, actual, "Two-Words", "two-words");
this.Test(slug, expected, actual, "Rutejìmo", "rutejimo");
this.Test(slug, expected, actual, "C#", "c-sharp");
Assert.Equal(expected, actual);
}
private void Test(ISlugConverter slug, List<string> expected, List<string> actual, string input, string output)
{
expected.Add(output);
actual.Add(slug.ToSlug(input));
}
}