Equivelency

by kflorence

HTML

<div id="equivalent"></div>

JavaScript

$.extend({
    // Test for equality any JavaScript type.
    // Discussions and reference: http://philrathe.com/articles/equiv
    // Test suites: http://philrathe.com/tests/equiv
    // Author: Philippe RathĂ© <[email protected]>
    equivalent: function() {
        var innerEquiv; // the real equiv function
        var callers = []; // stack to decide between skip/abort functions
        var parents = []; // stack to avoiding loops from circular referencing
        // Call the o related callback with the given arguments.


        function bindCallbacks(o, callbacks, args) {
            var prop = $.type(o);
            if (prop) {
                if ($.type(callbacks[prop]) === "function") {
                    return callbacks[prop].apply(callbacks, args);
                } else {
                    return callbacks[prop]; // or undefined
                }
            }
        }

        var callbacks = function() {
            // for string, boolean, number and null


            function useStrictEquality(b, a) {
                if (b instanceof a.constructor || a instanceof b.constructor) {
                    // to catch short annotaion VS 'new' annotation of a declaration
                    // e.g. var i = 1;
                    //      var j = new Number(1);
                    return a == b;
                } else {
                    return a === b;
                }
            }

            return {
                "string": useStrictEquality,
                "boolean": useStrictEquality,
                "number": useStrictEquality,
                "null": useStrictEquality,
                "undefined": useStrictEquality,

                "nan": function(b) {
                    return isNaN(b);
                },

                "date": function(b, a) {
                    return $.type(b) === "date" && a.valueOf() === b.valueOf();
                },

                "regexp": function(b, a) {
                    return $.type(b) === "regexp"...