Skip to content

Commit e9b2cfc

Browse files
Tidy
1 parent f48b678 commit e9b2cfc

File tree

5 files changed

+76
-34
lines changed

5 files changed

+76
-34
lines changed

src/Example/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ static void Main()
1313
var config = BuildConfig();
1414

1515
ServiceCollection services = new ServiceCollection();
16-
services.AddShortCodeGenerator<SequentialCodeGenerator>(config);
16+
services.AddShortCodeGenerator<SequentialCodeGenerator>(options =>
17+
{
18+
options.Pattern = new[]
19+
{
20+
new PatternOptions(NamedCharacterSpaces.UpperLatinLetters, 3),
21+
new PatternOptions("-"),
22+
new PatternOptions(NamedCharacterSpaces.Digits, 3),
23+
};
24+
});
1725

1826
ServiceProvider provider = services.BuildServiceProvider();
1927
var factory = provider.GetRequiredService<IShortCodeFactory>();

src/Stravaig.ShortCode.DependencyInjection/PatternOptionsExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace Stravaig.ShortCode.DependencyInjection
22
{
33
internal static class PatternOptionsExtensions
44
{
5-
public static PatternPart ToPatternPart(this ShortCodeOptions.PatternOptions options)
5+
public static PatternPart ToPatternPart(this PatternOptions options)
66
{
77
if (options.Type == PatternType.Fixed)
88
return new PatternPart(options.FixedString);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
3+
namespace Stravaig.ShortCode.Internal
4+
{
5+
internal static class StringExtensions
6+
{
7+
private const int CharacterSpaceMinLength = 2;
8+
9+
internal static void ValidateCharacterSpace(this string characterSpace)
10+
{
11+
if (string.IsNullOrWhiteSpace(characterSpace))
12+
throw new ArgumentException(
13+
"The value cannot be, null, Empty or contain whitespace.",
14+
nameof(characterSpace));
15+
if (characterSpace.Length < CharacterSpaceMinLength)
16+
throw new ArgumentException(
17+
$"The value must contain at least {CharacterSpaceMinLength} characters.",
18+
nameof(characterSpace));
19+
}
20+
}
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Stravaig.ShortCode.Internal;
2+
3+
namespace Stravaig.ShortCode
4+
{
5+
public class PatternOptions
6+
{
7+
private string _characterSpace;
8+
public PatternType Type { get; set; }
9+
10+
public string CharacterSpace
11+
{
12+
get => _characterSpace;
13+
set
14+
{
15+
value.ValidateCharacterSpace();
16+
_characterSpace = value;
17+
}
18+
}
19+
20+
public int Length { get; set; }
21+
22+
public string FixedString { get; set; }
23+
24+
public PatternOptions()
25+
{
26+
}
27+
28+
public PatternOptions(string fixedString)
29+
{
30+
Type = PatternType.Fixed;
31+
FixedString = fixedString;
32+
Length = fixedString.Length;
33+
}
34+
35+
public PatternOptions(string characterSpace, int length)
36+
{
37+
Type = PatternType.EncodeIntoCharacterSpace;
38+
CharacterSpace = characterSpace;
39+
Length = length;
40+
}
41+
}
42+
}

src/Stravaig.ShortCode/ShortCodeOptions.cs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3+
using Stravaig.ShortCode.Internal;
34

45
namespace Stravaig.ShortCode
56
{
67
public class ShortCodeOptions
78
{
8-
public class PatternOptions
9-
{
10-
private string _characterSpace;
11-
public PatternType Type { get; set; }
12-
13-
public string CharacterSpace
14-
{
15-
get => _characterSpace;
16-
set
17-
{
18-
ValidateCharacterSpace(value);
19-
_characterSpace = value;
20-
}
21-
}
22-
23-
public int Length { get; set; }
24-
25-
public string FixedString { get; set; }
26-
}
279
private const int AbsoluteMinLength = 1;
2810
private const int AbsoluteMaxLength = 64;
29-
private const int CharacterSpaceMinLength = 2;
3011

3112
private int _maxLength = AbsoluteMaxLength;
3213
private int? _fixedLength;
@@ -37,22 +18,12 @@ public string CharacterSpace
3718
get => _characterSpace;
3819
set
3920
{
40-
ValidateCharacterSpace(value);
21+
value.ValidateCharacterSpace();
4122
_characterSpace = value;
4223
}
4324
}
4425

45-
private static void ValidateCharacterSpace(string value)
46-
{
47-
if (string.IsNullOrWhiteSpace(value))
48-
throw new ArgumentException(
49-
"The value cannot be, null, Empty or contain whitespace.",
50-
nameof(CharacterSpace));
51-
if (value.Length < CharacterSpaceMinLength)
52-
throw new ArgumentException(
53-
$"The value must contain at least {CharacterSpaceMinLength} characters.",
54-
nameof(CharacterSpace));
55-
}
26+
5627

5728
public int MaxLength
5829
{

0 commit comments

Comments
 (0)