Base.js 27.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
/*

MochiKit.Base 0.5

See <http://mochikit.com/> for documentation, downloads, license, etc.

(c) 2005 Bob Ippolito.  All rights Reserved.

*/

if (typeof(dojo) != 'undefined') {
    dojo.provide("MochiKit.Base");
}

if (typeof(MochiKit) == 'undefined') {
    MochiKit = {};
}
if (typeof(MochiKit.Base) == 'undefined') {
    MochiKit.Base = {};
}

MochiKit.Base.VERSION = "0.5";
MochiKit.Base.NAME = "MochiKit.Base"
MochiKit.Base.__repr__ = function () {
    return "[" + this.NAME + " " + this.VERSION + "]";
}
MochiKit.Base.toString = function () {
    return this.__repr__();
}

MochiKit.Base.clone = function (obj) {
    var me = arguments.callee;
    if (arguments.length == 1) {
        me.prototype = obj;
        return new me();
    }
};
        
MochiKit.Base.extend = function (self, obj, /* optional */skip) {
    /***

        Mutate an array by extending it with an array-like obj,
        starting with the "skip" index of obj.  If null is given
        as the initial array, a new one will be created.

        This mutates *and returns* the given array, be warned.

    ***/
    
    // Extend an array with an array-like object starting
    // from the skip index
    if (!skip) {
        skip = 0;
    }
    if (obj) {
        // allow iterable fall-through, but skip the full isArrayLike
        // check for speed, this is called often.
        var l = obj.length;
        if (typeof(l) != 'number' /* !isArrayLike(obj) */) {
            if (MochiKit.Iter) {
                obj = MochiKit.Iter.list(obj);
                l = obj.length;
            } else {
                throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
            }
        }
        if (!self) {
            self = [];
        }
        for (var i = skip; i < l; i++) {
            self.push(obj[i]);
        }
    }
    // This mutates, but it's convenient to return because
    // it's often used like a constructor when turning some
    // ghetto array-like to a real array
    return self;
};


MochiKit.Base.update = function (self, obj/*, ... */) {
    /***

        Mutate an object by replacing its key:value pairs with those
        from other object(s).  Key:value pairs from later objects will
        overwrite those from earlier objects.
        
        If null is given as the initial object, a new one will be created.

        This mutates *and returns* the given object, be warned.

        A version of this function that creates a new object is available
        as merge(o1, o2, ...)

    ***/
    if (self == null) {
        self = {};
    }
    for (var i = 1; i < arguments.length; i++) {
        var o = arguments[i];
        if (typeof(o) != 'undefined' && o != null) {
            for (var k in o) {
                self[k] = o[k];
            }
        }
    }
    return self;
};

MochiKit.Base.setdefault = function (self, obj/*, ...*/) {
    /***

        Mutate an object by replacing its key:value pairs with those
        from other object(s) IF they are not already set on the initial
        object.
        
        If null is given as the initial object, a new one will be created.

        This mutates *and returns* the given object, be warned.

    ***/
    if (self == null) {
        self = {};
    }
    for (var i = 1; i < arguments.length; i++) {
        var o = arguments[i];
        for (var k in o) {
            if (!(k in self)) {
                self[k] = o[k];
            }
        }
    }
    return self;
};

MochiKit.Base.keys = function (obj) {
    /***

        Return an array of the property names of an object
        (in no particular order).

    ***/
    var rval = [];
    for (var prop in obj) {
        rval.push(prop);
    }
    return rval;
};
    
MochiKit.Base.items = function (obj) {
    /***

        Return an array of [propertyName, propertyValue] pairs for an
        object (in no particular order).

    ***/
    var rval = [];
    for (var prop in obj) {
        rval.push([prop, obj[prop]]);
    }
    return rval;
};

