Changeset 6655
- Timestamp:
- 05/03/07 17:51:04 (1 year ago)
- Files:
-
- spinoffs/prototype/branches/event/src/array.js (modified) (2 diffs)
- spinoffs/prototype/branches/event/src/base.js (modified) (1 diff)
- spinoffs/prototype/branches/event/test/unit/base.html (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
spinoffs/prototype/branches/event/src/array.js
r6173 r6655 1 var $A = Array.from = function(iterable) {1 function $A(iterable) { 2 2 if (!iterable) return []; 3 3 if (iterable.toArray) { … … 10 10 } 11 11 } 12 13 Array.from = $A; 12 14 13 15 Object.extend(Array.prototype, Enumerable); spinoffs/prototype/branches/event/src/base.js
r6614 r6655 48 48 }); 49 49 50 Function.prototype.bind = function() { 51 var __method = this, args = $A(arguments), object = args.shift(); 52 return function() { 53 return __method.apply(object, args.concat($A(arguments))); 50 Object.extend(Function.prototype, { 51 bind: function() { 52 var __method = this, args = $A(arguments), object = args.shift(); 53 return function() { 54 return __method.apply(object, args.concat($A(arguments))); 55 } 56 }, 57 58 bindAsEventListener: function() { 59 var __method = this, args = $A(arguments), object = args.shift(); 60 return function(event) { 61 return __method.apply(object, [event || window.event].concat(args)); 62 } 63 }, 64 65 curry: function() { 66 var __method = this, args = $A(arguments); 67 return function() { 68 return __method.apply(this, args.concat($A(arguments))); 69 } 70 }, 71 72 delay: function() { 73 var __method = this, args = $A(arguments), timeout = args.shift() * 1000; 74 return window.setTimeout(function() { 75 return __method.apply(__method, args); 76 }, timeout); 77 }, 78 79 wrap: function(wrapper) { 80 var __method = this; 81 return function() { 82 return wrapper.apply(this, [__method.bind(this)].concat($A(arguments))); 83 } 54 84 } 55 } 85 }); 56 86 57 Function.prototype.bindAsEventListener = function() { 58 var __method = this, args = $A(arguments), object = args.shift(); 59 return function(event) { 60 return __method.apply(object, [event || window.event].concat(args)); 61 } 62 } 87 Function.prototype.defer = Function.prototype.delay.curry(0.01); 63 88 64 89 Object.extend(Number.prototype, { spinoffs/prototype/branches/event/test/unit/base.html
r6173 r6655 80 80 }}, 81 81 82 testFunctionCurry: function() { with(this) { 83 var split = function(delimiter, string) { return string.split(delimiter); }; 84 var splitOnColons = split.curry(":"); 85 assertEnumEqual(split(":", "0:1:2:3:4:5"), splitOnColons("0:1:2:3:4:5")); 86 }}, 87 88 testFunctionDelay: function() { with(this) { 89 window.delayed = undefined; 90 var delayedFunction = function() { window.delayed = true; }; 91 var delayedFunctionWithArgs = function() { window.delayedWithArgs = $A(arguments).join(' '); }; 92 delayedFunction.delay(0.8); 93 delayedFunctionWithArgs.delay(0.8, 'hello', 'world'); 94 assertUndefined(window.delayed); 95 wait(1000, function() { 96 assert(window.delayed); 97 assertEqual('hello world', window.delayedWithArgs); 98 }); 99 }}, 100 101 testFunctionWrap: function() { with(this) { 102 function sayHello(){ 103 return 'hello world'; 104 }; 105 106 assertEqual('HELLO WORLD', sayHello.wrap(function(proceed) { 107 return proceed().toUpperCase(); 108 })()); 109 110 var temp = String.prototype.capitalize; 111 String.prototype.capitalize = String.prototype.capitalize.wrap(function(proceed, eachWord) { 112 if(eachWord && this.include(' ')) return this.split(' ').map(function(str){ 113 return str.capitalize(); 114 }).join(' '); 115 return proceed(); 116 }); 117 assertEqual('Hello world', 'hello world'.capitalize()); 118 assertEqual('Hello World', 'hello world'.capitalize(true)); 119 assertEqual('Hello', 'hello'.capitalize()); 120 String.prototype.capitalize = temp; 121 }}, 122 123 testFunctionDefer: function() { with(this) { 124 window.deferred = undefined; 125 var deferredFunction = function() { window.deferred = true; }; 126 deferredFunction.defer(); 127 assertUndefined(window.deferred); 128 wait(50, function() { 129 assert(window.deferred); 130 }); 131 }}, 132 82 133 testObjectInspect: function() { with(this) { 83 134 assertEqual('undefined', Object.inspect());