Skip to content

Commit

Permalink
Added Regex and IBAN (#7)
Browse files Browse the repository at this point in the history
* IBAN

* Regex

* fix postcode

* 1.0.6.0

* 1.0.5.0
  • Loading branch information
StefH authored Dec 2, 2018
1 parent a10babb commit 1fedd5d
Show file tree
Hide file tree
Showing 28 changed files with 2,432 additions and 61 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ This is a simple generator to create random data.

## Supported Random Data

- Text Patterns
- Text Regex Patterns
- Lorum Ipsum Text
- Words
- First/Last Names
- Cities
- Countries
- IP-Addresses (V4 and V6)
- IBANs
- MAC Addresses
- Email Addresses
- Guids
- DateTime
- Integers
- Numbers (integer, long, float, double, ...)

## Usage

``` csharp
// Generate a random text with a Regular expression
var randomizerTextRegex = RandomizerFactory.GetRandomizer(new FieldOptionsTextRegex { Pattern = @"^[0-9]{4}[A-Z]{2}" });
string textRegex = randomizerTextRegex.Generate();

// Generate a random first name
var randomizerFirstName = RandomizerFactory.GetRandomizer(new FieldOptionsFirstName());
string firstName = randomizerFirstName.Generate();
Expand All @@ -44,8 +49,8 @@ You can also use a UI to generate SQL insert table statements.
### Referenced files
- https://www.cambiaresearch.com/articles/13/csharp-randomprovider-class
- https://www.codeproject.com/Articles/423229/CsharpRandomStringGenerator
- https://github.com/SaladLab/NetLegacySupport/tree/master/core/ConcurrentDictionary/System/Collections/Concurrent

### NuGet dependencies
- NLipsum
- NetLegacySupport.ConcurrentDictionary (only for .NET 2.0 & 3.5)
- LinqBridge (only for .NET 2.0)
- Fare
Binary file added resources/IBAN.xlsx
Binary file not shown.
12 changes: 12 additions & 0 deletions src/ConsoleAppClassic/MainTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ public static class MainTest
{
public static void Run()
{
var randomizerTextRegex = RandomizerFactory.GetRandomizer(new FieldOptionsTextRegex { Pattern = @"^[1-9][0-9]{3}([A-RT-Z][A-Z]|[S][BCE-RT-Z])$" });
string textRegex = randomizerTextRegex.Generate();
Write(randomizerTextRegex, textRegex);

var randomizerIBAN1 = RandomizerFactory.GetRandomizer(new FieldOptionsIBAN());
string IBAN1 = randomizerIBAN1.Generate();
Write(randomizerIBAN1, IBAN1);

var randomizerIBAN2 = RandomizerFactory.GetRandomizer(new FieldOptionsIBAN { CountryCode = "NL" });
string IBAN2 = randomizerIBAN2.Generate();
Write(randomizerIBAN2, IBAN2);

var randomizerCity = RandomizerFactory.GetRandomizer(new FieldOptionsCity());
string city = randomizerCity.Generate();
Write(randomizerCity, city);
Expand Down
2,108 changes: 2,108 additions & 0 deletions src/RandomDataGenerator/Compatibility/ConcurrentDictionary.cs

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions src/RandomDataGenerator/Compatibility/FrameworkTraits.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copied from https://raw.githubusercontent.com/SaladLab/NetLegacySupport/master/core/ConcurrentDictionary/System/Collections/Concurrent/FrameworkTraits.cs
#if NET20 || NET35
using System;
using System.Threading;

namespace RandomDataGenerator.Compatibility
{
internal static class Volatile
{
public static T Read<T>(ref T location) where T : class
{
//
// The VM will replace this with a more efficient implementation.
//
var value = location;
Thread.MemoryBarrier();
return value;
}

public static void Write<T>(ref T location, T value) where T : class
{
//
// The VM will replace this with a more efficient implementation.
//
Thread.MemoryBarrier();
location = value;
}
}

internal static class PlatformHelper
{
public static int ProcessorCount
{
get { return Environment.ProcessorCount; }
}
}

internal static class Monitor
{
public static void Enter(Object obj, ref bool lockTaken)
{
if (lockTaken)
throw new ArgumentException("Argument must be initialized to false", "lockTaken");

System.Threading.Monitor.Enter(obj);
lockTaken = true;
}
}
}

#endif
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
using System.Collections.Generic;
using Fare;
using RandomDataGenerator.TextData.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace RandomDataGenerator.TextData
{
public sealed class Texts
internal sealed class ListData
{
public IEnumerable<string> LastNames { get; }

public IEnumerable<string> MaleNames { get; }

public IEnumerable<string> FemaleNames { get; }

public IEnumerable<string> CityNames { get; }

public IEnumerable<string> CountryNames { get; }

public IEnumerable<string> Directions { get; }

public IEnumerable<string> StreetTypes { get; }

public IEnumerable<string> TopLevelDomains { get; }

Texts()
public IEnumerable<IBAN> IBANs { get; }

ListData()
{
LastNames = GetResourceAsLines("LastNames");
MaleNames = GetResourceAsLines("MaleNames");
Expand All @@ -26,30 +39,39 @@ public sealed class Texts
Directions = new[] { "North", "East", "South", "West" };
StreetTypes = new[] { "St.", "Ln.", "Ave.", "Way", "Blvd.", "Ct." };
TopLevelDomains = new[] { "com", "net", "org", "us", "gov", "nl" };
IBANs = GetResourceAsItems("IBAN", (columns) =>
{
return new IBAN
{
CountryName = columns[0],
CountryCode = columns[1],
Generator = new Xeger(columns[2])
};
});
}

public static Texts Instance => Nested.TextInstance;
public static ListData Instance => Nested.TextInstance;

// ReSharper disable once ClassNeverInstantiated.Local
class Nested
private class Nested
{
// Explicit static constructor to tell C# compiler not to mark type as before field-init
static Nested()
{
}

internal static readonly Texts TextInstance = new Texts();
internal static readonly ListData TextInstance = new ListData();
}

private Stream GetResourceAsStream(string resourceName)
{
return typeof(Texts).GetTypeInfo().Assembly.GetManifestResourceStream($"RandomDataGenerator.TextData.{resourceName}.txt");
return typeof(ListData).GetTypeInfo().Assembly.GetManifestResourceStream($"RandomDataGenerator.Data.Text.{resourceName}.txt");
}

private IEnumerable<string> GetResourceAsLines(string fileName)
{
var stream = GetResourceAsStream(fileName);
using (var reader = new StreamReader(stream, Encoding.ASCII))
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
Expand All @@ -58,5 +80,14 @@ private IEnumerable<string> GetResourceAsLines(string fileName)
}
}
}

private IEnumerable<T> GetResourceAsItems<T>(string fileName, Func<string[], T> convert)
{
var lines = GetResourceAsLines(fileName);
foreach (string line in lines)
{
yield return convert(line.Split('\t'));
}
}
}
}
13 changes: 13 additions & 0 deletions src/RandomDataGenerator/Data/Models/IBAN.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Fare;

namespace RandomDataGenerator.TextData.Models
{
internal class IBAN
{
public string CountryName { get; set; }

public string CountryCode { get; set; }

public Xeger Generator { get; set; }
}
}
54 changes: 54 additions & 0 deletions src/RandomDataGenerator/Data/Text/IBAN.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Albania AL ^AL\d{10}[0-9A-Z]{16}$
Andorra AD ^AD\d{10}[0-9A-Z]{12}$
Austria AT ^AT\d{18}$
Kingdom of Bahrain BH ^BH\d{2}[A-Z]{4}[0-9A-Z]{14}$
Belgium BE ^BE\d{14}$
Bosnia and Herzegovina BA ^BA\d{18}$
Bulgaria BG ^BG\d{2}[A-Z]{4}\d{6}[0-9A-Z]{8}$
Croatia HR ^HR\d{19}$
Cyprus CY ^CY\d{10}[0-9A-Z]{16}$
Czech Republic CZ ^CZ\d{22}$
Denmark DK ^DK\d{16}$|^FO\d{16}$|^GL\d{16}$
Dominican Republic DO ^DO\d{2}[0-9A-Z]{4}\d{20}$
Estonia EE ^EE\d{18}$
Finland FI ^FI\d{16}$
France FR ^FR\d{12}[0-9A-Z]{11}\d{2}$
Georgia GE ^GE\d{2}[A-Z]{2}\d{16}$
Germany DE ^DE\d{20}$
Gibraltar GI ^GI\d{2}[A-Z]{4}[0-9A-Z]{15}$
Greece GR ^GR\d{9}[0-9A-Z]{16}$
Hungary HU ^HU\d{26}$
Iceland IS ^IS\d{24}$
Ireland IE ^IE\d{2}[A-Z]{4}\d{14}$
Israel IL ^IL\d{21}$
Italy IT ^IT\d{2}[A-Z]\d{10}[0-9A-Z]{12}$
Kazakhstan KZ ^[A-Z]{2}\d{5}[0-9A-Z]{13}$
KUWAIT KW ^KW\d{2}[A-Z]{4}22!$
Latvia LV ^LV\d{2}[A-Z]{4}[0-9A-Z]{13}$
LEBANON LB ^LB\d{6}[0-9A-Z]{20}$
Liechtenstein (Principality of) LI ^LI\d{7}[0-9A-Z]{12}$
Lithuania LT ^LT\d{18}$
Luxembourg LU ^LU\d{5}[0-9A-Z]{13}$
Macedonia MK ^MK\d{5}[0-9A-Z]{10}\d{2}$
Malta MT ^MT\d{2}[A-Z]{4}\d{5}[0-9A-Z]{18}$
Mauritania MR ^MR13\d{23}$
Mauritius MU ^MU\d{2}[A-Z]{4}\d{19}[A-Z]{3}$
Monaco MC ^MC\d{12}[0-9A-Z]{11}\d{2}$
Montenegro ME ^ME\d{20}$
The Netherlands NL ^NL\d{2}[A-Z]{4}\d{10}$
Norway NO ^NO\d{13}$
Poland PL ^PL\d{10}[0-9A-Z]{16}$
Portugal PT ^PT\d{23}$
Romania RO ^RO\d{2}[A-Z]{4}[0-9A-Z]{16}$
San Marino SM ^SM\d{2}[A-Z]\d{10}[0-9A-Z]{12}$
Saudi Arabia SA ^SA\d{4}[0-9A-Z]{18}$
Serbia RS ^RS\d{20}$
Slovak Republic SK ^SK\d{22}$
Slovenia SI ^SI\d{17}$
Spain ES ^ES\d{22}$
Sweden SE ^SE\d{22}$
Switzerland CH ^CH\d{7}[0-9A-Z]{12}$
Tunisia TN ^TN59\d{20}$
Turkey TR ^TR\d{7}[0-9A-Z]{17}$
United Arab Emirates AE ^AE\d{21}$
United Kingdom GB ^GB\d{2}[A-Z]{4}\d{14}$
2 changes: 2 additions & 0 deletions src/RandomDataGenerator/FieldOptions/FieldOptionsAbstract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace RandomDataGenerator.FieldOptions
[XmlInclude(typeof(FieldOptionsDateTime))]
[XmlInclude(typeof(FieldOptionsEmailAddress))]
[XmlInclude(typeof(FieldOptionsFirstName))]
[XmlInclude(typeof(FieldOptionsIBAN))]
[XmlInclude(typeof(FieldOptionsNumber<short>))]
[XmlInclude(typeof(FieldOptionsNumber<int>))]
[XmlInclude(typeof(FieldOptionsNumber<long>))]
Expand All @@ -23,6 +24,7 @@ namespace RandomDataGenerator.FieldOptions
[XmlInclude(typeof(FieldOptionsLastName))]
[XmlInclude(typeof(FieldOptionsMACAddress))]
[XmlInclude(typeof(FieldOptionsTextPattern))]
[XmlInclude(typeof(FieldOptionsTextRegex))]
[XmlInclude(typeof(FieldOptionsText))]
[XmlInclude(typeof(FieldOptionsTextWords))]
[XmlInclude(typeof(FieldOptionsStringList))]
Expand Down
8 changes: 8 additions & 0 deletions src/RandomDataGenerator/FieldOptions/FieldOptionsIBAN.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

namespace RandomDataGenerator.FieldOptions
{
public class FieldOptionsIBAN : FieldOptionsAbstract, IFieldOptionsString
{
public string CountryCode { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace RandomDataGenerator.FieldOptions
using System;

namespace RandomDataGenerator.FieldOptions
{
[Obsolete("Use FieldOptionsTextRegex")]
public class FieldOptionsTextPattern : FieldOptionsAbstract, IFieldOptionsString
{
/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions src/RandomDataGenerator/FieldOptions/FieldOptionsTextRegex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace RandomDataGenerator.FieldOptions
{
public class FieldOptionsTextRegex : FieldOptionsAbstract, IFieldOptionsString
{
/// <summary>
/// Use any valid Regex pattern to generate a string.
/// </summary>
public string Pattern { get; set; }
}
}
21 changes: 21 additions & 0 deletions src/RandomDataGenerator/Generators/RandomItemFromListGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace RandomDataGenerator.Generators
{
internal class RandomItemFromListGenerator<T>
{
private readonly T[] _list;

public RandomItemFromListGenerator(IEnumerable<T> list, Func<T, bool> predicate = null)
{
_list = predicate == null ? list.ToArray() : list.Where(predicate).ToArray();
}

public T Generate()
{
return _list.Length > 0 ? _list[RandomValueGenerator.Next(0, _list.Length)] : default(T);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
using System.Collections.Generic;
using System.Linq;

namespace RandomDataGenerator.Generators
{
internal class RandomStringFromListGenerator
internal class RandomStringFromListGenerator : RandomItemFromListGenerator<string>
{
private readonly string[] _list;

public RandomStringFromListGenerator(IEnumerable<string> list)
{
_list = list.ToArray();
}

public string Generate()
public RandomStringFromListGenerator(IEnumerable<string> list) : base(list)
{
return _list.Length > 0 ? _list[RandomValueGenerator.Next(0, _list.Length)] : string.Empty;
}
}
}
Loading

0 comments on commit 1fedd5d

Please sign in to comment.