]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/_base/array.js.uncompressed.js
1 define("dojo/_base/array", ["./kernel", "../has", "./lang"], function(dojo
, has
, lang
){
5 // our old simple function builder stuff
9 return cache
[fn
] = new Function("item", "index", "array", fn
); // Function
11 // magic snippet: if(typeof fn == "string") fn = cache[fn] || buildFn(fn);
15 function everyOrSome(some
){
17 return function(a
, fn
, o
){
18 var i
= 0, l
= a
&& a
.length
|| 0, result
;
19 if(l
&& typeof a
== "string") a
= a
.split("");
20 if(typeof fn
== "string") fn
= cache
[fn
] || buildFn(fn
);
23 result
= !fn
.call(o
, a
[i
], i
, a
);
30 result
= !fn(a
[i
], i
, a
);
36 return every
; // Boolean
40 // indexOf, lastIndexOf
43 var delta
= 1, lOver
= 0, uOver
= 0;
45 delta
= lOver
= uOver
= -1;
47 return function(a
, x
, from, last
){
48 if(last
&& delta
> 0){
49 // TODO: why do we use a non-standard signature? why do we need "last"?
50 return array
.lastIndexOf(a
, x
, from);
52 var l
= a
&& a
.length
|| 0, end
= up
? l
+ uOver
: lOver
, i
;
54 i
= up
? lOver
: l
+ uOver
;
62 i
= from >= l
? l
+ uOver
: from;
65 if(l
&& typeof a
== "string") a
= a
.split("");
66 for(; i
!= end
; i
+= delta
){
77 // The Javascript v1.6 array extensions.
79 every
: everyOrSome(false),
81 every: function(arr, callback, thisObject){
83 // Determines whether or not every item in arr satisfies the
84 // condition implemented by callback.
86 // the array to iterate on. If a string, operates on individual characters.
87 // callback: Function|String
88 // a function is invoked with three arguments: item, index,
89 // and array and returns true if the condition is met.
90 // thisObject: Object?
91 // may be used to scope the call to callback
94 // This function corresponds to the JavaScript 1.6 Array.every() method, with one difference: when
95 // run over sparse arrays, this implementation passes the "holes" in the sparse array to
96 // the callback function with a value of undefined. JavaScript 1.6's every skips the holes in the sparse array.
97 // For more details, see:
98 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/every
100 // | // returns false
101 // | array.every([1, 2, 3, 4], function(item){ return item>1; });
104 // | array.every([1, 2, 3, 4], function(item){ return item>0; });
108 some
: everyOrSome(true),
110 some: function(arr, callback, thisObject){
112 // Determines whether or not any item in arr satisfies the
113 // condition implemented by callback.
115 // the array to iterate over. If a string, operates on individual characters.
116 // callback: Function|String
117 // a function is invoked with three arguments: item, index,
118 // and array and returns true if the condition is met.
119 // thisObject: Object?
120 // may be used to scope the call to callback
123 // This function corresponds to the JavaScript 1.6 Array.some() method, with one difference: when
124 // run over sparse arrays, this implementation passes the "holes" in the sparse array to
125 // the callback function with a value of undefined. JavaScript 1.6's some skips the holes in the sparse array.
126 // For more details, see:
127 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/some
130 // | array.some([1, 2, 3, 4], function(item){ return item>1; });
133 // | array.some([1, 2, 3, 4], function(item){ return item<1; });
137 indexOf
: index(true),
139 indexOf: function(arr, value, fromIndex, findLast){
141 // locates the first index of the provided value in the
142 // passed array. If the value is not found, -1 is returned.
144 // This method corresponds to the JavaScript 1.6 Array.indexOf method, with one difference: when
145 // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
146 // 1.6's indexOf skips the holes in the sparse array.
147 // For details on this method, see:
148 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf
151 // fromIndex: Integer?
152 // findLast: Boolean?
157 lastIndexOf
: index(false),
159 lastIndexOf: function(arr, value, fromIndex){
161 // locates the last index of the provided value in the passed
162 // array. If the value is not found, -1 is returned.
164 // This method corresponds to the JavaScript 1.6 Array.lastIndexOf method, with one difference: when
165 // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
166 // 1.6's lastIndexOf skips the holes in the sparse array.
167 // For details on this method, see:
168 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/lastIndexOf
171 // fromIndex: Integer?
176 forEach: function(arr
, callback
, thisObject
){
178 // for every item in arr, callback is invoked. Return values are ignored.
179 // If you want to break out of the loop, consider using array.every() or array.some().
180 // forEach does not allow breaking out of the loop over the items in arr.
182 // the array to iterate over. If a string, operates on individual characters.
184 // a function is invoked with three arguments: item, index, and array
186 // may be used to scope the call to callback
188 // This function corresponds to the JavaScript 1.6 Array.forEach() method, with one difference: when
189 // run over sparse arrays, this implementation passes the "holes" in the sparse array to
190 // the callback function with a value of undefined. JavaScript 1.6's forEach skips the holes in the sparse array.
191 // For more details, see:
192 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/forEach
194 // | // log out all members of the array:
196 // | [ "thinger", "blah", "howdy", 10 ],
198 // | console.log(item);
202 // | // log out the members and their indexes
204 // | [ "thinger", "blah", "howdy", 10 ],
205 // | function(item, idx, arr){
206 // | console.log(item, "at index:", idx);
210 // | // use a scoped object member as the callback
213 // | prefix: "logged via obj.callback:",
214 // | callback: function(item){
215 // | console.log(this.prefix, item);
219 // | // specifying the scope function executes the callback in that scope
221 // | [ "thinger", "blah", "howdy", 10 ],
226 // | // alternately, we can accomplish the same thing with lang.hitch()
228 // | [ "thinger", "blah", "howdy", 10 ],
229 // | lang.hitch(obj, "callback")
232 // callback: Function|String
233 // thisObject: Object?
235 var i
= 0, l
= arr
&& arr
.length
|| 0;
236 if(l
&& typeof arr
== "string") arr
= arr
.split("");
237 if(typeof callback
== "string") callback
= cache
[callback
] || buildFn(callback
);
240 callback
.call(thisObject
, arr
[i
], i
, arr
);
244 callback(arr
[i
], i
, arr
);
249 map: function(arr
, callback
, thisObject
, Ctr
){
251 // applies callback to each element of arr and returns
252 // an Array with the results
254 // the array to iterate on. If a string, operates on
255 // individual characters.
256 // callback: Function|String
257 // a function is invoked with three arguments, (item, index,
258 // array), and returns a value
259 // thisObject: Object?
260 // may be used to scope the call to callback
263 // This function corresponds to the JavaScript 1.6 Array.map() method, with one difference: when
264 // run over sparse arrays, this implementation passes the "holes" in the sparse array to
265 // the callback function with a value of undefined. JavaScript 1.6's map skips the holes in the sparse array.
266 // For more details, see:
267 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
269 // | // returns [2, 3, 4, 5]
270 // | array.map([1, 2, 3, 4], function(item){ return item+1 });
272 // TODO: why do we have a non-standard signature here? do we need "Ctr"?
273 var i
= 0, l
= arr
&& arr
.length
|| 0, out
= new (Ctr
|| Array
)(l
);
274 if(l
&& typeof arr
== "string") arr
= arr
.split("");
275 if(typeof callback
== "string") callback
= cache
[callback
] || buildFn(callback
);
278 out
[i
] = callback
.call(thisObject
, arr
[i
], i
, arr
);
282 out
[i
] = callback(arr
[i
], i
, arr
);
288 filter: function(arr
, callback
, thisObject
){
290 // Returns a new Array with those items from arr that match the
291 // condition implemented by callback.
293 // the array to iterate over.
294 // callback: Function|String
295 // a function that is invoked with three arguments (item,
296 // index, array). The return of this function is expected to
297 // be a boolean which determines whether the passed-in item
298 // will be included in the returned array.
299 // thisObject: Object?
300 // may be used to scope the call to callback
303 // This function corresponds to the JavaScript 1.6 Array.filter() method, with one difference: when
304 // run over sparse arrays, this implementation passes the "holes" in the sparse array to
305 // the callback function with a value of undefined. JavaScript 1.6's filter skips the holes in the sparse array.
306 // For more details, see:
307 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
309 // | // returns [2, 3, 4]
310 // | array.filter([1, 2, 3, 4], function(item){ return item>1; });
312 // TODO: do we need "Ctr" here like in map()?
313 var i
= 0, l
= arr
&& arr
.length
|| 0, out
= [], value
;
314 if(l
&& typeof arr
== "string") arr
= arr
.split("");
315 if(typeof callback
== "string") callback
= cache
[callback
] || buildFn(callback
);
319 if(callback
.call(thisObject
, value
, i
, arr
)){
326 if(callback(value
, i
, arr
)){
334 clearCache: function(){
340 1 && lang
.mixin(dojo
, array
);