Skip to content

Instantly share code, notes, and snippets.

View JamieMason's full-sized avatar
💤
On a break from OSS for a couple of weeks

Jamie Mason JamieMason

💤
On a break from OSS for a couple of weeks
View GitHub Profile
@JamieMason
JamieMason / createGetterSetter.js
Created December 15, 2010 14:23
Allows you to create a jQuery style (like $('a').attr(), $('a').css()) getter and setter function for any variable or property you choose. When a property name of your subject is provided, it's returned. If a 2nd argument is passed as well, the named prop
/**
* Allows you to create a jQuery style (like .attr(), .css()) getter and setter function for any variable or property you choose.
* When a property name of your subject is provided, it's returned. if a 2nd argument is passed as well, your subject is set to that value
*
* @param property The variable or property your getter/setter will operate on
* @return [chainTo] {Object} Optionally return the object the subject is a property of, to allow chaining
* @author https://github.com/JamieMason
*/
function createGetterSetter(subject, chainTo)
{
@JamieMason
JamieMason / mergeTemplateWithData.js
Created December 15, 2010 14:48
really simple form of templating in JavaScript, this function replaces named tokens in strings with the values of Object properties of the same name. When an Array is passed, the template is repeated for every object in the Array.
/**
* A really simple form of templating, this function replaces named tokens in strings with the values of Object properties of the same name.
* When an Array is passed, the template is repeated for every object in the Array.
*
* @author https://github.com/JamieMason
* @param {String} template A string containing various ${someVar}, ${someOtherVar} tokens to be found and replaced
* @param {Object} data A value object whose properties' values eg: .someVar, .someOtherVar will be merged with the supplied template. If an Array is passed, the template will be applied to each Object in the Array.
* @return A copy of the template merged with your data, in those places where the property names of your data matched the named tokens in the template.
* @type String
*/
@JamieMason
JamieMason / PluginSniffer.js
Created December 15, 2010 15:09
Provides Cross Browser Plugin Detection for Quicktime, Flash, Windows Media, Acrobat, Java, etc... or whatever you extend it to. Basically a cleaned up copy of http://www.knallgrau.at/code/plugin_js version 0.5 from 2006.
/*global ActiveXObject*/
/**
* Provides Cross Browser Plugin Detection for Quicktime, Flash, Windows Media, Acrobat, Java, etc... or whatever you extend it to. Modified version of http://www.knallgrau.at/code/plugin_js version 0.5
* @author https://github.com/JamieMason
* @constructor
*/
function PluginSniffer()
{
var self = this; // Reference this instance, whatever it might be called instead of Plugin
@JamieMason
JamieMason / getRandomNumber.js
Created December 15, 2010 15:10
Returns a random number between a specified range, optionally set to a fixed number of decimal places
/**
* Returns a random number between a specified range, optionally set to a fixed number of decimal places
*
* @author https://github.com/JamieMason
* @param {Number} minimumValue The lowest value the random number can be
* @param {Number} maximumValue The highest value the random number can be
* @param {Number} decimalPlaces Optional value to set the number of decimal places
* @type Number
*/
function getRandomNumber(minimumValue, maximumValue, decimalPlaces)
@JamieMason
JamieMason / BrowserChrome.js
Created December 15, 2010 15:38
Mini-library for getting information on the Browser Chrome and display properties of the client machine. Provides the methods getAvailableScreenHeight, getAvailableScreenWidth, getColourDepth, getScrollBarThickness, getViewPortHeight, getViewPortWidth, ge
/**
* Mini-library for getting information on the Browser Chrome and display properties of the client machine. Provides the methods getAvailableScreenHeight, getAvailableScreenWidth, getColourDepth, getScrollBarThickness, getViewPortHeight, getViewPortWidth, getWindowHeight, getWindowLeft, getWindowTop, getWindowWidth
*
* @author http://github.com/JamieMason
* @constructor
*/
function BrowserChrome()
{
/**
@JamieMason
JamieMason / alignTemplate.js
Created December 15, 2010 15:52
Performs a find/replace on the contents of a generic view template. eg: A generic template should have quite reusable names which describe the content in respect to the view it's marking up, rather than the data that's being merged into it. Like ${heading
/**
* Performs a find/replace on the contents of a generic view template. eg: A generic template should have quite reusable names which describe the content in respect to the view it's marking up, rather than the data that's being merged into it. Like ${heading} rather than ${brandName} for example. The same view template might be used in different places with different data.
*
* @author https://github.com/JamieMason
* @param {String} templateContents The template to find/replace on, eg: '<div><strong>${boldText}</strong> ${regularText} (${sideNote})</div>'
* @return A new template with renamed tokens which match the properties of the model you plan to use it against
* @type String
*/
function alignTemplate(templateContents, bindings)
{
@JamieMason
JamieMason / oProtected_example.js
Created December 16, 2010 12:13
The examples of how to share protected data within an inheritance chain in JavaScript using Douglas Crockford's Functional Inheritance Pattern I didn't find particularily clear. I use this example below to remind myself how I perceive it works.
var oSuperclass = function (spec, oProtected)
{
oProtected = oProtected || {};
var that = {};
oProtected.supMethod = function()
{
console.log('superclass says: ', spec.jamie);
};
@JamieMason
JamieMason / isNumber.js
Created December 20, 2010 16:13
Assert value is Number
/**
* Determine whether the supplied value's datatype is Number.
* Odd problems can occur eg: "1"+"6" is "16" not 7.
* @param {Mixed} value Could be a true number: 123, 12.00, or eg: "123", "12.00"
* @return {Boolean} Whether value's type is Number, eg: "123" would be false whereas 123 would be true.
*/
function isNum(data)
{
return !isNaN(parseFloat(data)) && typeof data !== 'string';
}
@JamieMason
JamieMason / createNamespace.js
Created December 21, 2010 15:05
A JavaScript function to create namespaces, avoiding the global namespace and keeping any existing or already defined namespaces intact..
function createNamespace(namespaceString, scope)
{
// eg: 'company.section.product.model.version' => ['company', 'section', 'product', 'model', 'version']
var nsNames = namespaceString.split(/[:\.]/g),
// store this to save looking it up on every pass of the loop
nsNamesLength = nsNames.length,
// an optional object to apply the namespace to, eg: myObj.company.* if set, otherwise window.company.*
refs = [scope || window],
@JamieMason
JamieMason / simpleData.js
Created December 28, 2010 17:43
Note to self for the format of modules in JamieMVC
var simpleData = function (data)
{
var that = {};
// Decorators should always hold a simpleData instance, as you can nest instances
// we do checking here rather than in every decorator
if (typeof data.set === 'function')
{
return data;
}