Skip to content

Commit

Permalink
feat: Add non alphanumeric removal for better url and filename support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Mateus committed May 26, 2020
1 parent d2acbf3 commit 8bf75c0
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 16 deletions.
6 changes: 4 additions & 2 deletions Sluggy.Tests/SluggyIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class SluggyIntegrationTests
{
[Trait("Project", "Sluggy")]
[Theory(DisplayName = "Should Use Separator")]
[InlineData("EU GOSTO DE TÁRTE", "eu-gosto-de-tarte", "-")]
[InlineData("EU GOSTO DE TÁRTE", "euAgostoAdeAtarte", "A")]
[InlineData("EU GOSTO", "euBananagosto", "Banana")]
[InlineData("", "", "")]
Expand All @@ -18,10 +19,11 @@ public void ShouldUseSeparator(string value, string expectation, string separato

[Trait("Project", "Sluggy")]
[Theory(DisplayName = "Should Convert ToSlug")]
[InlineData("EU GOSTO DE TÁRTE", "eu-gosto-de-tarte")]
[InlineData("EU GOSTO DE TÁRTE ", "eu-gosto-de-tarte")]
[InlineData("eu gosto de tarte das", "eu-gosto-de-tarte-das")]
[InlineData("eu gosto de tarte", "eu-gosto-de-tarte")]
[InlineData("eu gosto de tarte ", "eu-gosto-de-tarte")]
[InlineData("eu não gosto de pão da avó", "eu-nao-gosto-de-pao-da-avo")]
[InlineData("Pão <>*.,;´`'~^!#%$&/()=}{[]@£€§¨|+ Avó - AAA", "pao-avo-aaa")]
[InlineData("", "")]
public void ShouldConvertToSlug(string value, string expectation)
{
Expand Down
1 change: 1 addition & 0 deletions Sluggy.Tests/SluggyUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Moq;
using Sluggy.Strategies;
using System;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Moq;
using Sluggy.Strategies;
using System.Linq;
using Xunit;

Expand Down
34 changes: 34 additions & 0 deletions Sluggy.Tests/Strategies/NonAlphaNumericStrategyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using Sluggy.Strategies;
using Xunit;

namespace Sluggy.Tests
{
public class NonAlphaNumericStrategyTests
{
[Trait("Project", "Sluggy")]
[Theory(DisplayName = "Should remove nonalphanumeric characters from string")]
[InlineData("<>*.,;´`'~^!#%$&/()=}{[]@£€§¨+|", "")]
[InlineData("a<>*.,;´`'~^!#%$&/()=}{[]@£€§¨+| -a", "a a")]
[InlineData("", "")]
public void ShouldNormalize(string value, string expectation)
{
var strategy = new NonAlphaNumericStrategy();

var translated = strategy.Translate(value);

Assert.Equal(expectation, translated);
}

[Trait("Project", "Sluggy")]
[Fact(DisplayName = "NonAlphaNumericStrategy Should Throw ArgumentNullException")]
public void ShouldThrowNullArgumentException()
{
const string text = null;

var strategy = new NonAlphaNumericStrategy();

Assert.Throws<ArgumentNullException>(() => strategy.Translate(text));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Sluggy.Strategies;
using Xunit;

namespace Sluggy.Tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Sluggy.Strategies;
using Xunit;

namespace Sluggy.Tests
Expand Down
11 changes: 7 additions & 4 deletions Sluggy/Sluggy.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Sluggy.Strategies;

namespace Sluggy
{
Expand All @@ -21,6 +22,7 @@ public static class Sluggy
/// This is the composite strategy used by default.
/// </summary>
public static readonly ITranslationStrategy DefaultTranslationStrategy = new CompositeStrategy(
new NonAlphaNumericStrategy(),
new ToLowerInvariantStrategy(),
new NormalizationStrategy());

Expand Down Expand Up @@ -70,10 +72,11 @@ public static string ToSlug(this string text, string separator, ITranslationStra
}

return text
.Split()
.Where(t => t.Length != 0)
.Select(t => strategy.Translate(t))
.Join(separator);
.Split()
.Where(t => t.Length != 0)
.Select(t => strategy.Translate(t))
.Where(t => t.Length != 0)
.Join(separator);
}

private static string Join(this IEnumerable<string> text, string separator) => string.Join(separator, text);
Expand Down
23 changes: 17 additions & 6 deletions Sluggy/Sluggy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,29 @@
<PackageLicenseUrl></PackageLicenseUrl>
<PackageProjectUrl />
<RepositoryUrl>https://github.com/RubenMateus/sluggy</RepositoryUrl>
<Copyright>Copyright (c) 2018 Ruben Mateus</Copyright>
<Copyright>Copyright (c) 2020 Ruben Mateus</Copyright>
<PackageTags>slug simple normalization url friendly generator normalizer urls slugs</PackageTags>
<Description>Sluggy is a configuration based class library designed to create friendly URL's.

With crazy support out of the box, but fully configurable.</Description>
<PackageReleaseNotes># Updated package dependencies</PackageReleaseNotes>
<PackageReleaseNotes># Updated package dependencies
Breaking Changes:
# Updated namespace for strategy classes to be more semantic when importing
# Added NonAlphanumericStrategy for better support for urls and filenames</PackageReleaseNotes>
<RepositoryType>Library</RepositoryType>
<Version>2.0.2</Version>
<PackageIconUrl>https://raw.githubusercontent.com/RubenMateus/sluggy/master/sluggy.png</PackageIconUrl>
<Version>3.0.0</Version>
<PackageIconUrl></PackageIconUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<AssemblyVersion>2.0.2.0</AssemblyVersion>
<FileVersion>2.0.2.0</FileVersion>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<FileVersion>3.0.0.0</FileVersion>
<PackageIcon>sluggy.png</PackageIcon>
</PropertyGroup>

<ItemGroup>
<None Include="..\sluggy.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace Sluggy
namespace Sluggy.Strategies
{
/// <summary>
/// The composite strategy to agreggate all the different ITranslationStrategies from Sluggy.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Sluggy
namespace Sluggy.Strategies
{
/// <summary>
/// Exposes the translation strategy, which supports a "translation" over a text.
Expand Down
32 changes: 32 additions & 0 deletions Sluggy/Strategies/NonAlphaNumericStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Linq;

namespace Sluggy.Strategies
{
/// <summary>
/// The standard NonAlphaNumeric Strategy used by Sluggy.
/// </summary>
public class NonAlphaNumericStrategy : ITranslationStrategy
{
/// <summary>
///
/// </summary>
/// <param name="text">The text to be translated.</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">Thrown when text is null.</exception>
public string Translate(string text)
{
if (text == null)
{
throw new ArgumentNullException(nameof(text));
}

var nonAlphaNumericChars = text.Where(c =>
char.IsLetterOrDigit(c) ||
char.IsWhiteSpace(c))
.ToArray();

return new string(nonAlphaNumericChars);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Text;

namespace Sluggy
namespace Sluggy.Strategies
{
/// <summary>
/// The standard normalization used by Sluggy.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Sluggy
namespace Sluggy.Strategies
{
/// <summary>
/// The standard ToLowerCase strategy used by Sluggy.
Expand Down

0 comments on commit 8bf75c0

Please sign in to comment.