Skip to content

Commit

Permalink
Merge pull request #43 from RubenMateus/feat/nonalphanumeric-removal
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenMateus authored May 26, 2020
2 parents d2acbf3 + b7e2342 commit 6d8987a
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 21 deletions.
15 changes: 11 additions & 4 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

[![NuGet](https://img.shields.io/nuget/v/Sluggy.svg)](https://www.nuget.org/packages/Sluggy/)

`dotnet add package Sluggy --version 2.0.1`
`dotnet add package Sluggy --version 3.0.1`

Sluggy is a configuration based class library designed to create friendly URL's.

**Check release notes for more info on the latest changes.**

---

## Sluggy Unidecode
Expand All @@ -32,13 +34,20 @@ Sluggy is a configuration based class library designed to create friendly URL's.
SluggyUnidecode is simple and powerful.
Extends the Sluggy package by using the Unidecode.NET library for normalization.

**Check release notes for more info on the latest changes.**

---

## QuickStart with Sluggy

### **There are 4 ways to use ToSlug()**

#### The default is *new CompositeStrategy(new ToLowerInvariantStrategy(), new NormalizationStrategy())*
#### The default strategies are:
``` csharp
NonAlphaNumericStrategy()
ToLowerInvariantStrategy()
NormalizationStrategy()
```

#### *Default separator is "-"*

Expand All @@ -53,7 +62,6 @@ using Sluggy;
"Eu NãO GoStO de PãO Da Avó".ToSlug();
// Output: "eu-nao-gosto-de-pao-da-avo"
```

---

### You can redefine the separator
Expand Down Expand Up @@ -110,7 +118,6 @@ using Sluggy;
var separator = "/";

// Defining the composite strategy
var normalizationWithUpperCaseStrategy = new CompositeStrategy(
new ToUpperCaseStrategy(),
new NormalizationStrategy());
Expand Down
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
2 changes: 1 addition & 1 deletion 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 All @@ -20,7 +21,6 @@ public void ShouldThrowNullArgumentException()
[InlineData("EU GOSTO DE TÁRTE", "tarte-tarte-tarte-tarte")]
[InlineData("EU GOSTO", "tarte-tarte")]
[InlineData("EU não GOSTO", "tarte-tarte-tarte")]
[InlineData("", "")]
public void ShouldReturnSlugifiedWithMocked(string value, string expectation)
{
const string translated = "tarte";
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<>*.,;´`'~^!#%$&/()=}{[]@£€§¨+|-ba", "aba")]
[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
10 changes: 6 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,10 @@ 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()
.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.1</Version>
<PackageIconUrl></PackageIconUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<AssemblyVersion>2.0.2.0</AssemblyVersion>
<FileVersion>2.0.2.0</FileVersion>
<AssemblyVersion>3.0.1.0</AssemblyVersion>
<FileVersion>3.0.1.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>
/// This strategy is used for removing non alpha numeric characters from the provided text.
/// </summary>
/// <param name="text">The text to be translated.</param>
/// <returns>The transformed text.</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 6d8987a

Please sign in to comment.