Skip to content

Instantly share code, notes, and snippets.

View sebnilsson's full-sized avatar
☁️

Seb Nilsson sebnilsson

☁️
View GitHub Profile
@sebnilsson
sebnilsson / array.matchFirst.js
Created April 25, 2014 08:39
Get the first matching item in array, based on predicate-function
function matchFirst(arr, predicateFn) {
var index = -1;
arr.some(function(x, i) {
var isMatch = predicateFn(x, i);
if (isMatch) {
index = i;
return true;
}
});
@sebnilsson
sebnilsson / SetupPsProfile.ps1
Last active August 29, 2015 14:02
Setup PowerShell Profile-file
# Check if PowerShell Profile-file exists
Test-Path $profile
# if "false" create Profile-file
New-Item -path $profile -type file –force
# Start editing file at
# C:\Users\[USERNAME]\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
@sebnilsson
sebnilsson / MapMvcAttributeRoutes.Testable.cs
Created June 26, 2014 13:46
ASP.NET MVC 5: Testable RouteCollection.MapMvcAttributeRoutes
public static void MapMvcAttributeRoutes(
this RouteCollection routeCollection,
Assembly controllerAssembly,
IInlineConstraintResolver constraintResolver = null)
{
var controllerTypes = (from type in controllerAssembly.GetExportedTypes()
where
type != null && type.IsPublic
&& type.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)
&& !type.IsAbstract && typeof(IController).IsAssignableFrom(type)
@sebnilsson
sebnilsson / InputTextChange.js
Created August 22, 2014 13:15
Events to listen to for text changing in text-input
// http://stackoverflow.com/q/10659097/2429
$('input[type=text]').on('change input propertychange paste', function() {});
@sebnilsson
sebnilsson / ToDynamicObjectExtensions.cs
Created August 25, 2014 09:07
Extension-method to convert any object to dynamic ExpandoObject
public static ExpandoObject ToDynamic(this object obj)
{
var expandoObject = new ExpandoObject();
if (obj == null)
{
return expandoObject;
}
var dictionaryValues = new RouteValueDictionary(obj);
if (!dictionaryValues.Any())
@sebnilsson
sebnilsson / SuppressXFrameOptionsHeaderAttribute.cs
Created September 30, 2014 08:40
ASP.NET MVC: X-Frame-Options ALLOWALL
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class SuppressXFrameOptionsHeaderAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.Headers.Set("X-Frame-Options", "ALLOWALL");
}
}
@sebnilsson
sebnilsson / ValidRfc3986Chars.cs
Created October 4, 2011 14:54
Valid RFC 3986 Chars
// According to:
// http://www.ietf.org/rfc/rfc3986.txt
public static char[] ValidRfc3986Chars = new[] {
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9',
'-','.','_','~',':','/','?','#','[',']','@','!','$','&','\'','(',')','*','+',',',';','=',
};
@sebnilsson
sebnilsson / gist:1275714
Last active September 27, 2015 13:17
UrlHelper.ToAbsolute
public static string ToAbsolute(this UrlHelper urlHelper, string appRelativeUrl,
string relativeHostName = null, bool? useSecureConnection = null) {
var httpContext = urlHelper.RequestContext.HttpContext;
if(string.IsNullOrWhiteSpace(relativeHostName) && httpContext.Request == null) {
throw new ArgumentException("The provided host-name cannot be null or empty if there is no current HttpContext.", "relativeHostName");
}
relativeHostName = (relativeHostName ?? string.Empty).Trim(new[] { '/' });
if(!Uri.IsWellFormedUriString(relativeHostName, UriKind.Relative)) {
@sebnilsson
sebnilsson / gist:1709905
Created January 31, 2012 10:59
Init Google Maps via Javascript
// Needs reference to 'http://maps.google.com/maps/api/js?sensor=false'. Optional: '&language=en'.
// mapElement should contain a 'data-locations'-attribute with JSON-formatted objects
// with properties 'lat', 'long', 'title' and optional 'description'
var initMap = function (mapElement) {
var dataLocations = $(mapElement).attr('data-locations'); // Find DOM-attribute containing locations
var markerPositions = ($.parseJSON(dataLocations) || { locations: [] }).locations || [];
var latlng = new google.maps.LatLng(59.33279, 18.06449);
@sebnilsson
sebnilsson / gist:1759713
Created February 7, 2012 13:38
Load jQuery async
// Load jQuery
(function () {
var jq = document.createElement('script'); jq.type = 'text/javascript'; jq.async = true;
jq.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(jq, s);
})();