Skip to content

Commit 1fedd5d

Browse files
authored
Added Regex and IBAN (StefH#7)
* IBAN * Regex * fix postcode * 1.0.6.0 * 1.0.5.0
1 parent a10babb commit 1fedd5d

28 files changed

+2432
-61
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,27 @@ This is a simple generator to create random data.
77

88
## Supported Random Data
99

10-
- Text Patterns
10+
- Text Regex Patterns
1111
- Lorum Ipsum Text
1212
- Words
1313
- First/Last Names
1414
- Cities
1515
- Countries
1616
- IP-Addresses (V4 and V6)
17+
- IBANs
1718
- MAC Addresses
1819
- Email Addresses
1920
- Guids
2021
- DateTime
21-
- Integers
22+
- Numbers (integer, long, float, double, ...)
2223

2324
## Usage
2425

2526
``` csharp
27+
// Generate a random text with a Regular expression
28+
var randomizerTextRegex = RandomizerFactory.GetRandomizer(new FieldOptionsTextRegex { Pattern = @"^[0-9]{4}[A-Z]{2}" });
29+
string textRegex = randomizerTextRegex.Generate();
30+
2631
// Generate a random first name
2732
var randomizerFirstName = RandomizerFactory.GetRandomizer(new FieldOptionsFirstName());
2833
string firstName = randomizerFirstName.Generate();
@@ -44,8 +49,8 @@ You can also use a UI to generate SQL insert table statements.
4449
### Referenced files
4550
- http://www.cambiaresearch.com/articles/13/csharp-randomprovider-class
4651
- http://www.codeproject.com/Articles/423229/CsharpRandomStringGenerator
52+
- https://github.com/SaladLab/NetLegacySupport/tree/master/core/ConcurrentDictionary/System/Collections/Concurrent
4753

4854
### NuGet dependencies
4955
- NLipsum
50-
- NetLegacySupport.ConcurrentDictionary (only for .NET 2.0 & 3.5)
51-
- LinqBridge (only for .NET 2.0)
56+
- Fare

resources/IBAN.xlsx

16.9 KB
Binary file not shown.

src/ConsoleAppClassic/MainTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ public static class MainTest
1111
{
1212
public static void Run()
1313
{
14+
var randomizerTextRegex = RandomizerFactory.GetRandomizer(new FieldOptionsTextRegex { Pattern = @"^[1-9][0-9]{3}([A-RT-Z][A-Z]|[S][BCE-RT-Z])$" });
15+
string textRegex = randomizerTextRegex.Generate();
16+
Write(randomizerTextRegex, textRegex);
17+
18+
var randomizerIBAN1 = RandomizerFactory.GetRandomizer(new FieldOptionsIBAN());
19+
string IBAN1 = randomizerIBAN1.Generate();
20+
Write(randomizerIBAN1, IBAN1);
21+
22+
var randomizerIBAN2 = RandomizerFactory.GetRandomizer(new FieldOptionsIBAN { CountryCode = "NL" });
23+
string IBAN2 = randomizerIBAN2.Generate();
24+
Write(randomizerIBAN2, IBAN2);
25+
1426
var randomizerCity = RandomizerFactory.GetRandomizer(new FieldOptionsCity());
1527
string city = randomizerCity.Generate();
1628
Write(randomizerCity, city);

src/RandomDataGenerator/Compatibility/ConcurrentDictionary.cs

Lines changed: 2108 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copied from https://raw.githubusercontent.com/SaladLab/NetLegacySupport/master/core/ConcurrentDictionary/System/Collections/Concurrent/FrameworkTraits.cs
2+
#if NET20 || NET35
3+
using System;
4+
using System.Threading;
5+
6+
namespace RandomDataGenerator.Compatibility
7+
{
8+
internal static class Volatile
9+
{
10+
public static T Read<T>(ref T location) where T : class
11+
{
12+
//
13+
// The VM will replace this with a more efficient implementation.
14+
//
15+
var value = location;
16+
Thread.MemoryBarrier();
17+
return value;
18+
}
19+
20+
public static void Write<T>(ref T location, T value) where T : class
21+
{
22+
//
23+
// The VM will replace this with a more efficient implementation.
24+
//
25+
Thread.MemoryBarrier();
26+
location = value;
27+
}
28+
}
29+
30+
internal static class PlatformHelper
31+
{
32+
public static int ProcessorCount
33+
{
34+
get { return Environment.ProcessorCount; }
35+
}
36+
}
37+
38+
internal static class Monitor
39+
{
40+
public static void Enter(Object obj, ref bool lockTaken)
41+
{
42+
if (lockTaken)
43+
throw new ArgumentException("Argument must be initialized to false", "lockTaken");
44+
45+
System.Threading.Monitor.Enter(obj);
46+
lockTaken = true;
47+
}
48+
}
49+
}
50+
51+
#endif
Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1-
using System.Collections.Generic;
1+
using Fare;
2+
using RandomDataGenerator.TextData.Models;
3+
using System;
4+
using System.Collections.Generic;
25
using System.IO;
6+
using System.Linq;
37
using System.Reflection;
48
using System.Text;
59

610
namespace RandomDataGenerator.TextData
711
{
8-
public sealed class Texts
12+
internal sealed class ListData
913
{
1014
public IEnumerable<string> LastNames { get; }
15+
1116
public IEnumerable<string> MaleNames { get; }
17+
1218
public IEnumerable<string> FemaleNames { get; }
19+
1320
public IEnumerable<string> CityNames { get; }
21+
1422
public IEnumerable<string> CountryNames { get; }
23+
1524
public IEnumerable<string> Directions { get; }
25+
1626
public IEnumerable<string> StreetTypes { get; }
27+
1728
public IEnumerable<string> TopLevelDomains { get; }
1829

19-
Texts()
30+
public IEnumerable<IBAN> IBANs { get; }
31+
32+
ListData()
2033
{
2134
LastNames = GetResourceAsLines("LastNames");
2235
MaleNames = GetResourceAsLines("MaleNames");
@@ -26,30 +39,39 @@ public sealed class Texts
2639
Directions = new[] { "North", "East", "South", "West" };
2740
StreetTypes = new[] { "St.", "Ln.", "Ave.", "Way", "Blvd.", "Ct." };
2841
TopLevelDomains = new[] { "com", "net", "org", "us", "gov", "nl" };
42+
IBANs = GetResourceAsItems("IBAN", (columns) =>
43+
{
44+
return new IBAN
45+
{
46+
CountryName = columns[0],
47+
CountryCode = columns[1],
48+
Generator = new Xeger(columns[2])
49+
};
50+
});
2951
}
3052

31-
public static Texts Instance => Nested.TextInstance;
53+
public static ListData Instance => Nested.TextInstance;
3254

3355
// ReSharper disable once ClassNeverInstantiated.Local
34-
class Nested
56+
private class Nested
3557
{
3658
// Explicit static constructor to tell C# compiler not to mark type as before field-init
3759
static Nested()
3860
{
3961
}
4062

41-
internal static readonly Texts TextInstance = new Texts();
63+
internal static readonly ListData TextInstance = new ListData();
4264
}
4365

4466
private Stream GetResourceAsStream(string resourceName)
4567
{
46-
return typeof(Texts).GetTypeInfo().Assembly.GetManifestResourceStream($"RandomDataGenerator.TextData.{resourceName}.txt");
68+
return typeof(ListData).GetTypeInfo().Assembly.GetManifestResourceStream($"RandomDataGenerator.Data.Text.{resourceName}.txt");
4769
}
4870

4971
private IEnumerable<string> GetResourceAsLines(string fileName)
5072
{
5173
var stream = GetResourceAsStream(fileName);
52-
using (var reader = new StreamReader(stream, Encoding.ASCII))
74+
using (var reader = new StreamReader(stream, Encoding.UTF8))
5375
{
5476
string line;
5577
while ((line = reader.ReadLine()) != null)
@@ -58,5 +80,14 @@ private IEnumerable<string> GetResourceAsLines(string fileName)
5880
}
5981
}
6082
}
83+
84+
private IEnumerable<T> GetResourceAsItems<T>(string fileName, Func<string[], T> convert)
85+
{
86+
var lines = GetResourceAsLines(fileName);
87+
foreach (string line in lines)
88+
{
89+
yield return convert(line.Split('\t'));
90+
}
91+
}
6192
}
6293
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Fare;
2+
3+
namespace RandomDataGenerator.TextData.Models
4+
{
5+
internal class IBAN
6+
{
7+
public string CountryName { get; set; }
8+
9+
public string CountryCode { get; set; }
10+
11+
public Xeger Generator { get; set; }
12+
}
13+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Albania AL ^AL\d{10}[0-9A-Z]{16}$
2+
Andorra AD ^AD\d{10}[0-9A-Z]{12}$
3+
Austria AT ^AT\d{18}$
4+
Kingdom of Bahrain BH ^BH\d{2}[A-Z]{4}[0-9A-Z]{14}$
5+
Belgium BE ^BE\d{14}$
6+
Bosnia and Herzegovina BA ^BA\d{18}$
7+
Bulgaria BG ^BG\d{2}[A-Z]{4}\d{6}[0-9A-Z]{8}$
8+
Croatia HR ^HR\d{19}$
9+
Cyprus CY ^CY\d{10}[0-9A-Z]{16}$
10+
Czech Republic CZ ^CZ\d{22}$
11+
Denmark DK ^DK\d{16}$|^FO\d{16}$|^GL\d{16}$
12+
Dominican Republic DO ^DO\d{2}[0-9A-Z]{4}\d{20}$
13+
Estonia EE ^EE\d{18}$
14+
Finland FI ^FI\d{16}$
15+
France FR ^FR\d{12}[0-9A-Z]{11}\d{2}$
16+
Georgia GE ^GE\d{2}[A-Z]{2}\d{16}$
17+
Germany DE ^DE\d{20}$
18+
Gibraltar GI ^GI\d{2}[A-Z]{4}[0-9A-Z]{15}$
19+
Greece GR ^GR\d{9}[0-9A-Z]{16}$
20+
Hungary HU ^HU\d{26}$
21+
Iceland IS ^IS\d{24}$
22+
Ireland IE ^IE\d{2}[A-Z]{4}\d{14}$
23+
Israel IL ^IL\d{21}$
24+
Italy IT ^IT\d{2}[A-Z]\d{10}[0-9A-Z]{12}$
25+
Kazakhstan KZ ^[A-Z]{2}\d{5}[0-9A-Z]{13}$
26+
KUWAIT KW ^KW\d{2}[A-Z]{4}22!$
27+
Latvia LV ^LV\d{2}[A-Z]{4}[0-9A-Z]{13}$
28+
LEBANON LB ^LB\d{6}[0-9A-Z]{20}$
29+
Liechtenstein (Principality of) LI ^LI\d{7}[0-9A-Z]{12}$
30+
Lithuania LT ^LT\d{18}$
31+
Luxembourg LU ^LU\d{5}[0-9A-Z]{13}$
32+
Macedonia MK ^MK\d{5}[0-9A-Z]{10}\d{2}$
33+
Malta MT ^MT\d{2}[A-Z]{4}\d{5}[0-9A-Z]{18}$
34+
Mauritania MR ^MR13\d{23}$
35+
Mauritius MU ^MU\d{2}[A-Z]{4}\d{19}[A-Z]{3}$
36+
Monaco MC ^MC\d{12}[0-9A-Z]{11}\d{2}$
37+
Montenegro ME ^ME\d{20}$
38+
The Netherlands NL ^NL\d{2}[A-Z]{4}\d{10}$
39+
Norway NO ^NO\d{13}$
40+
Poland PL ^PL\d{10}[0-9A-Z]{16}$
41+
Portugal PT ^PT\d{23}$
42+
Romania RO ^RO\d{2}[A-Z]{4}[0-9A-Z]{16}$
43+
San Marino SM ^SM\d{2}[A-Z]\d{10}[0-9A-Z]{12}$
44+
Saudi Arabia SA ^SA\d{4}[0-9A-Z]{18}$
45+
Serbia RS ^RS\d{20}$
46+
Slovak Republic SK ^SK\d{22}$
47+
Slovenia SI ^SI\d{17}$
48+
Spain ES ^ES\d{22}$
49+
Sweden SE ^SE\d{22}$
50+
Switzerland CH ^CH\d{7}[0-9A-Z]{12}$
51+
Tunisia TN ^TN59\d{20}$
52+
Turkey TR ^TR\d{7}[0-9A-Z]{17}$
53+
United Arab Emirates AE ^AE\d{21}$
54+
United Kingdom GB ^GB\d{2}[A-Z]{4}\d{14}$

src/RandomDataGenerator/FieldOptions/FieldOptionsAbstract.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace RandomDataGenerator.FieldOptions
1010
[XmlInclude(typeof(FieldOptionsDateTime))]
1111
[XmlInclude(typeof(FieldOptionsEmailAddress))]
1212
[XmlInclude(typeof(FieldOptionsFirstName))]
13+
[XmlInclude(typeof(FieldOptionsIBAN))]
1314
[XmlInclude(typeof(FieldOptionsNumber<short>))]
1415
[XmlInclude(typeof(FieldOptionsNumber<int>))]
1516
[XmlInclude(typeof(FieldOptionsNumber<long>))]
@@ -23,6 +24,7 @@ namespace RandomDataGenerator.FieldOptions
2324
[XmlInclude(typeof(FieldOptionsLastName))]
2425
[XmlInclude(typeof(FieldOptionsMACAddress))]
2526
[XmlInclude(typeof(FieldOptionsTextPattern))]
27+
[XmlInclude(typeof(FieldOptionsTextRegex))]
2628
[XmlInclude(typeof(FieldOptionsText))]
2729
[XmlInclude(typeof(FieldOptionsTextWords))]
2830
[XmlInclude(typeof(FieldOptionsStringList))]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+

2+
namespace RandomDataGenerator.FieldOptions
3+
{
4+
public class FieldOptionsIBAN : FieldOptionsAbstract, IFieldOptionsString
5+
{
6+
public string CountryCode { get; set; }
7+
}
8+
}

src/RandomDataGenerator/FieldOptions/FieldOptionsTextPattern.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
namespace RandomDataGenerator.FieldOptions
1+
using System;
2+
3+
namespace RandomDataGenerator.FieldOptions
24
{
5+
[Obsolete("Use FieldOptionsTextRegex")]
36
public class FieldOptionsTextPattern : FieldOptionsAbstract, IFieldOptionsString
47
{
58
/// <summary>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace RandomDataGenerator.FieldOptions
2+
{
3+
public class FieldOptionsTextRegex : FieldOptionsAbstract, IFieldOptionsString
4+
{
5+
/// <summary>
6+
/// Use any valid Regex pattern to generate a string.
7+
/// </summary>
8+
public string Pattern { get; set; }
9+
}
10+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace RandomDataGenerator.Generators
6+
{
7+
internal class RandomItemFromListGenerator<T>
8+
{
9+
private readonly T[] _list;
10+
11+
public RandomItemFromListGenerator(IEnumerable<T> list, Func<T, bool> predicate = null)
12+
{
13+
_list = predicate == null ? list.ToArray() : list.Where(predicate).ToArray();
14+
}
15+
16+
public T Generate()
17+
{
18+
return _list.Length > 0 ? _list[RandomValueGenerator.Next(0, _list.Length)] : default(T);
19+
}
20+
}
21+
}
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32

43
namespace RandomDataGenerator.Generators
54
{
6-
internal class RandomStringFromListGenerator
5+
internal class RandomStringFromListGenerator : RandomItemFromListGenerator<string>
76
{
8-
private readonly string[] _list;
9-
10-
public RandomStringFromListGenerator(IEnumerable<string> list)
11-
{
12-
_list = list.ToArray();
13-
}
14-
15-
public string Generate()
7+
public RandomStringFromListGenerator(IEnumerable<string> list) : base(list)
168
{
17-
return _list.Length > 0 ? _list[RandomValueGenerator.Next(0, _list.Length)] : string.Empty;
189
}
1910
}
2011
}

0 commit comments

Comments
 (0)