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);

Bookmarklet to de-cache CSS and images

Save the following as a bookmarklet to dynamically get the latest versions of CSS and images, regardless of caching. This version doesn’t do CSS background images, though.


javascript:(function(){var i,j,x;x=document.getElementsByTagName('link');for(i=0,j=x.length;i<j;i++){x[i].href=x[i].href+'?'+new Date().getTime();}x=document.getElementsByTagName('img');for(i=0,j=x.length;i<j;i++){x[i].href=x[i].href+'?'+new Date().getTime();}})();

CSS to Change Highlighted Text Style in Mozilla

moz-selection!

e.g.:

*::-moz-selection {
background:#FF2E1D none repeat scroll 0%;
color:#FFFFFF;
}

Working example:

http://www.uniqlo.co.uk/stores

jQuery plugin to emulate “shake” on login failure in OSX login box

Ridiculous, but still:

jQuery.fn.shake = function(intShakes /*Amount of shakes*/, intDistance /*Shake distance*/, intDuration /*Time duration*/) {
  this.each(function() {
    $(this).css({position:'relative'});
    for (var x=1; x<=intShakes; x++) {
      $(this).animate({left:(intDistance*-1)}, (((intDuration/intShakes)/4)))
      .animate({left:intDistance}, ((intDuration/intShakes)/2))
      .animate({left:0}, (((intDuration/intShakes)/4)));
    }
  });
  return this;
};
//example
$(function() {
  $('#btn').click(function() {
    $(this).shake(3, 6, 180);
  });
});