Monthly Archives: June 2008

assertArgumentType – test expected data types against actual data types

My colleague Andrew Beeching gets credit for this one. An amazingly useful piece of code to check that parameters you are receiving match your expectations. It also allows for null values to be passed, where you might be checking arguments that may or may not exist. It uses jQuery to check for parameters that might be functions and this functionality could be fallible as demonstrated in this article from DHTML Kitchen.

This is especially useful for TDD.

function assertArgumentType(expected, actual, allowNull){
        if (expected !== undefined) {
            actual = actual || null;
        }
        if (expected === Function) {
            if (!$.isFunction(actual)) {
                throw "InvalidArgNotAFunctionException";
            }
            // Match OR object is allowed to be null
        }
        else
            if ((allowNull && actual === null) || (actual.constructor && actual.constructor === expected)) {
                return;
            }
            else {
                switch (expected) {
                    case String:
                        throw ("InvalidArgNotAStringException");
                        break;
                    case Number:
                        throw ("InvalidArgNaNException");
                        break;
                    case Boolean:
                        throw ("InvalidArgNotABooleanException");
                        break;
                    case Array:
                        throw ("InvalidArgNotAnArrayException");
                        break;
                    case Object:
                        throw ("InvalidArgNotAnObjectException");
                        break;
                    case undefined:
                        throw ("InvalidArgUndefinedException");
                        break;
                    case null:
                        throw ("InvalidArgNullException");
                        break;
                }
            }
    }

jquery.metadata.js – in reverse!

Stand back, kids!

This requires jQuery, jquery.metadata.js and jquery.json.js. You will also need to modify jquery.json.js so it surrounds JSON names and values in single quotes rather than doubles, and escapes single quotes, but this is trivial.

To use:

jQuery(selector).setadata({‘name’:'value’});

It will also chain your original selection.

/******************************************************************
Name: setadata
Description: Add metadata into elements - or metadata in reverse
Author: AK
Date: 24th June 2008
Version: 0.2
Dependencies: jQuery, jquery.metadata.js, jquery.json.js
Notes:
******************************************************************/
(function($) {
    $.fn.setadata = function(jsonData) {
        return this.each(function() {
			data = '{}';
			var m = /({.*})/.exec(this.className);
            if (m) {
				data = m[1];
				this.className = $.trim(this.className.replace(data, ''));
			}
			if (data.indexOf('{') < 0) data = "{" + data + "}";
            data = eval("(" + data + ")");
			for (var property in jsonData) {
				data[property] = jsonData[property];
			}
			this.className += ' ' + $.toJSON(data);
		});
    };
})(jQuery);