jquery.my.enumerable.js (3603B)
1 (function ( $ ) { 2 var methods = { 3 // $([1,2,3]).collect(function() { return this * this }) // => [1, 4, 9] 4 collect: function(enumerable, callback) { 5 var result = []; 6 $.each(enumerable, function(index) { 7 result.push(callback.call(this, index)); 8 }); 9 return $(result); 10 }, 11 12 // Added by jspam@free.fr 13 // $('.selector').invoke('height') // => [150, 25, 63] 14 invoke: function (enumerable, method) { 15 var result = []; 16 var args = []; 17 for (i = 2; i < arguments.length; i++) { 18 args.push(arguments[i]); 19 } 20 $.each(enumerable, function(index) { 21 var dollmeth = $(this)[method]; 22 if (dollmeth === undefined) 23 result.push(this[method].apply(this, args)); 24 else 25 result.push(dollmeth.apply($(this), args)); 26 }); 27 return $(result); 28 }, 29 30 // $([1,2,3]).inject(0, function(a) { return a + this }) // => 6 31 inject: function(enumerable, initialValue, callback) { 32 var accumulator = initialValue; 33 34 $.each(enumerable, function (index) { 35 accumulator = callback.call(this, accumulator, index); 36 }); 37 return accumulator; 38 }, 39 40 // $([1,2,3]).select(function() { return this % 2 == 1 }) // => [1, 3] 41 select: function(enumerable, callback) { 42 var result = []; 43 $.each(enumerable, function(index) { 44 if (callback.call(this, index)) 45 result.push(this); 46 }); 47 return $(result); 48 }, 49 50 // $([1,2,3]).reject(function() { return this % 2 == 1 }) // => [2] 51 reject: function(enumerable, callback) { 52 return $.select(enumerable, negate(callback)); 53 }, 54 55 // $([1,2]).any(function() { return this == 1 }) // => true 56 any: function(enumerable, callback) { 57 return $.inject(enumerable, false, function(accumulator, index) { 58 return accumulator || callback.call(this, index); 59 }); 60 }, 61 62 // $([1,1]).any(function() { return this == 1 }) // => true 63 all: function(enumerable, callback) { 64 return $.inject(enumerable, true, function(accumulator, index) { 65 return accumulator && callback.call(this, index); 66 }); 67 }, 68 69 // $([1,2,3]).sum() // => 6 70 sum: function(enumerable) { 71 return $.inject(enumerable, 0, function(accumulator) { 72 return accumulator + this; 73 }); 74 } 75 }; 76 77 var staticFunctions = {}; 78 var iteratorFunctions = {}; 79 $.each( methods, function(name, f){ 80 staticFunctions[name] = makeStaticFunction(f); 81 iteratorFunctions[name] = makeIteratorFunction(staticFunctions[name]); 82 }); 83 $.extend(staticFunctions); 84 $.fn.extend(iteratorFunctions); 85 86 // Private methods 87 function makeStaticFunction(f) { 88 return f; /* function() { 89 if (arguments.length > 1) // The first argument is the enumerable 90 validateCallback(arguments[arguments.length - 1]); 91 92 return f.apply(this, arguments); 93 } */ 94 } 95 96 function makeIteratorFunction(staticFunction) { 97 return function() { 98 // arguments isn't a real array, concat doesn't work 99 // unless you explicitly convert it 100 function toArray() { 101 var result = [] 102 for (var i = 0; i < this.length; i++) 103 result.push(this[i]) 104 return(result) 105 } 106 return staticFunction.apply(this, [this].concat(toArray.apply(arguments))) 107 } 108 } 109 110 function validateCallback(callback) { 111 if (!jQuery.isFunction(callback)) 112 throw("callback needs to be a function, it was: " + callback); 113 } 114 115 function negate(f) { 116 return function() { 117 return !f.apply(this, arguments) 118 } 119 } 120 })( jQuery );