Skip to content

Instantly share code, notes, and snippets.

View JamieMason's full-sized avatar

Jamie Mason JamieMason

View GitHub Profile
@JamieMason
JamieMason / apache_perms.sh
Created August 23, 2011 10:11
set correct permissions on apache hosted files
find . -type f -exec chmod 644 {} \; && find . -type d -exec chmod 755 {} \;
@JamieMason
JamieMason / gist:1203142
Created September 8, 2011 10:58
Throttle bandwidth on OS X, to test your Web App when run over a slow connection.
# 1. create rule to limit bandwidth to X kbps
sudo ipfw pipe 1 config bw 40KByte/s
# 2. enable rule
sudo ipfw add 1 pipe 1 src-port 80
# 3. When you're finished, remember to delete the rule to restore your internet connection to normal speed
sudo ipfw delete 1
@JamieMason
JamieMason / gist:1237679
Created September 23, 2011 15:34
memoize experiment
function memoizeTailored (fn, params)
{
var cache = (fn.memoize = fn.memoize || {}),
stringifyJson,
sliceArray;
// return original general purpose script if no detail can be provided to cut the memoizer's workload
if (!params || !('args' in params) || params.args < 1 || params.args > 3 || params.usesObjects === true) {
stringifyJson = JSON.stringify;
sliceArray = Array.prototype.slice;
@JamieMason
JamieMason / gist:1237763
Created September 23, 2011 16:07
Log Object prototype chain
javascript:(function(oSubject){var%20key,arr%20=%20[],obj%20=%20{},i;for%20(key%20in%20oSubject){arr.push([key,%20oSubject[key]]);}arr.sort();for%20(i%20=%200;%20i%20<%20arr.length;%20i++){obj[arr[i][0]]%20=%20arr[i][1];}console.log(obj);}%20(eval(prompt(%27Enter%20reference%20to%20Object%27))));void(0);
@JamieMason
JamieMason / httpGetUrl.js
Created November 3, 2011 13:53
Small utility to manage HTTP GET urls and their parameters, using objects (requires underscore.js)
/*global _*/
function httpGetUrl (sUrl)
{
sUrl = new String(sUrl || window.location.href);
var oUrlParams = {}
, aSplitUrl = sUrl.split('?')
, sUrlRoot = aSplitUrl[0]
, sUrlParams = aSplitUrl[1];
@JamieMason
JamieMason / jquery_attr_pattern.js
Created November 10, 2011 13:54
Abstraction of the design pattern behind jQuery.attr (http://api.jquery.com/attr/).
/**
* @param {Function} fAll Delegate for attr();
* @param {Function} fKey Delegate for attr('some_key');
* @param {Function} fCollection Delegate for attr({some_key:'some_value', some_other_key:'some_other_value'});
* @param {Function} fKeyAndValue Delegate for attr('some_key', 'some_value');
* @param {String|Array|Object} accessor A string referring to a key name, an array of key names, or an object of key/value pairs
* @param {Mixed} value A value normally used to set a property to
*/
function attrPattern (fAll, fKey, fCollection, fKeyAndValue, accessor, value)
{
@JamieMason
JamieMason / console.logStack.js
Created December 1, 2011 16:34
console.logStack() -> Log the call stack to the console to see where your bit of code was called from.
if (typeof console == "undefined")
{
window.console = {
log: function() {}
, group: function() {}
, groupEnd: function() {}
, groupCollapsed: function() {}
};
}
@JamieMason
JamieMason / asynchronousChain.js
Created January 4, 2012 11:49
This utility is used for hiding a (normally asynchronous) implementation behind chainable methods, which I find more descriptive and readable.
/**
* This utility extends functionObject and is used for hiding a (normally asynchronous) implementation behind chainable methods.
*
* @augments functionObject
* @see functionObject
* @returns {Function}
*
* @example
*
* // definition
@JamieMason
JamieMason / escapedRx.js
Created January 12, 2012 09:33
Get a RegExp from an unescaped RegExp String
function escapedRx (text, flags)
{
return new RegExp(text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), flags || '');
}
@JamieMason
JamieMason / randomColour.js
Created January 13, 2012 09:06
Random hexadecimal colour generator
function randomColour ()
{
return "#" + Math.random().toString(16).slice(2, 8);
}