Skip to content

Instantly share code, notes, and snippets.

View AdamLJohnson's full-sized avatar

Adam Johnson AdamLJohnson

View GitHub Profile
@AdamLJohnson
AdamLJohnson / T4TemplateChanges.cs
Created January 31, 2012 17:49
DBContext T4 Template changes for Key Required StringLength Attributes
// Add at the top with the other Usings
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
//Add following methods inside the file
private bool IsNullable(TypeUsage usage)
{
return (bool)usage.Facets.First(facet => facet.Name == "Nullable").Value;
}
@AdamLJohnson
AdamLJohnson / fizbuzz.cs
Created August 6, 2012 06:14
My FizzBuzz Test
private static string FizzBuzz(int i)
{
if ((i % 3 == 0) && (i % 5 == 0)) return "FizzBuzz";
else if (i % 3 == 0) return "Fizz";
else if (i % 5 == 0) return "Buzz";
else return i.ToString();
}
@AdamLJohnson
AdamLJohnson / proxy.cs
Created April 25, 2013 23:14
Create a proxy action to help with handling cross site scripting issues. Got the idea from: http://encosia.com/use-asp-nets-httphandler-to-bridge-the-cross-domain-gap/
public void proxy(string url)
{
WebClient wc = new WebClient();
string baseUrl = url;
string response = wc.DownloadString(baseUrl);
HttpContext.Response.ContentType = "application/json";
HttpContext.Response.Write(response);
}
@AdamLJohnson
AdamLJohnson / GetAge.js
Created September 10, 2013 16:23
This function returns age when given a DOB.
//This function returns age when given a DOB. getAge(DOB)
//You can also get the age on a given date. getAge(DOB, onDate)
function getAge(d1, d2) {
d2 = d2 || new Date();
var diff = d2.getTime() - d1.getTime();
return Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25));
}
@AdamLJohnson
AdamLJohnson / SetupASPvNext.sh
Last active December 30, 2015 17:15
Script to setup ASP.Net vNext on Ubuntu
#!/bin/sh
##https://docs.asp.net/en/latest/getting-started/installing-on-linux.html#installing-on-debian-ubuntu-and-derivatives
## Install the .NET Version Manager (DNVM)
sudo apt-get --assume-yes install unzip curl
curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh
# Install the .NET Execution Environment (DNX)
## Install the DNX prerequisites:
@AdamLJohnson
AdamLJohnson / RecursiveSetProperty.cs
Last active February 29, 2016 18:59
Recursive SetProperty
using System;
using System.Linq;
using System.Reflection;
public class RecursiveSetProperty
{
/// <summary>
/// Sets the property of an object recursively.
/// </summary>
/// <param name="target">The target object.</param>
@AdamLJohnson
AdamLJohnson / EnumerableExtensions.cs
Created January 31, 2012 17:28
LINQ using Wildcards
// Source: http://stackoverflow.com/questions/3102250/linq-search-using-wildcards-character-like
public static class EnumerableExtensions
{
public static IEnumerable<T> MatchesWildcard<T>(this IEnumerable<T> sequence, Func<T,string> expression, string pattern)
{
var regEx = WildcardToRegex(pattern);
return sequence.Where(item => Regex.IsMatch(expression(item), regEx));
}
public static string WildcardToRegex(string pattern)
@AdamLJohnson
AdamLJohnson / OpenWithSublimeText3.bat
Created August 2, 2016 16:33 — forked from roundand/OpenWithSublimeText3.bat
Open folders and files with Sublime Text 3 from windows explorer context menu (tested in Windows 7)
@echo off
SET st3Path=C:\Program Files\Sublime Text 3\sublime_text.exe
rem add it for all file types
@reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3" /t REG_SZ /v "" /d "Open with Sublime Text 3" /f
@reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3" /t REG_EXPAND_SZ /v "Icon" /d "%st3Path%,0" /f
@reg add "HKEY_CLASSES_ROOT\*\shell\Open with Sublime Text 3\command" /t REG_SZ /v "" /d "%st3Path% \"%%1\"" /f
rem add it for folders
@reg add "HKEY_CLASSES_ROOT\Folder\shell\Open with Sublime Text 3" /t REG_SZ /v "" /d "Open with Sublime Text 3" /f
@AdamLJohnson
AdamLJohnson / postal-codes.json
Created September 14, 2017 14:53 — forked from matthewbednarski/postal-codes.json
Global postal codes regex formats
[{ "Note": "The first two digits (ranging from 10–43) correspond to the province, while the last two digits correspond either to the city/delivery zone (range 01–50) or to the district/delivery zone (range 51–99). Afghanistan Postal code lookup", "Country": "Afghanistan", "ISO": "AF", "Format": "NNNN", "Regex": "^\\d{4}$" }, { "Note": "With Finland, first two numbers are 22.", "Country": "Åland Islands", "ISO": "AX", "Format": "NNNNN", "Regex": "^\\d{5}$" }, { "Note": "Introduced in 2006, gradually implemented throughout 2007.", "Country": "Albania", "ISO": "AL", "Format": "NNNN", "Regex": "^\\d{4}$" }, { "Note": "First two as in ISO 3166-2:DZ", "Country": "Algeria", "ISO": "DZ", "Format": "NNNNN", "Regex": "^\\d{5}$" }, { "Note": "U.S. ZIP codes (range 96799)", "Country": "American Samoa", "ISO": "AS", "Format": "NNNNN (optionally NNNNN-NNNN or NNNNN-NNNNNN)", "Regex": "^\\d{5}(-{1}\\d{4,6})$" }, { "Note":
@AdamLJohnson
AdamLJohnson / FullDBBackup.ps1
Created February 9, 2016 17:57
Powershell script to backup all SQL Databases on a server. Useful for SQL Express.
$serverName = ".\SQLExpress"
$backupDirectory = "D:\backupSQL"
$daysToStoreDailyBackups = 7
$daysToStoreWeeklyBackups = 28
$monthsToStoreMonthlyBackups = 3
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null