Skip to content

Instantly share code, notes, and snippets.

@corymartin
corymartin / immutabilly.js
Last active August 29, 2015 14:27
Mutates an object hash into an object of getters and setters where the setters - if used - will throw an error.
function immutabilly(props) {
Object.keys(props).forEach(key => {
const prop = props[key];
if (prop === Object(prop)) {
immutabilly(prop);
}
Object.defineProperty(props, key, {
enumerable: true,
get: () => prop,
set: () => {throw new Error(`Illegal assignment to immutable property '${key}'`);},
/*
* childManagement
* ===============
*/
void function() {
var childrenKey = '_children';
/*
* Creates a private collection for child views on the receiver if one
@corymartin
corymartin / timedTest.js
Created November 10, 2014 04:24
Simple test wrapper for testing code speed.
/**
* Test code speed.
*
* timeTest('#forEach() test', 10000, function(count) {
* someArray.forEach(function(){});
* });
*/
function timedTest(name, count, test) {
if (count !== +count) throw new TypeError('count must be a number');
// Make sure we have a uint to avoid infinite loop.
"------------------------"
"PATHOGEN PLUGIN SETTINGS
"------------------------"
call pathogen#runtime_append_all_bundles()
"execute pathogen#infect()
"------------------------"
" My Settings
"------------------------"
@corymartin
corymartin / namespace.js
Last active December 18, 2015 10:39
Creates a namespace based upon a string representation of a namespace. Adapted from a similar function in JavaScript Patterns by Stoyan Stefanov.
/*
* Creates a namespace based upon a string representation of a namespace.
* Adapted from a similar function in JavaScript Patterns by Stoyan Stefanov.
*
* @param {String} ns String representation of namespace. Eg, 'foo.bar.baz.goo'
* @returns {Object} Last object created in the namespace.
*/
void function() {
var root = 'myapp';
@corymartin
corymartin / RenderPartialToString.cs
Created May 16, 2013 15:43
Renders a partial template with it's model and returns the output as a string.
/// <summary>
/// Returns the rendered HTML for a partial view.
/// </summary>
/// <param name="partial">Path to partial template.</param>
/// <param name="model"></param>
/// <returns></returns>
protected string RenderPartialToString(string partial, object model)
{
this.ViewData.Model = model;
using (var sw = new StringWriter())
@corymartin
corymartin / adder.js
Last active December 16, 2015 14:19
Addition function to avoid inaccuracies of floating point addition.
var adder = function(precision) {
precision |= 0;
return function() {
var sum = 0;
var len = arguments.length;
while (len--) sum += (arguments[len] * precision) | 0;
return sum / precision;
};
};
@corymartin
corymartin / range.js
Last active December 16, 2015 00:29
Tiny array range api
/**
* range(2, 6) // [2,3,4,5,6]
* range(5).to(8) // [5,6,7,8]
* range(4).until(10) // [4,5,6,7,8,9]
*/
var range = function(s, e) {
if (e == null) {
return {
to: function(e) {
@corymartin
corymartin / printElement.js
Last active December 11, 2015 23:38
Prints an element and its contents. jQuery free version of Ben Nadel's jQuery plugin. http://www.bennadel.com/blog/1591-Ask-Ben-Print-Part-Of-A-Web-Page-With-jQuery.htm
/**
* Prints an element and its contents.
* jQuery free version of Ben Nadel's jQuery plugin.
* http://www.bennadel.com/blog/1591-Ask-Ben-Print-Part-Of-A-Web-Page-With-jQuery.htm
*
* @param {HTMLElement} el
* @returns {undefined}
*/
function printElement(el) {
var csslinks = document.getElementsByTagName('link');
@corymartin
corymartin / ExampleUsage.cs
Created December 26, 2012 03:02
Extension method: System.Web.Mvc.FormCollection#Fetch
[HttpPost]
public ActionResult Create(FormCollection collection)
{
var username = collection.Fetch("username", "anonymous");
// username will equal "anonymous" if it was not submitted
}
// VERSUS:
[HttpPost]