Skip to content

Instantly share code, notes, and snippets.

View JamieMason's full-sized avatar
🏖️
On Holiday until 26 May

Jamie Mason JamieMason

🏖️
On Holiday until 26 May
View GitHub Profile
@JamieMason
JamieMason / encapsulate.js
Created April 4, 2011 14:01
Encapsulates an object and exposes each of it's properties with their own getter and setter, which maintains the original data type. Methods can be overridden to validate properties more strictly if needed.
var encapsulate = (function()
{
var rGetFirstChar = /^([a-z])/;
function isNumber (value)
{
return !isNaN(parseFloat(value)) && typeof value !== 'string';
}
@JamieMason
JamieMason / FAO_Steve.js
Created April 11, 2011 14:46
Proof of concept to extend objects and maintain protected data
// A simple class creation library.
// Inspired by base2 and Prototype
(function(){
var initializing = false,
// Determine if functions can be serialized
fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
@JamieMason
JamieMason / keyboard-listener.js
Created May 2, 2011 10:36
Rough snippet of a page-wide key press listener
function _keyEventWasDataEntry (e)
{
var key = e.charCode,
keychar = String.fromCharCode(key);
// Return false if key was;
// 9 tab
// 8 backspace
// 16 shift
@JamieMason
JamieMason / functionQueue.js
Created June 1, 2011 14:46
A very basic object which takes an array of functions and calls them in sequence at defined intervals. I don't imagine a lot of other uses for it, but it was used to save time on a repetitive task on a UI where I wanted to post incremental data to a form
function functionQueue (aFunctions, nSecondsInterval)
{
var _queue = [],
_arguments = [],
i,
oSelf = this;
nSecondsInterval = nSecondsInterval * 1000;
@JamieMason
JamieMason / log_api_to_console.bookmarklet.txt
Created June 16, 2011 09:10
A Bookmarklet/Favelet to log to the console every member of an Object and it's prototype chain, flattened and sorted into alphabetical order.
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 / Object.join.js
Created June 21, 2011 15:57
Apply similar behaviour to Array.join to an Object
Object.prototype.join = function (separator, template)
{
var key
, output = []
, length = 0
, self = this
, tKey = '{key}'
, tValue = '{value}';
separator = separator || ',';
@JamieMason
JamieMason / AsyncGroup.js
Created July 5, 2011 11:59
Merge multiple asynchronous functions into a group, notifying you via callback when every request in the group has completed.
function AsyncGroup ()
{
this.members = [];
this.callbacks = [];
AsyncGroup.statics.init.apply(this, arguments);
}
// statics used for utils to keep the instance API clear
AsyncGroup.statics = {
@JamieMason
JamieMason / average.js
Created July 28, 2011 09:29
Using underscore.js, return the average value from an array of Numbers.
function average (arr)
{
return _.reduce(arr, function(memo, num)
{
return memo + num;
}, 0) / arr.length;
}
@JamieMason
JamieMason / partial.js
Last active September 26, 2015 14:28
Simple partial application without binding
function partial(fn) {
var fixed = [].slice.call(arguments, 1);
return function() {
return fn.apply(this, fixed.concat([].slice.call(arguments, 0)));
};
}
@JamieMason
JamieMason / write.php
Created August 10, 2011 09:42
Rough logger that writes out each argument on a new line in HTML
function write ()
{
foreach (func_get_args() as $value) {
echo $value . '<br />\n\r';
};
}
write('this', 'is', 'a', 'quick', 'and', 'dirty', 'logger');