MochiKit.Base.NamedError = function (name) {
    this.message = name;
    this.name = name;
};
MochiKit.Base.NamedError.prototype = new Error();
MochiKit.Base.NamedError.prototype.repr = function () {
    if (this.message && this.message != this.name) {
        return this.name + "(" + repr(this.message) + ")";
    } else {
        return this.name + "()";
    }
}
MochiKit.Base.NamedError.prototype.toString = function () {
    return this.repr();
}

MochiKit.Base.operator = {
    /***

        A table of JavaScript's operators for usage with map, filter, etc.

    ***/

    // unary logic operators
    "truth": function (a) { return !!a; }, 
    "lognot": function (a) { return !a; },
    "identity": function (a) { return a; },

    // bitwise unary operators
    "not": function (a) { return ~a; },
    "neg": function (a) { return -a; },

    // binary operators
    "add": function (a, b) { return a + b; },
    "div": function (a, b) { return a / b; },
    "mod": function (a, b) { return a % b; },

    // bitwise binary operators
    "and": function (a, b) { return a & b; },
    "or": function (a, b) { return a | b; },
    "xor": function (a, b) { return a ^ b; },
    "lshift": function (a, b) { return a << b; },
    "rshift": function (a, b) { return a >> b; },
    "zrshift": function (a, b) { return a >>> b; },

    // near-worthless build-in comparators
    "eq": function (a, b) { return a == b; },
    "ne": function (a, b) { return a != b; },
    "gt": function (a, b) { return a > b; },
    "ge": function (a, b) { return a >= b; },
    "lt": function (a, b) { return a < b; },
    "le": function (a, b) { return a <= b; },

    // compare comparators
    "ceq": function (a, b) { return MochiKit.Base.compare(a, b) == 0; },
    "cne": function (a, b) { return MochiKit.Base.compare(a, b) != 0; },
    "cgt": function (a, b) { return MochiKit.Base.compare(a, b) == 1; },
    "cge": function (a, b) { return MochiKit.Base.compare(a, b) != -1; },
    "clt": function (a, b) { return MochiKit.Base.compare(a, b) == -1; },
    "cle": function (a, b) { return MochiKit.Base.compare(a, b) != 1; },

    // binary logical operators
    "logand": function (a, b) { return a && b; },
    "logor": function (a, b) { return a || b; },
    "contains": function (a, b) { return b in a; }
};

MochiKit.Base.forward = function (func) {
    /***

    Returns a function that forwards a method call to this.func(...)

    ***/
    return function () {
        return this[func].apply(this, arguments);
    };
};

MochiKit.Base.itemgetter = function (func) {
    /***

    Returns a function that returns arg[func]

    ***/
    return function (arg) {
        return arg[func];
    };
};

MochiKit.Base.typeMatcher = function (/* typ */) {
    /***

    Given a set of types (as string arguments),
    returns a function that will return true if the types of the given
    objects are members of that set.

    ***/
    
    var types = {};
    for (var i = 0; i < arguments.length; i++) {
        var typ = arguments[i];
        types[typ] = typ;
    }
    return function () { 
        for (var i = 0; i < arguments.length; i++) {
            if (!(typeof(arguments[i]) in types)) {
                return false;
            }
        }
        return true;
    };
};

MochiKit.Base.isNull = function (/* ... */) {
    /***

    Returns true if all arguments are null.

    ***/
    for (var i = 0; i < arguments.length; i++) {
        if (arguments[i] != null) {
            return false;
        }
    }
    return true;
}

MochiKit.Base.isUndefinedOrNull = function (/* ... */) {
    /***

        Returns true if all arguments are undefined or null

    ***/
    for (var i = 0; i < arguments.length; i++) {
        var o = arguments[i];
        if (!(typeof(o) == 'undefined' || o == null)) {
            return false;
        }
    }
    return true;
};

MochiKit.Base.isNotEmpty = function (obj) {
    /***

        Returns true if all the array or string arguments
        are not empty

    ***/
    for (var i = 0; i < arguments.length; i++) {
        var o = arguments[i];
        if (!(o && o.length)) {
            return false;
        }
    }
    return true;
};

