[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/include/prototype-1.4.0/src/ -> enumerable.js (source)

   1  var $break    = new Object();
   2  var $continue = new Object();
   3  
   4  var Enumerable = {
   5    each: function(iterator) {
   6      var index = 0;
   7      try {
   8        this._each(function(value) {
   9          try {
  10            iterator(value, index++);
  11          } catch (e) {
  12            if (e != $continue) throw e;
  13          }
  14        });
  15      } catch (e) {
  16        if (e != $break) throw e;
  17      }
  18    },
  19    
  20    all: function(iterator) {
  21      var result = true;
  22      this.each(function(value, index) {
  23        result = result && !!(iterator || Prototype.K)(value, index);
  24        if (!result) throw $break;
  25      });
  26      return result;
  27    },
  28    
  29    any: function(iterator) {
  30      var result = true;
  31      this.each(function(value, index) {
  32        if (result = !!(iterator || Prototype.K)(value, index)) 
  33          throw $break;
  34      });
  35      return result;
  36    },
  37    
  38    collect: function(iterator) {
  39      var results = [];
  40      this.each(function(value, index) {
  41        results.push(iterator(value, index));
  42      });
  43      return results;
  44    },
  45    
  46    detect: function (iterator) {
  47      var result;
  48      this.each(function(value, index) {
  49        if (iterator(value, index)) {
  50          result = value;
  51          throw $break;
  52        }
  53      });
  54      return result;
  55    },
  56    
  57    findAll: function(iterator) {
  58      var results = [];
  59      this.each(function(value, index) {
  60        if (iterator(value, index))
  61          results.push(value);
  62      });
  63      return results;
  64    },
  65    
  66    grep: function(pattern, iterator) {
  67      var results = [];
  68      this.each(function(value, index) {
  69        var stringValue = value.toString();
  70        if (stringValue.match(pattern))
  71          results.push((iterator || Prototype.K)(value, index));
  72      })
  73      return results;
  74    },
  75    
  76    include: function(object) {
  77      var found = false;
  78      this.each(function(value) {
  79        if (value == object) {
  80          found = true;
  81          throw $break;
  82        }
  83      });
  84      return found;
  85    },
  86    
  87    inject: function(memo, iterator) {
  88      this.each(function(value, index) {
  89        memo = iterator(memo, value, index);
  90      });
  91      return memo;
  92    },
  93    
  94    invoke: function(method) {
  95      var args = $A(arguments).slice(1);
  96      return this.collect(function(value) {
  97        return value[method].apply(value, args);
  98      });
  99    },
 100    
 101    max: function(iterator) {
 102      var result;
 103      this.each(function(value, index) {
 104        value = (iterator || Prototype.K)(value, index);
 105        if (value >= (result || value))
 106          result = value;
 107      });
 108      return result;
 109    },
 110    
 111    min: function(iterator) {
 112      var result;
 113      this.each(function(value, index) {
 114        value = (iterator || Prototype.K)(value, index);
 115        if (value <= (result || value))
 116          result = value;
 117      });
 118      return result;
 119    },
 120    
 121    partition: function(iterator) {
 122      var trues = [], falses = [];
 123      this.each(function(value, index) {
 124        ((iterator || Prototype.K)(value, index) ? 
 125          trues : falses).push(value);
 126      });
 127      return [trues, falses];
 128    },
 129    
 130    pluck: function(property) {
 131      var results = [];
 132      this.each(function(value, index) {
 133        results.push(value[property]);
 134      });
 135      return results;
 136    },
 137    
 138    reject: function(iterator) {
 139      var results = [];
 140      this.each(function(value, index) {
 141        if (!iterator(value, index))
 142          results.push(value);
 143      });
 144      return results;
 145    },
 146    
 147    sortBy: function(iterator) {
 148      return this.collect(function(value, index) {
 149        return {value: value, criteria: iterator(value, index)};
 150      }).sort(function(left, right) {
 151        var a = left.criteria, b = right.criteria;
 152        return a < b ? -1 : a > b ? 1 : 0;
 153      }).pluck('value');
 154    },
 155    
 156    toArray: function() {
 157      return this.collect(Prototype.K);
 158    },
 159    
 160    zip: function() {
 161      var iterator = Prototype.K, args = $A(arguments);
 162      if (typeof args.last() == 'function')
 163        iterator = args.pop();
 164  
 165      var collections = [this].concat(args).map($A);
 166      return this.map(function(value, index) {
 167        iterator(value = collections.pluck(index));
 168        return value;
 169      });
 170    },
 171    
 172    inspect: function() {
 173      return '#<Enumerable:' + this.toArray().inspect() + '>';
 174    }
 175  }
 176  
 177  Object.extend(Enumerable, {
 178    map:     Enumerable.collect,
 179    find:    Enumerable.detect,
 180    select:  Enumerable.findAll,
 181    member:  Enumerable.include,
 182    entries: Enumerable.toArray
 183  });


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7