quickie: jQuery array comparison helper

I was looking for an answer to this question today an ended up answering it myself.

jQuery.extend({
    arrayCompare: function (arrayA, arrayB) {
        if (arrayA.length != arrayB.length) { return false; }
        // sort modifies original array
        // (which are passed by reference to our method!)
        // so clone the arrays before sorting
        var a = jQuery.extend(true, [], arrayA);
        var b = jQuery.extend(true, [], arrayB);
        a.sort(); 
        b.sort();
        for (var i = 0, l = a.length; i < l; i++) {
            if (a[i] !== b[i]) { 
                return false;
            }
        }
        return true;
    }
});

var a = [1, 2, 3];
var b = [2, 3, 4];
var c = [3, 4, 2];

jQuery.arrayCompare(a, b);
// false

jQuery.arrayCompare(b, c);
// true

// c is still unsorted [3, 4, 2]
Advertisements

free jQuery plugin: ‘dataPanel’ flexible tooltip

I was looking around recently for a jQuery tooltip plugin. I wanted a bit more flexibility in styling the content of the tooltip than the ones I found though… basically I wanted more than just ‘title’ and ‘description’.

More of an informational panel than a little tooltip, hence ‘dataPanel’.

In the one I made you can have an arbitrary number of fields within the tooltip div – you just pass the plugin a css selector for each one when you attach it to your source element.

Read more of this post