MochiKit.Base.isArrayLike = function () {
    /***

        Returns true if all given arguments are Array-like

    ***/
    for (var i = 0; i < arguments.length; i++) {
        var o = arguments[i];
        var typ = typeof(o);
        if (
            (typ != 'object' && !(typ == 'function' && typeof(o.item) == 'function')) ||
            o == null ||
            typeof(o.length) != 'number'
        ) {
            return false;
        }
    }
    return true;
};

MochiKit.Base.isDateLike = function () {
    /***

        Returns true if all given arguments are Date-like

    ***/
    for (var i = 0; i < arguments.length; i++) {
        var o = arguments[i];
        if (typeof(o) != "object" || typeof(o.getTime) != 'function') {
            return false;
        }
    }
    return true;
};


MochiKit.Base.xmap = function (fn/*, obj... */) {
    /***
    
        Return an array composed of fn(obj) for every obj given as an
        argument.

        If fn is null, operator.identity is used.

    ***/
    if (fn == null) {
        return MochiKit.Base.extend(null, arguments, 1);
    }
    var rval = [];
    for (var i = 1; i < arguments.length; i++) {
        rval.push(fn(arguments[i]));
    }
    return rval;
};

MochiKit.Base.map = function (fn, lst/*, lst... */) {
    /***

        Return a new array composed of the results of fn(x) for every x in
        lst

        If fn is null, and only one sequence argument is given the identity
        function is used.
        
            map(null, lst) -> lst.slice();

        If fn is null, and more than one sequence is given as arguments,
        then the Array function is used, making it equivalent to zip.

            map(null, p, q, ...)
                -> zip(p, q, ...)
                -> [[p0, q0, ...], [p1, q1, ...], ...];

    ***/
    var isArrayLike = MochiKit.Base.isArrayLike;
    if (arguments.length <= 2) {
        // allow an iterable to be passed
        if (!isArrayLike(lst)) {
            if (MochiKit.Iter) {
                // fast path for map(null, iterable)
                lst = MochiKit.Iter.list(lst);
                if (fn == null) {
                    return lst;
                }
            } else {
                throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
            }
        }
        // fast path for map(null, lst)
        if (fn == null) {
            return MochiKit.Base.extend(null, lst);
        }
        // fast path for map(fn, lst)
        if (typeof(Array.prototype.map) == 'function') {
            // Mozilla fast-path
            return Array.prototype.map.call(lst, fn);
        }
        var rval = [];
        for (var i = 0; i < lst.length; i++) {
            rval.push(fn(lst[i]));
        }
        return rval;
    } else {
        // default for map(null, ...) is zip(...)
        if (fn == null) {
            fn = Array;
        }
        var length = null;
        for (var i = 1; i < arguments.length; i++) {
            // allow iterables to be passed
            if (!isArrayLike(arguments[i])) {
                if (MochiKit.Iter) {
                    arguments[i] = MochiKit.Iter.list(arguments[i]);
                } else {
                    throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
                }
            }
            // find the minimum length
            var l = arguments[i].length;
            if (length == null || length > l) {
                length = l;
            }
        }
        var rval = [];
        for (var i = 0; i < length; i++) {
            var args = [];
            for (var j = 1; j < arguments.length; j++) {
                args.push(arguments[j][i]);
            }
            rval.push(fn.apply(this, args));
        }
        return rval;
    }
};

MochiKit.Base.xfilter = function (fn/*, obj... */) {
    var rval = [];
    if (fn == null) {
        fn = MochiKit.Base.operator.truth;
    }
    for (var i = 1; i < arguments.length; i++) {
        var o = arguments[i];
        if (fn(o)) {
            rval.push(o);
        }
    }
    return rval;
};

