Skip to content

Instantly share code, notes, and snippets.

View pilotgeraldb's full-sized avatar
😎
being awesome

Gerald B. pilotgeraldb

😎
being awesome
View GitHub Profile
public class ModelValidator : AbstractValidator<Model> {
public ModelValidator() {
RuleFor(x => x.Surname).NotEmpty();
RuleFor(x => x.Forename).NotEmpty().WithMessage("Please specify a first name");
RuleFor(x => x.Discount).NotEqual(0).When(x => x.HasDiscount);
RuleFor(x => x.Address).Length(20, 250);
RuleFor(x => x.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
}
}
@pilotgeraldb
pilotgeraldb / extension method -- IList -- move
Last active June 23, 2016 18:28
allows to 'move' items in an IList object
public static void Move<T>(this IList<T> list, int oldIndex, int newIndex)
{
var item = list[oldIndex];
list.RemoveAt(oldIndex);
if (newIndex > oldIndex)
{
newIndex--;
}
@pilotgeraldb
pilotgeraldb / custom config reader
Created June 21, 2016 14:21
deserializes a section in the config file
public abstract class XmlDeserializeConfigSectionHandler : IConfigurationSectionHandler
{
public XmlDeserializeConfigSectionHandler() : base()
{
}
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
Type t = this.GetType();
@pilotgeraldb
pilotgeraldb / hash file
Last active June 6, 2016 20:22
creates a hash of a file from a filename
using (MD5 md5 = MD5.Create())
{
using (FileStream stream = File.OpenRead(filename))
{
byte[] bytes = md5.ComputeHash(stream);
bool uppercase = false;
StringBuilder result = new StringBuilder(bytes.Length * 2);
for (int i = 0; i < bytes.Length; i++)
@pilotgeraldb
pilotgeraldb / split enumerable into groups of n
Created June 1, 2016 14:07
splits an ienumerable into groups of count n
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> source, int countPerList)
{
if (source == null || countPerList <= 0)
{
return Enumerable.Empty<IEnumerable<T>>();
}
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / countPerList)
@pilotgeraldb
pilotgeraldb / sqrt
Last active May 31, 2016 19:57
a javascript implementation using a taylor series to approximate the square root of a number (my own original work)
function squareRoot(value, precision)
{
function calcSeedVal(value, precision)
{
var a = 0;
var result = 0;
if(value > 9)
{
a = value / Math.pow(10,precision);
@pilotgeraldb
pilotgeraldb / MSSQL -- table metadata
Created May 11, 2016 15:28
retrieves table information in mssql
DECLARE @tablename varchar(50)
SET @tablename = '' -- <--EDIT THIS VALUE
DECLARE @columnname varchar(50)
SET @columnname = '' -- <--EDIT THIS VALUE
SELECT
SCHEMANAME = SCHEMA_NAME(SYSTABLES.SCHEMA_ID),
TABLENAME = SYSTABLES.NAME,
COLUMNNAME = _SYSCOLUMNS.NAME,
@pilotgeraldb
pilotgeraldb / string prototypes
Created May 10, 2016 16:46
javascript, utility string prototypes
String.prototype.startsWith = function(str)
{
return (this.indexOf(str) === 0);
};
String.prototype.contains = function(str)
{
return (this.indexOf(str, 0) !== -1);
};
@pilotgeraldb
pilotgeraldb / PsudeoGUID
Created May 10, 2016 14:44
javascript, a function for creating psuedo guid-like strings.
function guid()
{
var guidTemplate = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx';
var guid = guidTemplate.replace(/[xy]/g, function (c)
{
var r = Math.random() * 16 | 0;
var v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
@pilotgeraldb
pilotgeraldb / MouseWheelEventHandler
Last active May 10, 2016 14:55
javascript cross browser mouse wheel event registration
if (window.addEventListener)
{
// IE9, Chrome, Safari, Opera
window.addEventListener("mousewheel", function (e)
{
someEventHandler(e);
}, false);
// Firefox
window.addEventListener("DOMMouseScroll", function (e)
{