| 1 |
define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/array", "./lambda"], |
| 2 |
function(kernel, lang, arr, df){ |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
var empty = {}; |
| 18 |
|
| 19 |
lang.mixin(df, { |
| 20 |
|
| 21 |
|
| 22 |
filter: function( a, f, o){ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
if(typeof a == "string"){ a = a.split(""); } |
| 27 |
o = o || kernel.global; f = df.lambda(f); |
| 28 |
var t = [], v, i, n; |
| 29 |
if(lang.isArray(a)){ |
| 30 |
|
| 31 |
for(i = 0, n = a.length; i < n; ++i){ |
| 32 |
v = a[i]; |
| 33 |
if(f.call(o, v, i, a)){ t.push(v); } |
| 34 |
} |
| 35 |
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
| 36 |
|
| 37 |
for(i = 0; a.hasNext();){ |
| 38 |
v = a.next(); |
| 39 |
if(f.call(o, v, i++, a)){ t.push(v); } |
| 40 |
} |
| 41 |
}else{ |
| 42 |
|
| 43 |
for(i in a){ |
| 44 |
if(!(i in empty)){ |
| 45 |
v = a[i]; |
| 46 |
if(f.call(o, v, i, a)){ t.push(v); } |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
return t; |
| 51 |
}, |
| 52 |
forEach: function( a, f, o){ |
| 53 |
|
| 54 |
|
| 55 |
if(typeof a == "string"){ a = a.split(""); } |
| 56 |
o = o || kernel.global; f = df.lambda(f); |
| 57 |
var i, n; |
| 58 |
if(lang.isArray(a)){ |
| 59 |
|
| 60 |
for(i = 0, n = a.length; i < n; f.call(o, a[i], i, a), ++i); |
| 61 |
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
| 62 |
|
| 63 |
for(i = 0; a.hasNext(); f.call(o, a.next(), i++, a)); |
| 64 |
}else{ |
| 65 |
|
| 66 |
for(i in a){ |
| 67 |
if(!(i in empty)){ |
| 68 |
f.call(o, a[i], i, a); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
return o; |
| 73 |
}, |
| 74 |
map: function( a, f, o){ |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
if(typeof a == "string"){ a = a.split(""); } |
| 79 |
o = o || kernel.global; f = df.lambda(f); |
| 80 |
var t, n, i; |
| 81 |
if(lang.isArray(a)){ |
| 82 |
|
| 83 |
t = new Array(n = a.length); |
| 84 |
for(i = 0; i < n; t[i] = f.call(o, a[i], i, a), ++i); |
| 85 |
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
| 86 |
|
| 87 |
t = []; |
| 88 |
for(i = 0; a.hasNext(); t.push(f.call(o, a.next(), i++, a))); |
| 89 |
}else{ |
| 90 |
|
| 91 |
t = []; |
| 92 |
for(i in a){ |
| 93 |
if(!(i in empty)){ |
| 94 |
t.push(f.call(o, a[i], i, a)); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
return t; |
| 99 |
}, |
| 100 |
every: function( a, f, o){ |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if(typeof a == "string"){ a = a.split(""); } |
| 105 |
o = o || kernel.global; f = df.lambda(f); |
| 106 |
var i, n; |
| 107 |
if(lang.isArray(a)){ |
| 108 |
|
| 109 |
for(i = 0, n = a.length; i < n; ++i){ |
| 110 |
if(!f.call(o, a[i], i, a)){ |
| 111 |
return false; |
| 112 |
} |
| 113 |
} |
| 114 |
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
| 115 |
|
| 116 |
for(i = 0; a.hasNext();){ |
| 117 |
if(!f.call(o, a.next(), i++, a)){ |
| 118 |
return false; |
| 119 |
} |
| 120 |
} |
| 121 |
}else{ |
| 122 |
|
| 123 |
for(i in a){ |
| 124 |
if(!(i in empty)){ |
| 125 |
if(!f.call(o, a[i], i, a)){ |
| 126 |
return false; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
return true; |
| 132 |
}, |
| 133 |
some: function( a, f, o){ |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if(typeof a == "string"){ a = a.split(""); } |
| 138 |
o = o || kernel.global; f = df.lambda(f); |
| 139 |
var i, n; |
| 140 |
if(lang.isArray(a)){ |
| 141 |
|
| 142 |
for(i = 0, n = a.length; i < n; ++i){ |
| 143 |
if(f.call(o, a[i], i, a)){ |
| 144 |
return true; |
| 145 |
} |
| 146 |
} |
| 147 |
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
| 148 |
|
| 149 |
for(i = 0; a.hasNext();){ |
| 150 |
if(f.call(o, a.next(), i++, a)){ |
| 151 |
return true; |
| 152 |
} |
| 153 |
} |
| 154 |
}else{ |
| 155 |
|
| 156 |
for(i in a){ |
| 157 |
if(!(i in empty)){ |
| 158 |
if(f.call(o, a[i], i, a)){ |
| 159 |
return true; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
return false; |
| 165 |
} |
| 166 |
}); |
| 167 |
|
| 168 |
return df; |
| 169 |
}); |