Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6655

Show
Ignore:
Timestamp:
05/03/07 17:51:04 (1 year ago)
Author:
mislav
Message:

Prototype: useful Function methods from [6626]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spinoffs/prototype/branches/event/src/array.js

    r6173 r6655  
    1 var $A = Array.from = function(iterable) { 
     1function $A(iterable) { 
    22  if (!iterable) return []; 
    33  if (iterable.toArray) { 
     
    1010  } 
    1111} 
     12 
     13Array.from = $A; 
    1214 
    1315Object.extend(Array.prototype, Enumerable); 
  • spinoffs/prototype/branches/event/src/base.js

    r6614 r6655  
    4848}); 
    4949 
    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))); 
     50Object.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    } 
    5484  } 
    55 } 
     85}); 
    5686 
    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 
     87Function.prototype.defer = Function.prototype.delay.curry(0.01); 
    6388 
    6489Object.extend(Number.prototype, { 
  • spinoffs/prototype/branches/event/test/unit/base.html

    r6173 r6655  
    8080    }}, 
    8181 
     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 
    82133    testObjectInspect: function() { with(this) { 
    83134      assertEqual('undefined', Object.inspect());