MochiKit.Base.filter = function (fn, lst, self) {
    var rval = [];
    // allow an iterable to be passed
    if (!MochiKit.Base.isArrayLike(lst)) {
        if (MochiKit.Iter) {
            lst = MochiKit.Iter.list(lst);
        } else {
            throw new TypeError("Argument not an array-like and MochiKit.Iter not present");
        }
    }
    if (fn == null) {
        fn = MochiKit.Base.operator.truth;
    }
    if (typeof(Array.prototype.filter) == 'function') {
        // Mozilla fast-path
        return Array.prototype.filter.call(lst, fn, self);
    }
    else if (typeof(self) == 'undefined' || self == null) {
        for (var i = 0; i < lst.length; i++) {
            var o = lst[i];
            if (fn(o)) {
                rval.push(o);
            }
        }
    } else {
        for (var i = 0; i < lst.length; i++) {
            var o = lst[i];
            if (fn.call(self, o)) {
                rval.push(o);
            }
        }
    }
    return rval;
};


MochiKit.Base.bind = function (func, self/* args... */) {
    var im_func = func.im_func;
    var im_preargs = func.im_preargs;
    var im_self = func.im_self;
    if (typeof(im_func) != 'function') {
        im_func = func;
    }
    if (typeof(self) != 'undefined') {
        im_self = self;
    }
    if (typeof(im_preargs) == 'undefined') {
        im_preargs = [];
    } else  {
        im_preargs = im_preargs.slice();
    }
    MochiKit.Base.extend(im_preargs, arguments, 2);
    var newfunc = function () {
        var args = arguments;
        var me = arguments.callee;
        if (me.im_preargs.length > 0) {
            args = MochiKit.Base.concat(me.im_preargs, args);
        }
        var self = me.im_self;
        if (!self) {
            self = this;
        }
        return me.im_func.apply(self, args);
    };
    newfunc.im_self = im_self;
    newfunc.im_func = im_func;
    newfunc.im_preargs = im_preargs;
    return newfunc;
};

MochiKit.Base.bindMethods = function (self) {
    /***

        Bind all functions in self to self,
        which gives you a semi-Pythonic sort of instance.

    ***/
    for (var k in self) {
        var func = self[k];
        if (typeof(func) == 'function') {
            self[k] = bind(func, self);
        }
    }
};

// A singleton raised when no suitable adapter is found
MochiKit.Base.NotFound = new MochiKit.Base.NamedError("MochiKit.Base.NotFound");

MochiKit.Base.AdapterRegistry = function () {
    /***

        A registry to facilitate adaptation.

        Pairs is an array of [name, check, wrap] triples
        
        All check/wrap functions in this registry should be of the same arity.

    ***/
    this.pairs = [];
};

MochiKit.Base.AdapterRegistry.prototype.register = function (name, check, wrap, /* optional */ override) {
    /***
        
        The check function should return true if the given arguments are
        appropriate for the wrap function.

        If override is given and true, the check function will be given
        highest priority.  Otherwise, it will be the lowest priority
        adapter.

    ***/

    if (override) {
        this.pairs.unshift([name, check, wrap]);
    } else {
        this.pairs.push([name, check, wrap]);
    }
};

MochiKit.Base.AdapterRegistry.prototype.match = function (/* ... */) {
    /***

        Find an adapter for the given arguments.
        
        If no suitable adapter is found, throws NotFound.

    ***/
    for (var i = 0; i < this.pairs.length; i++) {
        var pair = this.pairs[i];
        if (pair[1].apply(this, arguments)) {
            return pair[2].apply(this, arguments);
        }
    }
    throw MochiKit.Base.NotFound;
};

MochiKit.Base.AdapterRegistry.prototype.unregister = function (name) {
    /***

        Remove a named adapter from the registry

    ***/
    for (var i = 0; i < this.pairs.length; i++) {
        var pair = this.pairs[i];
        if (pair[0] == name) {
            this.pairs.splice(i, 1);
            return true;
        }
    }
    return false;
};

