]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/_base/array.js
2 Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
8 if(!dojo
._hasResource
["dojo._base.array"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo
._hasResource
["dojo._base.array"] = true;
10 dojo
.provide("dojo._base.array");
11 dojo
.require("dojo._base.lang");
15 var _getParts = function(arr
, obj
, cb
){
17 (typeof arr
== "string") ? arr
.split("") : arr
,
19 // FIXME: cache the anonymous functions we create here?
20 (typeof cb
== "string") ? new Function("item", "index", "array", cb
) : cb
24 var everyOrSome = function(/*Boolean*/every
, /*Array|String*/arr
, /*Function|String*/callback
, /*Object?*/thisObject
){
25 var _p
= _getParts(arr
, thisObject
, callback
); arr
= _p
[0];
26 for(var i
=0,l
=arr
.length
; i
<l
; ++i
){
27 var result
= !!_p
[2].call(_p
[1], arr
[i
], i
, arr
);
29 return result
; // Boolean
32 return every
; // Boolean
36 indexOf: function( /*Array*/ array
,
38 /*Integer?*/ fromIndex
,
39 /*Boolean?*/ findLast
){
41 // locates the first index of the provided value in the
42 // passed array. If the value is not found, -1 is returned.
44 // This method corresponds to the JavaScript 1.6 Array.indexOf method, with one difference: when
45 // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
46 // 1.6's indexOf skips the holes in the sparse array.
47 // For details on this method, see:
48 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf
50 var step
= 1, end
= array
.length
|| 0, i
= 0;
55 if(fromIndex
!= undefined){ i
= fromIndex
; }
56 if((findLast
&& i
> end
) || i
< end
){
57 for(; i
!= end
; i
+= step
){
58 if(array
[i
] == value
){ return i
; }
64 lastIndexOf: function(/*Array*/array
, /*Object*/value
, /*Integer?*/fromIndex
){
66 // locates the last index of the provided value in the passed
67 // array. If the value is not found, -1 is returned.
69 // This method corresponds to the JavaScript 1.6 Array.lastIndexOf method, with one difference: when
70 // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
71 // 1.6's lastIndexOf skips the holes in the sparse array.
72 // For details on this method, see:
73 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/lastIndexOf
74 return dojo
.indexOf(array
, value
, fromIndex
, true); // Number
77 forEach: function(/*Array|String*/arr
, /*Function|String*/callback
, /*Object?*/thisObject
){
79 // for every item in arr, callback is invoked. Return values are ignored.
80 // If you want to break out of the loop, consider using dojo.every() or dojo.some().
81 // forEach does not allow breaking out of the loop over the items in arr.
83 // the array to iterate over. If a string, operates on individual characters.
85 // a function is invoked with three arguments: item, index, and array
87 // may be used to scope the call to callback
89 // This function corresponds to the JavaScript 1.6 Array.forEach() method, with one difference: when
90 // run over sparse arrays, this implemenation passes the "holes" in the sparse array to
91 // the callback function with a value of undefined. JavaScript 1.6's forEach skips the holes in the sparse array.
92 // For more details, see:
93 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/forEach
95 // | // log out all members of the array:
97 // | [ "thinger", "blah", "howdy", 10 ],
99 // | console.log(item);
103 // | // log out the members and their indexes
105 // | [ "thinger", "blah", "howdy", 10 ],
106 // | function(item, idx, arr){
107 // | console.log(item, "at index:", idx);
111 // | // use a scoped object member as the callback
114 // | prefix: "logged via obj.callback:",
115 // | callback: function(item){
116 // | console.log(this.prefix, item);
120 // | // specifying the scope function executes the callback in that scope
122 // | [ "thinger", "blah", "howdy", 10 ],
127 // | // alternately, we can accomplish the same thing with dojo.hitch()
129 // | [ "thinger", "blah", "howdy", 10 ],
130 // | dojo.hitch(obj, "callback")
133 // match the behavior of the built-in forEach WRT empty arrs
134 if(!arr
|| !arr
.length
){ return; }
136 // FIXME: there are several ways of handilng thisObject. Is
137 // dojo.global always the default context?
138 var _p
= _getParts(arr
, thisObject
, callback
); arr
= _p
[0];
139 for(var i
=0,l
=arr
.length
; i
<l
; ++i
){
140 _p
[2].call(_p
[1], arr
[i
], i
, arr
);
144 every: function(/*Array|String*/arr
, /*Function|String*/callback
, /*Object?*/thisObject
){
146 // Determines whether or not every item in arr satisfies the
147 // condition implemented by callback.
149 // the array to iterate on. If a string, operates on individual characters.
151 // a function is invoked with three arguments: item, index,
152 // and array and returns true if the condition is met.
154 // may be used to scope the call to callback
156 // This function corresponds to the JavaScript 1.6 Array.every() method, with one difference: when
157 // run over sparse arrays, this implemenation passes the "holes" in the sparse array to
158 // the callback function with a value of undefined. JavaScript 1.6's every skips the holes in the sparse array.
159 // For more details, see:
160 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/every
162 // | // returns false
163 // | dojo.every([1, 2, 3, 4], function(item){ return item>1; });
166 // | dojo.every([1, 2, 3, 4], function(item){ return item>0; });
167 return everyOrSome(true, arr
, callback
, thisObject
); // Boolean
170 some: function(/*Array|String*/arr
, /*Function|String*/callback
, /*Object?*/thisObject
){
172 // Determines whether or not any item in arr satisfies the
173 // condition implemented by callback.
175 // the array to iterate over. If a string, operates on individual characters.
177 // a function is invoked with three arguments: item, index,
178 // and array and returns true if the condition is met.
180 // may be used to scope the call to callback
182 // This function corresponds to the JavaScript 1.6 Array.some() method, with one difference: when
183 // run over sparse arrays, this implemenation passes the "holes" in the sparse array to
184 // the callback function with a value of undefined. JavaScript 1.6's some skips the holes in the sparse array.
185 // For more details, see:
186 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/some
189 // | dojo.some([1, 2, 3, 4], function(item){ return item>1; });
192 // | dojo.some([1, 2, 3, 4], function(item){ return item<1; });
193 return everyOrSome(false, arr
, callback
, thisObject
); // Boolean
196 map: function(/*Array|String*/arr
, /*Function|String*/callback
, /*Function?*/thisObject
){
198 // applies callback to each element of arr and returns
199 // an Array with the results
201 // the array to iterate on. If a string, operates on
202 // individual characters.
204 // a function is invoked with three arguments, (item, index,
205 // array), and returns a value
207 // may be used to scope the call to callback
209 // This function corresponds to the JavaScript 1.6 Array.map() method, with one difference: when
210 // run over sparse arrays, this implemenation passes the "holes" in the sparse array to
211 // the callback function with a value of undefined. JavaScript 1.6's map skips the holes in the sparse array.
212 // For more details, see:
213 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
215 // | // returns [2, 3, 4, 5]
216 // | dojo.map([1, 2, 3, 4], function(item){ return item+1 });
218 var _p
= _getParts(arr
, thisObject
, callback
); arr
= _p
[0];
219 var outArr
= (arguments
[3] ? (new arguments
[3]()) : []);
220 for(var i
=0,l
=arr
.length
; i
<l
; ++i
){
221 outArr
.push(_p
[2].call(_p
[1], arr
[i
], i
, arr
));
223 return outArr
; // Array
226 filter: function(/*Array*/arr
, /*Function|String*/callback
, /*Object?*/thisObject
){
228 // Returns a new Array with those items from arr that match the
229 // condition implemented by callback.
231 // the array to iterate over.
233 // a function that is invoked with three arguments (item,
234 // index, array). The return of this function is expected to
235 // be a boolean which determines whether the passed-in item
236 // will be included in the returned array.
238 // may be used to scope the call to callback
240 // This function corresponds to the JavaScript 1.6 Array.filter() method, with one difference: when
241 // run over sparse arrays, this implemenation passes the "holes" in the sparse array to
242 // the callback function with a value of undefined. JavaScript 1.6's filter skips the holes in the sparse array.
243 // For more details, see:
244 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
246 // | // returns [2, 3, 4]
247 // | dojo.filter([1, 2, 3, 4], function(item){ return item>1; });
249 var _p
= _getParts(arr
, thisObject
, callback
); arr
= _p
[0];
251 for(var i
=0,l
=arr
.length
; i
<l
; ++i
){
252 if(_p
[2].call(_p
[1], arr
[i
], i
, arr
)){
256 return outArr
; // Array