Skip to content

Instantly share code, notes, and snippets.

Kamran Ayub kamranayub

Block or report user

Report or block kamranayub

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
kamranayub / PrettyJson.cs
Last active Sep 25, 2015
Formats a valid JSON string into a pretty output
View PrettyJson.cs
/// <summary>
/// Formats a JSON string by walking through it and examining the contents.
/// </summary>
/// <param name="json">Unformatted JSON string, expects valid JSON with quoted keys and no whitespace.</param>
/// <returns>Formatted JSON string</returns>
/// <remarks>
/// [ { should have line breaks and tabs after them
/// ] } should have line breaks and tabs before them
/// : should have a space after it
/// , should have a line break and tab
kamranayub / gist:853906
Created Mar 4, 2011
Generates a URL-friendly "slug" from an unsanitized string
View gist:853906
/// <summary>
/// Will transform "some $ugly ###url wit[]h spaces" into "some-ugly-url-with-spaces"
/// </summary>
public static string Slugify(this string phrase, int maxLength = 50)
string str = phrase.ToLower();
// invalid chars, make into spaces
str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
// convert multiple spaces/hyphens into one space
kamranayub / FieldLabelFor.cs
Created Apr 7, 2011
MVC FieldLabelFor helper. Helps generate a better label based entirely off a model's metadata properties.
View FieldLabelFor.cs
/* add using statements */
public static class HtmlExtensions {
/// <summary>
/// Generates a better label.
/// Text based off given labelText, [DisplayName], or property name.
/// If the field is optional ([Required]), adds an (optional) em tag.
/// If the field has a description ([Description]), adds a class="note" span tag.
/// </summary>
/// <typeparam name="TModel"></typeparam>
kamranayub / gist:935461
Created Apr 21, 2011
This ProxyConverter can be used with AutoMapper to help when converting Entity Framework proxied objects (i.e. DynamicProxy_xxxxxxx). Room for improvement. See:
View gist:935461
// See:
// For use with AutoMapper
public class ProxyConverter<TSource, TDestination> : ITypeConverter<TSource, TDestination>
where TSource : class
where TDestination : class
public TDestination Convert(ResolutionContext context)
// Get dynamic proxy base type
kamranayub / iebuttonfix.js
Created May 19, 2011
IE 7 and below <button> tag value on POST fix
View iebuttonfix.js
$(function () {
// IE 7 and below <button> fix
// See:
if ($.browser.msie) {
if ($.browser.version === "7.0" ||
$.browser.version === "6.0") {
$("button[name][value]").live('click', function () {
var $this = $(this),
kamranayub / EnumDropDown.cs
Created Oct 14, 2011
Drop-down for Enum MVC helper
View EnumDropDown.cs
public static MvcHtmlString DropdownForEnum<TModel>(this HtmlHelper<TModel> helper, Type type,
string name, string optionLabel, object htmlAttributes)
if (!type.IsEnum) throw new ArgumentException("type must be that of an enum", "type");
var dictionary = new Dictionary<string, string>();
var values = type.GetEnumValues();
foreach (var val in values)
kamranayub / PerfTestCaseBackingMethods.cs
Created Oct 20, 2011
Performance of reading AppSetting via different backing methods
View PerfTestCaseBackingMethods.cs
using System;
using System.Diagnostics;
namespace PerfBackingFields
class Program
private const int Iterations = 100000;
static void Main(string[] args)
kamranayub / ExcludeDirectorySearch.cs
Created Jan 9, 2012
Cassette ExcludeDirectorySearch
View ExcludeDirectorySearch.cs
/// <summary>
/// An exclude directory search for Cassette. Provide the patterns you want to search for
/// and this will exclude *.min/*-vsdoc files as well as the directories you specify.
/// </summary>
public class ExcludeDirectorySearch : FileSearch
/// <summary>
/// Excludes specified directories in search (also .min and -vsdoc files)
/// </summary>
/// <param name="pattern">File search pattern (wildcards, e.g. *.css;*.less)</param>
kamranayub / Transforms.xml
Created Feb 26, 2012
MSBuild Inline Task to Transform a Hierarchy of XML Files using XDT Transforms
View Transforms.xml
<!-- This task takes in a XDT transform file and transforms it, following any inheritance chain.
There should be at least one base transform for this to work; otherwise just use Microsoft's
regular TransformXml task. -->
TaskDirectory="path/to/directory/of/Microsoft.Web.Publishing.Tasks" />
kamranayub / knockout.compiledTemplateSource.js
Created Mar 21, 2012
Pre-compiled jQuery tmpl support for KO 2.0.0
View knockout.compiledTemplateSource.js
// jQuery.tmpl Compiled Source Plugin for Knockout 2.0
// Kamran Ayub -
// Adds support for referencing named pre-compiled templates
// e.g. $.template('name', 'markup')
// Specifically, this makes Cassette Knockout compiled templates
// work in KO 2.0.0
(function (ko) {
ko.templateSources.compiledTemplateSource = function (name) {
You can’t perform that action at this time.