MochiKit.Base.registerComparator = function (name, check, comparator, /* optional */ override) {
    /***

        Register a comparator for use with the compare function.

        name should be a unique identifier describing the comparator.

        check is a function (a, b) that returns true if a and b
        can be compared with comparator.

        comparator is a function (a, b) that returns:

             0 when a == b
             1 when a > b
            -1 when a < b

        comparator is guaranteed to only be called if check(a, b)
        returns a true value.

        If override is given and true, then it will be made the
        highest precedence comparator.  Otherwise, the lowest.

    ***/
    MochiKit.Base.comparatorRegistry.register(name, check, comparator, override);
};

MochiKit.Base.compare = function (a, b) {
    /***

        Compare two objects in a sensible manner.  Currently this is:
        
            1. undefined and null compare equal to each other
            2. undefined and null are less than anything else
            3. comparators registered with registerComparator are
               used to find a good comparator.  Built-in comparators
               are currently available for arrays and dates.
            4. Otherwise hope that the built-in comparison operators
               do something useful, which should work for numbers
               and strings.

        Returns what one would expect from a comparison function.

        returns:

             0 when a == b
             1 when a > b 
            -1 when a < b

    ***/
    if (a == b) {
        return 0;
    }
    var aIsNull = (typeof(a) == 'undefined' || a == null);
    var bIsNull = (typeof(b) == 'undefined' || b == null);
    if (aIsNull && bIsNull) {
        return 0;
    } else if (aIsNull) {
        return -1;
    } else if (bIsNull) {
        return 1;
    }
    try {
        return MochiKit.Base.comparatorRegistry.match(a, b);
    } catch (e) {
        if (e != MochiKit.Base.NotFound) {
            throw e;
        }
        if (a < b) {
            return -1;
        } else if (a > b) {
            return 1;
        }
        // These types can't be compared
        var repr = MochiKit.Base.repr;
        throw new TypeError(repr(a) + " and " + repr(b) + " can not be compared");
    }
};

MochiKit.Base.compareDateLike = function (a, b) {
    a = a.getTime();
    b = b.getTime();
    if (a == b) {
        return 0;
    } else if (a < b) {
        return -1;
    } else if (a > b) {
        return 1;
    }
    var repr = MochiKit.Base.repr;
    throw new TypeError(repr(a) + " and " + repr(b) + " can not be compared");
};

MochiKit.Base.compareArrayLike = function (a, b) {
    var compare = MochiKit.Base.compare;
    var count = a.length;
    var rval = 0;
    if (count > b.length) {
        rval = 1;
    } else if (count < b.length) {
        rval = -1;
        count = b.length;
    }
    for (var i = 0; i < count; i++) {
        var cmp = compare(a[i], b[i]);
        if (cmp) {
            return cmp;
        }
    }
    return rval;
};

MochiKit.Base.registerRepr = function (name, check, wrap, /* optional */override) {
    /***

        Register a repr function.  repr functions should take
        one argument and return a string representation of it
        suitable for developers, primarily used when debugging.

        If override is given, it is used as the highest priority
        repr, otherwise it will be used as the lowest.

    ***/
    MochiKit.Base.reprRegistry.register(name, check, wrap, override);
};

MochiKit.Base.repr = function (o) {
    /***

        Return a "programmer representation" for an object

    ***/

    try {
        if (typeof(o.__repr__) == 'function') {
            return o.__repr__();
        } else if (typeof(o.repr) == 'function' && o.repr != arguments.callee) {
            return o.repr();
        }
        return MochiKit.Base.reprRegistry.match(o);
    } catch (e) {
        if (typeof(o.NAME) == 'string' && (
                o.toString == Function.prototype.toString ||
                o.toString == Object.prototype.toString
            )) {
            return o.NAME;
        }
        return o;
    }
};

MochiKit.Base.reprArrayLike = function (o) {
    return "[" + MochiKit.Base.map(MochiKit.Base.repr, o).join(", ") + "]";
};

