Skip to content

Instantly share code, notes, and snippets.

View feanz's full-sized avatar

Richard Forrest feanz

View GitHub Profile
@feanz
feanz / DaySuffix.cs
Created November 27, 2013 11:30
DaySuffix
public static string DaySuffix(this DateTime dateTime)
{
return (dateTime.Day == 1)
? "st"
: (dateTime.Day == 2)
? "nd"
: (dateTime.Day == 3)
? "rd"
: "th";
}
@feanz
feanz / DistinctBy.cs
Created November 26, 2013 11:44
DistinctBy Linq Expression
/// <summary>
/// Select distinct elements in collection on a given property
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="source">The collection to filter</param>
/// <param name="keySelector">The property to distinct collection by</param>
/// <returns></returns>
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
@feanz
feanz / CurrentVersionHeaderAttribute.cs
Created November 21, 2013 12:02
MVC Application Version Filter
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class CurrentVersionHeaderAttribute : ActionFilterAttribute
{
private const string _xVersion = "X-Version";
private static readonly string _version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
var headers = filterContext.HttpContext.Response.Headers;
@feanz
feanz / Trello
Created November 21, 2013 11:04
Trello Deserialise Checklist
void Main()
{
var data = @"{'id':'528c97b12314dcac5c0021b0','name':'Query','idBoard':'5268f13aa63b12236e0051d2','idCard':
'528c97aa3a003a4a0c00339a','pos':16384,'checkItems':[{'state':'incomplete','id':'528c97bf7a14af624e00228d',
'name':'What ares are on or off in what page for pocket hub section','nameData':{'emoji':{}},'pos':16970},
{'state':'incomplete','id':'528c97d3e790ecc25c0035f6',
'name':'Buy now integration','nameData':null,'pos':33467},
{'state':'incomplete','id':'528c97d9205dd7a54c003598',
'name':'Buys now quantitiy integration','nameData':null,'pos':50628},
{'state':'incomplete','id':'528c97f0b52fd8ab2f003227','name':'Vote functionality integration','nameData':null,'pos':67217},
@feanz
feanz / BetterDefaultModelBinder.cs
Created November 8, 2013 09:33
BetterDefaultModelBinder means all collection properties are instantiated as empty collections not null.
public class BetterDefaultModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
var model = base.CreateModel(controllerContext, bindingContext, modelType);
if (model == null || model is IEnumerable)
return model;
foreach (var property in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
@feanz
feanz / RemoveSpecialCharacters.cs
Created October 31, 2013 17:10
Remove Special Characters
protected static string RemoveAllSpecialCharacters(string input)
{
var r = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
return r.Replace(input, String.Empty);
}
protected static string RemoveSelectSpecialCharacters(string input)
{
var r = new Regex("[~#%&*{}:<>?|\".]", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
return r.Replace(input, String.Empty);
@feanz
feanz / SerializationExtensions.cs
Created October 30, 2013 15:39
SerializationExtensions
public static class SerializationExtensions
{
public static string Serialize(this object obj)
{
using (var memoryStream = new MemoryStream())
using (var reader = new StreamReader(memoryStream))
{
var serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
@feanz
feanz / DeploymentController.cs
Created October 23, 2013 10:28
Sitecore deployment manageer
public partial class DeploymentController : Controller
{
private DeploymentManager _manager;
protected DeploymentManager Manager
{
get { return _manager ?? (_manager = new DeploymentManager()); }
}
public virtual ActionResult Delete()
PRINT 'Checking the existence of the Version table.'
IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Version]') AND type IN (N'U'))
BEGIN
PRINT 'The Version table does not exist.'
PRINT 'Creating the Version table...'
CREATE TABLE [dbo].[Version] ([Current] [nvarchar](100) NOT NULL)
END
PRINT 'Checking the existence of a row in the Version table.'
IF NOT EXISTS (SELECT 1 FROM [dbo].[Version])
@feanz
feanz / 0_MicrosoftTranslator.cs
Last active August 12, 2017 06:37
Microsoft Translator
using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using Think.Formica.Extensions;
namespace Think.Formica.Translation