Skip to content

Instantly share code, notes, and snippets.

@lukencode
Created June 1, 2012 05:45
Show Gist options
  • Save lukencode/2849237 to your computer and use it in GitHub Desktop.
Save lukencode/2849237 to your computer and use it in GitHub Desktop.
thinkin fillr
public class NameFiller : IPropertyFiller<string>
{
private Random _rand;
private static Regex _combinedRegex = new Regex("name|fullname|firstname|lastname|surname|middlename|maidenname", RegexOptions.IgnoreCase);
private static Regex _fullNameRegex = new Regex("name|fullname", RegexOptions.IgnoreCase);
private static Regex _firstNameRegex = new Regex("firstname|middlename", RegexOptions.IgnoreCase);
private static Regex _surnameRegex = new Regex("lastname|surname|maidenname", RegexOptions.IgnoreCase);
private static List<string> _firstNames = new List<string> { "Luke", "John", "Mary" };
private static List<string> _surnames = new List<string> { "Lowrey", "Smith", "Peters" };
public NameFiller()
{
_rand = new Random();
}
public bool ShouldFill(string propertyName, Type propertyType)
{
if (propertyType != typeof(string))
return false;
var cleaned = propertyName.Replace("_", "");
return _combinedRegex.IsMatch(cleaned);
}
public string Fill(string propertyName, Type propertyType)
{
var cleaned = propertyName.Replace("_", "");
if (_firstNameRegex.IsMatch(cleaned))
{
return _firstNames.PickRandom();
}
else if (_surnameRegex.IsMatch(cleaned))
{
return _surnames.PickRandom();
}
else
{
return _firstNames.PickRandom() + " " + _surnames.PickRandom();
}
}
}
@dkarzon
Copy link

dkarzon commented Jun 1, 2012

Why not just use the PropertyInfo class instead of name and type?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment