Skip to content

Commit

Permalink
ft: finalize changes to char limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Winner-Timothy Bolorunduro committed Aug 25, 2020
1 parent c941763 commit 0c75674
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
4 changes: 2 additions & 2 deletions shortid.Test/ShortId.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ public void SetSeedThrowsWhenCharacterSetIsLessThan20Characters()
.Should()
.Throw<InvalidOperationException>()
.WithMessage(
"The replacement characters must be at least 20 letters in length and without whitespace.");
"The replacement characters must be at least 50 letters in length and without whitespace.");
}

[Fact]
public void SetSeedWorksWithValidCharSet()
{
const string seed = "783ujrcuei039jhbewqoiewoiehjsbsahauwiwGHDEWIUkj4";
const string seed = "ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫";
Action action = () => { ShortId.SetCharacters(seed); };

action
Expand Down
18 changes: 8 additions & 10 deletions shortid/ShortId.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Text;
using shortid.Configuration;
using shortid.Utils;
Expand Down Expand Up @@ -132,24 +133,21 @@ public static void SetCharacters(string characters)
throw new ArgumentException("The replacement characters must not be null or empty.");
}

var stringBuilder = new StringBuilder();
foreach (var character in characters)
{
if (!char.IsWhiteSpace(character))
{
stringBuilder.Append(character);
}
}
var charSet = characters
.ToCharArray()
.Where(x => !char.IsWhiteSpace(x))
.Distinct()
.ToArray();

if (stringBuilder.Length < Constants.MinimumCharacterSetLength)
if (charSet.Length < Constants.MinimumCharacterSetLength)
{
throw new InvalidOperationException(
$"The replacement characters must be at least {Constants.MinimumCharacterSetLength} letters in length and without whitespace.");
}

lock (ThreadLock)
{
_pool = stringBuilder.ToString();
_pool = new string(charSet);
}
}

Expand Down

0 comments on commit 0c75674

Please sign in to comment.