Skip to content

Commit f48b678

Browse files
Add ShortCode static additions for patterned codes
1 parent c0ae6c1 commit f48b678

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/Stravaig.ShortCode.Tests/ShortCodeTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ public void Use_SetsTheInternalGeneratorToCryptographicallyRandom()
105105
generator.ShouldBeOfType(typeof(CryptographicallyRandomCodeGenerator));
106106
}
107107

108+
[Test]
109+
public void GeneratePatternedShortCode_GetsAPatternedShortCode()
110+
{
111+
var shortCode = ShortCode.GeneratePatternedShortCode();
112+
Console.WriteLine(shortCode);
113+
shortCode.ShouldNotBeNull();
114+
shortCode.ShouldMatch(@"\w\w\w-\w\w\w-\w\w\w");
115+
}
116+
117+
[Test]
118+
public void SetPattern_ChangesThePattern()
119+
{
120+
ShortCode.SetPattern(new[]
121+
{
122+
new PatternPart(NamedCharacterSpaces.UpperLatinLetters, 2),
123+
new PatternPart("-"),
124+
new PatternPart(NamedCharacterSpaces.Digits, 4),
125+
new PatternPart("-"),
126+
new PatternPart(NamedCharacterSpaces.LowerLatinLetters, 2)
127+
});
128+
var shortCode = ShortCode.GeneratePatternedShortCode();
129+
Console.WriteLine(shortCode);
130+
shortCode.ShouldNotBeNull();
131+
shortCode.ShouldMatch(@"[A-Z]{2}-[0-9]{4}-[a-z]{2}");
132+
}
133+
108134
private static IEnumerable<int> Lengths()
109135
{
110136
for (int i = 1; i <= 9; i++)

src/Stravaig.ShortCode/PatternedEncoder.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,6 @@ private ulong BuildShortCode(ulong fullCode, PatternPart part, char[] code, int
5656
public int MaxLength()
5757
{
5858
throw new NotImplementedException();
59-
int result = 0;
60-
ulong remainingRange = ulong.MaxValue;
61-
foreach (var part in _parts)
62-
{
63-
result += (int)(Math.Log(remainingRange) / Math.Log(part.Length));
64-
}
65-
66-
return result;
6759
}
6860

6961
public string NamedCharacterSpace =>

src/Stravaig.ShortCode/ShortCode.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public static string GenerateRandomShortCode(int? length = null)
3939
return _encoder.Convert(code, length ?? _defaultLength);
4040
}
4141

42+
public static string GeneratePatternedShortCode()
43+
{
44+
ulong code = _randomGenerator.GetNextCode();
45+
return _patternedEncoder.Convert(code);
46+
}
47+
4248
public static void SetSequentialSeed(ulong seed)
4349
{
4450
_sequentialCodeGenerator = new SequentialCodeGenerator(seed);

0 commit comments

Comments
 (0)