Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View timhobbs's full-sized avatar

Tim Hobbs timhobbs

View GitHub Profile
@timhobbs
timhobbs / ConvertCsvToList.cs
Created May 24, 2013 19:44
Converts CSV file contents (as IList<string>) to a typed IList<T>
private IList<T> ConvertCsvToList<T>(IList<string> csv, string[] header) {
var list = new List<T>();
foreach (var row in csv) {
var columns = row.Split(',');
T obj = (T)Activator.CreateInstance(typeof(T));
for (int i = 0; i < columns.Length; i++) {
var h = Regex.Match(header[i].Replace("@", "_"), @"(?<="")(?:\\.|[^""\\])*(?="")").Value;
var c = Regex.Match(columns[i], @"(?<="")(?:\\.|[^""\\])*(?="")").Value;
var prop = typeof(Em.Schools.Data.Domain.Match).GetProperty(h);
/// <summary>
/// Reads database schema from query, generates assembly in the memory, and returns dynamic object
/// </summary>
public static System.Collections.IEnumerable DynamicSqlQuery(this Database database, string sql, params object[] parameters)
{
TypeBuilder builder = DynamicMapper.createTypeBuilder(
"MyDynamicAssembly", "MyDynamicModule", "MyDynamicType");
using (System.Data.IDbCommand command = database.Connection.CreateCommand())
{
@timhobbs
timhobbs / Trolliminator! Bookmarklet.js
Last active December 15, 2015 02:49
Trolliminator! eliminates troll posts from Gigya comments. Simply add a comma-separated list of troll usernames to the trolls variable at the top, then save this js snippet as a bookmark. Whenever you want to trolliminate posts, just click the bookmark! All troll posts will be marked with a "Trolliminated!" string and a total count will be displ…
javascript: (function () {
var trolls = [
'troll', // <-- add troll names here
];
var el = document.createElement('div'),
b = document.getElementsByTagName('body')[0];
otherlib = false, msg = '';
el.style.position = 'fixed';
el.style.height = '32px';
@timhobbs
timhobbs / toggle-data.js
Created November 21, 2012 19:20
Select data- attribute on an input
var d = $("input[data-toggle-class]");
$.each(d, function (idx, data) {
console.log(idx, data);
});
@timhobbs
timhobbs / FilterExtension.cs
Created November 16, 2012 01:13
An extension method for Telerik MVC grid filtering
///<remarks>
/// Turns out this wasn't needed - and far less elegant than the existing extension method.
/// However, it is still an example of a generic way to match a given property with a value
/// and how to select that property by name (string) and compare its value to the value
/// that is passed in. It works...
///</remarks>
public static IList<T> ApplyFilter<T>(this IList<T> list, FilterDescriptor filter) {
// We wont allow filtering on anything but string type properties
if (filter.MemberType != typeof(string)) throw new ArgumentException("Filtering is only allowed for properties with a type of 'string'.");
@timhobbs
timhobbs / em.js
Created November 15, 2012 06:02
jQuery "remote" validation
window.Em = window.Em || {};
Em = {
setFocusOutValidation: function (form) {
var s = $.data(form, "validator").settings;
s.onfocusout = function (element) {
if ($(element).val().length > 0) {
$(element).valid();
}
};
@timhobbs
timhobbs / Adapter.js
Created October 16, 2012 00:49 — forked from michaeljacobdavis/Adapter.js
NotEqual Fluent Validation validator with client side validation
(function ($) {
$.validator.addMethod("notequal", function (value, element, param) {
if (param.indexOf("#") == -1) return value != param;
return value != $(param).val();
}, $.validator.messages.notequal);
$.validator.unobtrusive.adapters.add("notequal", ["field"], function (options) {
options.rules["notequal"] = options.params.field;
if (options.message) options.messages["notequal"] = options.message;
});
@timhobbs
timhobbs / jquery.text3d.js
Created November 15, 2011 23:08
jQuery 3d text plug-in
/**
* 3D Text plugin for jQuery
* v1.0
* Creates 3D text using CSS3 text-shadows
*
* By Craig Buckler, @craigbuckler, http://optimalworks.net
*
* As featured on SitePoint.com:
* http://www.sitepoint.com/css3-3d-text-jquery-plugin/
*
@timhobbs
timhobbs / AjaxRedirect.cs
Created October 25, 2011 20:50
AjaxRedirectResult and AjaxRequestExtension
using System;
using System.Web;
using System.Web.Mvc;
/// <summary>
/// See http://stackoverflow.com/questions/1171035/asp-net-mvc-custom-error-handling-application-error-global-asax/5952774#5952774
/// for implementation details
/// </summary>
public class AjaxRedirectResult : RedirectResult {
@timhobbs
timhobbs / MaximumWeightAttribute.js
Created October 22, 2011 19:50
MaximumWeightAttribute custom attribute jQuery unobtrusive client side validation implementation
<script type="text/javascript">
$.validator.addMethod("maximumweight",
function (value, element, parameters) {
var carrier = $("#" + parameters["dependentproperty"]).val();
var carriervalue = parameters["dependentvalue"].toString();
var weightvalue = Number(parameters["weightvalue"]);
if (carrier == carriervalue && value > weightvalue) {
return false;
}
return true;