Skip to content

Instantly share code, notes, and snippets.

View sebnilsson's full-sized avatar
☁️

Seb Nilsson sebnilsson

☁️
View GitHub Profile
@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 / 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 / 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 / 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 / StringEnsureStartsEndsWithExtensions.cs
Created December 13, 2013 09:07
Helper-functions to ensure string starts/ends with a specific string
public static class StringExtensions
{
public static string EnsureEndsWith(
this string value,
string endValue,
StringComparison comparisonType = StringComparison.CurrentCulture)
{
return (value == null || value.EndsWith(endValue, comparisonType)) ? value : (value + endValue);
}
@sebnilsson
sebnilsson / throttleFn.js
Last active December 31, 2015 03:59
Convert function to throttle function
function throttleFn(fn, wait, context) {
wait = wait || defaultThrottleWait;
var last, timeoutId;
return function() {
var ctx = (context || this),
now = +new Date,
args = arguments;
if (last && now < (last + wait)) {
@sebnilsson
sebnilsson / TrimBom.cs
Created October 23, 2013 12:44
Trim BOM (Byte Order Mark) from text.
public static string TrimBom(string text, Encoding encoding)
{
var bom = encoding.GetPreamble();
if (bom.Length < 1)
{
return text;
}
var textBytes = Convert.FromBase64String(text);
if (!textBytes.Take(bom.Length).SequenceEqual(bom))
@sebnilsson
sebnilsson / chocolatey-unzip-movetoparent.ps1
Created September 11, 2013 11:13
Unpacking a zip-package and moving sub-folder to parent, in Chocolatey
$packageName = '{PACKAGE_NAME}'
$url = 'http://SITE.COM/FILE.ZIP'
$subFolderFilter = '*SUBFOLDER*'
try {
$rootDir = Join-Path "$env:systemdrive\" $packageName
if (Test-Path "$rootDir") {
Write-Host "Removing existing files, keeping config-files"
@sebnilsson
sebnilsson / FirstRowText.js
Created September 3, 2013 11:53
Get the first row of text
function firstRowText(text) {
return text.match(/^(.*)$/m)[0] || '';
}
@sebnilsson
sebnilsson / GetAssemblyTypes.cs
Last active December 18, 2015 16:39
Helper to get types assignable from specific type, with reflection.
public static IEnumerable<Type> GetAssemblyTypes<TType>(params Assembly[] assemblies)
{
return GetAssemblyTypes(typeof(TType), assemblies);
}
public static IEnumerable<Type> GetAssemblyTypes(Type rootType, params Assembly[] assemblies)
{
if (assemblies == null || !assemblies.Any())
{
assemblies = new[] { rootType.Assembly };