jquery.my.enumerable.js (3698B)
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 /* Removed by jspam@free.fr : conflicts with $.select() (for input forms) 42 select: function(enumerable, callback) { 43 var result = []; 44 $.each(enumerable, function(index) { 45 if (callback.call(this, index)) 46 result.push(this); 47 }); 48 return $(result); 49 }, */ 50 51 // $([1,2,3]).reject(function() { return this % 2 == 1 }) // => [2] 52 reject: function(enumerable, callback) { 53 return /*$.select*/$.filter(enumerable, negate(callback)); 54 }, 55 56 // $([1,2]).any(function() { return this == 1 }) // => true 57 any: function(enumerable, callback) { 58 return $.inject(enumerable, false, function(accumulator, index) { 59 return accumulator || callback.call(this, index); 60 }); 61 }, 62 63 // $([1,1]).any(function() { return this == 1 }) // => true 64 all: function(enumerable, callback) { 65 return $.inject(enumerable, true, function(accumulator, index) { 66 return accumulator && callback.call(this, index); 67 }); 68 }, 69 70 // $([1,2,3]).sum() // => 6 71 sum: function(enumerable) { 72 return $.inject(enumerable, 0, function(accumulator) { 73 return accumulator + this; 74 }); 75 } 76 }; 77 78 var staticFunctions = {}; 79 var iteratorFunctions = {}; 80 $.each( methods, function(name, f){ 81 staticFunctions[name] = makeStaticFunction(f); 82 iteratorFunctions[name] = makeIteratorFunction(staticFunctions[name]); 83 }); 84 $.extend(staticFunctions); 85 $.fn.extend(iteratorFunctions); 86 87 // Private methods 88 function makeStaticFunction(f) { 89 return f; /* function() { 90 if (arguments.length > 1) // The first argument is the enumerable 91 validateCallback(arguments[arguments.length - 1]); 92 93 return f.apply(this, arguments); 94 } */ 95 } 96 97 function makeIteratorFunction(staticFunction) { 98 return function() { 99 // arguments isn't a real array, concat doesn't work 100 // unless you explicitly convert it 101 function toArray() { 102 var result = [] 103 for (var i = 0; i < this.length; i++) 104 result.push(this[i]) 105 return(result) 106 } 107 return staticFunction.apply(this, [this].concat(toArray.apply(arguments))) 108 } 109 } 110 111 function validateCallback(callback) { 112 if (!jQuery.isFunction(callback)) 113 throw("callback needs to be a function, it was: " + callback); 114 } 115 116 function negate(f) { 117 return function() { 118 return !f.apply(this, arguments) 119 } 120 } 121 })( jQuery );