Skip to content

Instantly share code, notes, and snippets.

'use strict';
var gameOfLife;
var NAMESPACE = {
// helper data structure - might not be most efficient but works
Infinite2dBooleanArray: function() {
var cells = new Array();
this.getValue = function(x, y) {
if (cells[x]) {
return cells[x][y] || false;
internal static System.Web.HttpContext FakeHttpContext()
{
var httpRequest = new System.Web.HttpRequest("", "http://localhost/", "");
var stringWriter = new System.IO.StringWriter();
var httpResponse = new System.Web.HttpResponse(stringWriter);
var httpContext = new System.Web.HttpContext(httpRequest, httpResponse);
var sessionContainer = new System.Web.SessionState.HttpSessionStateContainer(
"id", new System.Web.SessionState.SessionStateItemCollection(),
new System.Web.HttpStaticObjectsCollection(), 10, true,
@timiles
timiles / ObjectFromNameValueCollectionConverter.cs
Created December 16, 2014 18:46
ObjectFromNameValueCollectionConverter - useful for eg ConfigurationManager.AppSettings
public class ObjectFromNameValueCollectionConverter
{
public static T CreateFromNameValueCollection<T>(NameValueCollection nameValueCollection, string keyPrefix = "")
where T : new()
{
var target = new T();
Type type = target.GetType();
foreach (var key in nameValueCollection.AllKeys.Where(x => x.StartsWith(keyPrefix)))
{
var propertyName = key.Substring(keyPrefix.Length);
@timiles
timiles / AppSettingsReader.cs
Last active August 29, 2015 14:15
Parse keys from config into IDictionary
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
internal static class AppSettingsReader
{
/// <summary>
@timiles
timiles / gist:2828976
Created May 29, 2012 15:15
RenderRazorViewToString extension
// credit: http://stackoverflow.com/a/2759898/487544
public static string RenderRazorViewToString(this Controller controller, string viewName, object model)
{
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);
@timiles
timiles / gist:2829059
Created May 29, 2012 15:27
nice css snippets
/*-- js/nojs with Modernizr --*/
html.js .js-hide, .no-js-hide
{
display: none;
}
html.js .no-js-hide
{
display: inherit;
}
@timiles
timiles / gist:2829104
Created May 29, 2012 15:32
nice js snippets
/*-- to detect mobile size in js --*/
/* credit: http://bricss.net/post/22198838298/easily-checking-in-javascript-if-a-css-media-query-has */
function isMobileSize() {
var size = window.getComputedStyle(document.body, ':after').getPropertyValue('content');
return size.indexOf('mobile') != -1;
}
/*
@media (max-width: 767px)
{
body:after
@timiles
timiles / gist:2835527
Created May 30, 2012 11:02
eg ModelState.Try("model.Email", () => user.ChangeEmail(newEmail));
// credit: @mcintyre321
public static class ModelStateDictionaryExtensions
{
public static bool Try(this ModelStateDictionary ms, string key, Action action)
{
try
{
action();
return true;
}
@timiles
timiles / gist:2851789
Created June 1, 2012 12:28
Url.AbsoluteAction helper
public static string AbsoluteAction(this UrlHelper url,
string actionName, string controllerName, object routeValues = null, bool isHttps = false)
{
return url.Action(actionName, controllerName, routeValues, "http" + (isHttps ? "s" : ""));
}
@timiles
timiles / gist:2851684
Created June 1, 2012 12:13
MenuActionLink HtmlHelper Extensions
/// <summary>
/// adds the active class if the link's action & controller matches current request
/// </summary>
public static MvcHtmlString MenuActionLink(this HtmlHelper htmlHelper,
string linkText, string actionName, string controllerName,
object routeValues = null, object htmlAttributes = null,
string activeClassName = "active")
{
IDictionary<string, object> htmlAttributesDictionary =
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);