MochiKit.Base.reprString = function (o) { 
    o = '"' + o.replace(/(["\\])/g, '\\$1') + '"';
    return o.replace(/(\n)/g, "\\n");
};

MochiKit.Base.reprNumber = function (o) {
    return o.toString();
};

MochiKit.Base.reprUndefined = function (o) {
    return "undefined";
};

MochiKit.Base.reprNull = function (o) {
    return "null";
};

MochiKit.Base.objEqual = function (a, b) {
    /***
        
        Compare the equality of two objects.

    ***/
    return (MochiKit.Base.compare(a, b) == 0);
};

MochiKit.Base.arrayEqual = function (self, arr) {
    /***

        Compare two arrays for equality, with a fast-path for length
        differences.

    ***/
    if (self.length != arr.length) {
        return false;
    }
    return (MochiKit.Base.compare(self, arr) == 0);
};

MochiKit.Base.concat = function (/* lst... */) {
    /***

        Concatenates all given array-like arguments and returns
        a new array:

            var lst = concat(["1","3","5"], ["2","4","6"]);
            assert(lst.toString() == "1,3,5,2,4,6");

    ***/
    var rval = [];
    var extend = MochiKit.Base.extend;
    for (var i = 0; i < arguments.length; i++) {
        extend(rval, arguments[i]);
    }
    return rval;
};

MochiKit.Base.keyComparator = function (key/* ... */) {
    /***

        A comparator factory that compares a[key] with b[key].
        e.g.:

            var lst = ["a", "bbb", "cc"];
            lst.sort(keyComparator("length"));
            assert(lst.toString() == "a,cc,bbb");

    ***/
    // fast-path for single key comparisons
    var compare = MochiKit.Base.compare;
    if (arguments.length == 1) {
        return function (a, b) {
            return compare(a[key], b[key]);
        }
    }
    var compareKeys = MochiKit.Base.extend(null, arguments);
    return function (a, b) {
        var rval = 0;
        // keep comparing until something is inequal or we run out of
        // keys to compare
        for (var i = 0; (rval == 0) && (i < compareKeys.length); i++) {
            var key = compareKeys[i];
            rval = compare(a[key], b[key]);
        }
        return rval;
    };
};

MochiKit.Base.reverseKeyComparator = function (key) {
    /***

        A comparator factory that compares a[key] with b[key] in reverse.
        e.g.:

            var lst = ["a", "bbb", "cc"];
            lst.sort(reverseKeyComparator("length"));
            assert(lst.toString() == "bbb,cc,aa");

    ***/
    var comparator = MochiKit.Base.keyComparator.apply(this, arguments);
    return function (a, b) {
        return comparator(b, a);
    }
};

MochiKit.Base.partial = function (func) {
    return MochiKit.Base.bind.apply(this, MochiKit.Base.extend([func, undefined], arguments, 1));
};
 
MochiKit.Base.listMinMax = function (which, lst) {
    /***

        If which == -1 then it will return the smallest
        element of the array-like lst.  This is also available
        as:

            listMin(lst)


        If which == 1 then it will return the largest
        element of the array-like lst.  This is also available
        as:
            
            listMax(list)

    ***/
    if (lst.length == 0) {
        return null;
    }
    var cur = lst[0];
    var compare = MochiKit.Base.compare;
    for (var i = 1; i < lst.length; i++) {
        var o = lst[i];
        if (compare(o, cur) == which) {
            cur = o;
        }
    }
    return cur;
};

MochiKit.Base.objMax = function (/* obj... */) {
    /***
    
        Return the maximum object out of the given arguments

    ***/
    return MochiKit.Base.listMinMax(1, arguments);
};
        
MochiKit.Base.objMin = function (/* obj... */) {
    /***

        Return the minimum object out of the given arguments

    ***/
    return MochiKit.Base.listMinMax(-1, arguments);
};

MochiKit.Base.nodeWalk = function (node, visitor) {
    /***

        Non-recursive generic node walking function (e.g. for a DOM)

        @param node: The initial node to be searched.

        @param visitor: The visitor function, will be called as
                        visitor(node), and should return an Array-like
                        of notes to be searched next (e.g.
                        node.childNodes).

    ***/
    var nodes = [node];
    var extend = MochiKit.Base.extend;
    while (nodes.length) {
        var res = visitor(nodes.shift());
        if (res) {
            extend(nodes, res);
        }
    }
};

   
MochiKit.Base.nameFunctions = function (namespace) {
    var base = namespace.NAME;
    if (typeof(base) == 'undefined') {
        base = '';
    } else {
        base = base + '.';
    }
    for (var name in namespace) {
        var o = namespace[name];
        if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') {
            try {
                o.NAME = base + name;
            } catch (e) {
                // pass
            }
        }
    }
}

MochiKit.Base.EXPORT = [
    "clone",
    "extend",
    "update",
    "setdefault",
    "keys",
    "items",
    "NamedError",
    "operator",
    "forward",
    "itemgetter",
    "typeMatcher",
    "isCallable",
    "isUndefined",
    "isUndefinedOrNull",
    "isNull",
    "isNotEmpty",
    "isArrayLike",
    "isDateLike",
    "xmap",
    "map",
    "xfilter",
    "filter",
    "bind",
    "bindMethods",
    "NotFound",
    "AdapterRegistry",
    "registerComparator",
    "compare",
    "registerRepr",
    "repr",
    "objEqual",
    "arrayEqual",
    "concat",
    "keyComparator",
    "reverseKeyComparator",
    "partial",
    "merge",
    "listMinMax",
    "listMax",
    "listMin",
    "objMax",
    "objMin",
    "nodeWalk",
    "zip"
];

MochiKit.Base.EXPORT_OK = [
    "nameFunctions",
    "comparatorRegistry",
    "reprRegistry",
    "compareDateLike",
    "compareArrayLike",
    "reprArrayLike",
    "reprString",
    "reprNumber",
    "reprUndefined",
    "reprNull"
];


MochiKit.Base.__new__ = function () {

    this.listMax = this.partial(this.listMinMax, 1);
    this.listMin = this.partial(this.listMinMax, -1);

    this.isCallable = this.typeMatcher('function');
    this.isUndefined = this.typeMatcher('undefined');

    this.merge = this.partial(this.update, null);
    this.zip = this.partial(this.map, null);

    this.comparatorRegistry = new this.AdapterRegistry();
    this.registerComparator("dateLike", this.isDateLike, this.compareDateLike);
    this.registerComparator("arrayLike", this.isArrayLike, this.compareArrayLike);

    this.reprRegistry = new this.AdapterRegistry();
    this.registerRepr("arrayLike", this.isArrayLike, this.reprArrayLike);
    this.registerRepr("string", this.typeMatcher("string"), this.reprString);
    this.registerRepr("numbers", this.typeMatcher("number", "boolean"), this.reprNumber);
    this.registerRepr("undefined", this.isUndefined, this.reprUndefined);
    this.registerRepr("null", this.isNull, this.reprNull);

    var all = this.concat(this.EXPORT, this.EXPORT_OK);
    this.EXPORT_TAGS = {
        ":common": this.concat(this.EXPORT_OK),
        ":all": all
    };

    this.nameFunctions(this);

};

MochiKit.Base.__new__();

//
// XXX: Internet Explorer blows
//
compare = MochiKit.Base.compare;


if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
    || (typeof(__MochiKit_Compat__) == 'boolean' && __MochiKit_Compat__)) {
        (function (self) {
            var all = self.EXPORT_TAGS[":all"];
            for (var i = 0; i < all.length; i++) {
                this[all[i]] = self[all[i]];
            }
        })(MochiKit.Base);
}