Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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';
"------------------------"
"PATHOGEN PLUGIN SETTINGS
"------------------------"
call pathogen#runtime_append_all_bundles()
"execute pathogen#infect()
"------------------------"
" My Settings
"------------------------"
@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.
/*
* childManagement
* ===============
*/
void function() {
var childrenKey = '_children';
/*
* Creates a private collection for child views on the receiver if one
@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}'`);},