Ticket #243: mochikit_rpartial.patch
| File mochikit_rpartial.patch, 9.0 KB (added by morten.barklund@…, 3 years ago) |
|---|
-
MochiKit/Base.js
598 598 }; 599 599 }, 600 600 601 /** @id MochiKit.Base. bind */602 bind: function (func, self/* args... */) {601 /** @id MochiKit.Base._bind */ 602 _bind: function (func, self, preargs, postargs) { 603 603 if (typeof(func) == "string") { 604 604 func = self[func]; 605 605 } 606 606 var im_func = func.im_func; 607 607 var im_preargs = func.im_preargs; 608 var im_postargs = func.im_postargs; 608 609 var im_self = func.im_self; 609 610 var m = MochiKit.Base; 610 611 if (typeof(func) == "function" && typeof(func.apply) == "undefined") { … … 624 625 } else { 625 626 im_preargs = im_preargs.slice(); 626 627 } 627 m.extend(im_preargs, arguments, 2); 628 im_preargs = m.concat(im_preargs, preargs); 629 if (typeof(im_postargs) == 'undefined') { 630 im_postargs = []; 631 } else { 632 im_postargs = im_postargs.slice(); 633 } 634 im_postargs = m.concat(im_postargs, postargs); 628 635 var newfunc = function () { 629 636 var args = arguments; 630 637 var me = arguments.callee; 631 638 if (me.im_preargs.length > 0) { 632 639 args = m.concat(me.im_preargs, args); 633 640 } 641 if (me.im_postargs.length > 0) { 642 args = m.concat(args, me.im_postargs); 643 } 634 644 var self = me.im_self; 635 645 if (!self) { 636 646 self = this; … … 640 650 newfunc.im_self = im_self; 641 651 newfunc.im_func = im_func; 642 652 newfunc.im_preargs = im_preargs; 653 newfunc.im_postargs = im_postargs; 643 654 return newfunc; 644 655 }, 645 656 657 /** @id MochiKit.Base.bind */ 658 bind: function (func, self/* args... */) { 659 var m = MochiKit.Base; 660 return m._bind(func, self, m.extend(null,arguments,2), []); 661 }, 662 663 /** @id MochiKit.Base.rbind */ 664 rbind: function (func, self/* args... */) { 665 var m = MochiKit.Base; 666 return m._bind(func, self, [], m.extend(null,arguments,2)); 667 }, 668 646 669 /** @id MochiKit.Base.bindMethods */ 647 670 bindMethods: function (self) { 648 671 var bind = MochiKit.Base.bind; … … 941 964 return m.bind.apply(this, m.extend([func, undefined], arguments, 1)); 942 965 }, 943 966 967 /** @id MochiKit.Base.rpartial */ 968 rpartial: function (func) { 969 var m = MochiKit.Base; 970 return m.rbind.apply(this, m.extend([func, undefined], arguments, 1)); 971 }, 972 944 973 /** @id MochiKit.Base.listMinMax */ 945 974 listMinMax: function (which, lst) { 946 975 if (lst.length === 0) { … … 1243 1272 "methodcaller", 1244 1273 "compose", 1245 1274 "bind", 1275 "rbind", 1246 1276 "bindMethods", 1247 1277 "NotFound", 1248 1278 "AdapterRegistry", … … 1256 1286 "keyComparator", 1257 1287 "reverseKeyComparator", 1258 1288 "partial", 1289 "rpartial", 1259 1290 "merge", 1260 1291 "listMinMax", 1261 1292 "listMax", -
MochiKit/DOM.js
397 397 /** @id MochiKit.DOM.getNodeAttribute */ 398 398 getNodeAttribute: function (node, attr) { 399 399 var self = MochiKit.DOM; 400 var base = MochiKit.Base; 401 if (base.isArrayLike(node)) 402 return base.map(base.rpartial(self.getNodeAttribute, attr), node); 403 if (base.isArrayLike(attr)) 404 return base.map(base.partial(self.getNodeAttribute, node), attr); 405 node = self.getElement(node); 400 406 var rename = self.attributeArray.renames[attr]; 401 node = self.getElement(node);402 407 try { 403 408 if (rename) { 404 409 return node[rename]; … … 413 418 /** @id MochiKit.DOM.removeNodeAttribute */ 414 419 removeNodeAttribute: function (node, attr) { 415 420 var self = MochiKit.DOM; 421 var base = MochiKit.Base; 422 if (base.isArrayLike(node)) 423 return base.map(base.rpartial(self.removeNodeAttribute, attr), node); 424 if (base.isArrayLike(attr)) 425 return base.map(base.partial(self.removeNodeAttribute, node), attr); 416 426 var rename = self.attributeArray.renames[attr]; 417 427 node = self.getElement(node); 418 428 try { … … 430 440 updateNodeAttributes: function (node, attrs) { 431 441 var elem = node; 432 442 var self = MochiKit.DOM; 443 var base = MochiKit.Base; 444 if (base.isArrayLike(node)) 445 return base.map(base.rpartial(self.updateNodeAttributes, attrs), node); 433 446 if (typeof(node) == 'string') { 434 447 elem = self.getElement(node); 435 448 } … … 662 675 /** @id MochiKit.DOM.getElement */ 663 676 getElement: function (id) { 664 677 var self = MochiKit.DOM; 678 var base = MochiKit.Base; 665 679 if (arguments.length == 1) { 680 if (base.isArrayLike(id)) 681 return base.map(self.getElement, id); 666 682 return ((typeof(id) == "string") ? 667 683 self._document.getElementById(id) : id); 668 684 } else { 669 return MochiKit.Base.map(self.getElement, arguments);685 return base.map(self.getElement, arguments); 670 686 } 671 687 }, 672 688 … … 764 780 /** @id MochiKit.DOM.setElementClass */ 765 781 setElementClass: function (element, className) { 766 782 var self = MochiKit.DOM; 783 var base = MochiKit.Base; 784 if (base.isArrayLike(element)) 785 return base.map(base.rpartial(self.setElementClass, className), element); 786 if (base.isArrayLike(className)) 787 return base.map(base.partial(self.setElementClass, element), className); 767 788 var obj = self.getElement(element); 768 789 if (self.attributeArray.compliant) { 769 790 obj.setAttribute("class", className); … … 786 807 /** @id MochiKit.DOM.addElementClass */ 787 808 addElementClass: function (element, className) { 788 809 var self = MochiKit.DOM; 810 var base = MochiKit.Base; 811 if (base.isArrayLike(element)) 812 return base.map(base.rpartial(self.addElementClass, className), element); 813 if (base.isArrayLike(className)) 814 return base.map(base.partial(self.addElementClass, element), className); 789 815 var obj = self.getElement(element); 790 816 var cls = obj.className; 791 817 // trivial case, no className yet -
tests/test_Base.js
113 113 t.is( compare([2, 1], [1, 1]), 1, "arrays compare gt (contents)" ); 114 114 115 115 // test partial application 116 var a = [];117 116 var func = function (a, b) { 118 117 if (arguments.length != 2) { 119 118 return "bad args"; … … 123 122 }; 124 123 var self = {"value": 1, "func": func}; 125 124 var self2 = {"value": 2}; 126 t.is( self.func(2, 3), 6, "setup for test is correct" );125 t.is( self.func(2, 3), 6, "setup for partial test is correct" ); 127 126 self.funcTwo = partial(self.func, 2); 128 127 t.is( self.funcTwo(3), 6, "partial application works" ); 129 128 t.is( self.funcTwo(3), 6, "partial application works still" ); … … 131 130 self.funcTwo = bind(bind(self.funcTwo, self2), null); 132 131 t.is( self.funcTwo(3), 6, "re-unbinding partial application works" ); 133 132 134 133 134 // test rpartial application 135 var func = function (a, b) { 136 if (arguments.length != 2) { 137 return "bad args"; 138 } else { 139 return this.value + a + b; 140 } 141 }; 142 var self = {"value": 1, "func": func}; 143 var self2 = {"value": 2}; 144 t.is( self.func(2, 3), 6, "setup for rpartial test is correct" ); 145 self.funcRThree = rpartial(self.func, 3); 146 t.is( self.funcRThree(2), 6, "rpartial application works" ); 147 t.is( self.funcRThree(2), 6, "rpartial application works still" ); 148 t.is( bind(self.funcRThree, self2)(2), 7, "rebinding rpartial works" ); 149 self.funcBetween = bind(bind(self.funcRThree, self2), null); 150 t.is( self.funcRThree(2), 6, "re-unbinding rpartial application works" ); 151 152 153 // test partial and rpartial together 154 var func = function (a, b, c) { 155 if (arguments.length != 3) { 156 return "bad args"; 157 } else { 158 return this.value + a + b + c; 159 } 160 }; 161 var self = {"value": 1, "func": func}; 162 var self2 = {"value": 2}; 163 t.is( self.func(2, 3, 4), 10, "setup for partial-rpartial test is correct" ); 164 self.funcBetween = rpartial(partial(self.func, 2), 4); 165 t.is( self.funcBetween(3), 10, "partial-rpartial application works" ); 166 t.is( self.funcBetween(3), 10, "partial-rpartial application works still" ); 167 self.funcBetween = partial(rpartial(self.func, 4), 2); 168 t.is( self.funcBetween(3), 10, "rpartial-partial application works" ); 169 t.is( self.funcBetween(3), 10, "rpartial-partial application works still" ); 170 t.is( bind(self.funcBetween, self2)(3), 11, "rebinding partial-rpartial works" ); 171 self.funcBetween = bind(bind(self.funcBetween, self2), null); 172 t.is( self.funcBetween(3), 10, "re-unbinding partial-rpartial application works" ); 173 135 174 // nodeWalk test 136 175 // ... looks a lot like a DOM tree on purpose 137 176 var tree = {
