1 define("dojo/_base/array", ["./kernel", "../has", "./lang"], function(dojo, has, lang){
5 // This module defines the Javascript v1.6 array extensions.
8 dojo.indexOf = function(arr, value, fromIndex, findLast){
10 // locates the first index of the provided value in the
11 // passed array. If the value is not found, -1 is returned.
13 // This method corresponds to the JavaScript 1.6 Array.indexOf method, with one difference: when
14 // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
15 // 1.6's indexOf skips the holes in the sparse array.
16 // For details on this method, see:
17 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf
20 // fromIndex: Integer?
24 dojo.lastIndexOf = function(arr, value, fromIndex){
26 // locates the last index of the provided value in the passed
27 // array. If the value is not found, -1 is returned.
29 // This method corresponds to the JavaScript 1.6 Array.lastIndexOf method, with one difference: when
30 // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
31 // 1.6's lastIndexOf skips the holes in the sparse array.
32 // For details on this method, see:
33 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/lastIndexOf
36 // fromIndex: Integer?
39 dojo.forEach = function(arr, callback, thisObject){
41 // for every item in arr, callback is invoked. Return values are ignored.
42 // If you want to break out of the loop, consider using dojo.every() or dojo.some().
43 // forEach does not allow breaking out of the loop over the items in arr.
45 // the array to iterate over. If a string, operates on individual characters.
47 // a function is invoked with three arguments: item, index, and array
49 // may be used to scope the call to callback
51 // This function corresponds to the JavaScript 1.6 Array.forEach() method, with one difference: when
52 // run over sparse arrays, this implementation passes the "holes" in the sparse array to
53 // the callback function with a value of undefined. JavaScript 1.6's forEach skips the holes in the sparse array.
54 // For more details, see:
55 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/forEach
57 // | // log out all members of the array:
59 // | [ "thinger", "blah", "howdy", 10 ],
61 // | console.log(item);
65 // | // log out the members and their indexes
67 // | [ "thinger", "blah", "howdy", 10 ],
68 // | function(item, idx, arr){
69 // | console.log(item, "at index:", idx);
73 // | // use a scoped object member as the callback
76 // | prefix: "logged via obj.callback:",
77 // | callback: function(item){
78 // | console.log(this.prefix, item);
82 // | // specifying the scope function executes the callback in that scope
84 // | [ "thinger", "blah", "howdy", 10 ],
89 // | // alternately, we can accomplish the same thing with dojo.hitch()
91 // | [ "thinger", "blah", "howdy", 10 ],
92 // | dojo.hitch(obj, "callback")
95 // callback: Function|String
96 // thisObject: Object?
98 dojo.every = function(arr, callback, thisObject){
100 // Determines whether or not every item in arr satisfies the
101 // condition implemented by callback.
103 // the array to iterate on. If a string, operates on individual characters.
104 // callback: Function|String
105 // a function is invoked with three arguments: item, index,
106 // and array and returns true if the condition is met.
107 // thisObject: Object?
108 // may be used to scope the call to callback
111 // This function corresponds to the JavaScript 1.6 Array.every() method, with one difference: when
112 // run over sparse arrays, this implementation passes the "holes" in the sparse array to
113 // the callback function with a value of undefined. JavaScript 1.6's every skips the holes in the sparse array.
114 // For more details, see:
115 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/every
117 // | // returns false
118 // | dojo.every([1, 2, 3, 4], function(item){ return item>1; });
121 // | dojo.every([1, 2, 3, 4], function(item){ return item>0; });
123 dojo.some = function(arr, callback, thisObject){
125 // Determines whether or not any item in arr satisfies the
126 // condition implemented by callback.
128 // the array to iterate over. If a string, operates on individual characters.
129 // callback: Function|String
130 // a function is invoked with three arguments: item, index,
131 // and array and returns true if the condition is met.
132 // thisObject: Object?
133 // may be used to scope the call to callback
136 // This function corresponds to the JavaScript 1.6 Array.some() method, with one difference: when
137 // run over sparse arrays, this implementation passes the "holes" in the sparse array to
138 // the callback function with a value of undefined. JavaScript 1.6's some skips the holes in the sparse array.
139 // For more details, see:
140 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/some
143 // | dojo.some([1, 2, 3, 4], function(item){ return item>1; });
146 // | dojo.some([1, 2, 3, 4], function(item){ return item<1; });
148 dojo.map = function(arr, callback, thisObject){
150 // applies callback to each element of arr and returns
151 // an Array with the results
153 // the array to iterate on. If a string, operates on
154 // individual characters.
155 // callback: Function|String
156 // a function is invoked with three arguments, (item, index,
157 // array), and returns a value
158 // thisObject: Object?
159 // may be used to scope the call to callback
162 // This function corresponds to the JavaScript 1.6 Array.map() method, with one difference: when
163 // run over sparse arrays, this implementation passes the "holes" in the sparse array to
164 // the callback function with a value of undefined. JavaScript 1.6's map skips the holes in the sparse array.
165 // For more details, see:
166 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
168 // | // returns [2, 3, 4, 5]
169 // | dojo.map([1, 2, 3, 4], function(item){ return item+1 });
171 dojo.filter = function(arr, callback, thisObject){
173 // Returns a new Array with those items from arr that match the
174 // condition implemented by callback.
176 // the array to iterate over.
177 // callback: Function|String
178 // a function that is invoked with three arguments (item,
179 // index, array). The return of this function is expected to
180 // be a boolean which determines whether the passed-in item
181 // will be included in the returned array.
182 // thisObject: Object?
183 // may be used to scope the call to callback
186 // This function corresponds to the JavaScript 1.6 Array.filter() method, with one difference: when
187 // run over sparse arrays, this implementation passes the "holes" in the sparse array to
188 // the callback function with a value of undefined. JavaScript 1.6's filter skips the holes in the sparse array.
189 // For more details, see:
190 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
192 // | // returns [2, 3, 4]
193 // | dojo.filter([1, 2, 3, 4], function(item){ return item>1; });
197 // our old simple function builder stuff
198 var cache = {}, u, array; // the export object
200 function clearCache(){
204 function buildFn(fn){
205 return cache[fn] = new Function("item", "index", "array", fn); // Function
207 // magic snippet: if(typeof fn == "string") fn = cache[fn] || buildFn(fn);
211 function everyOrSome(some){
213 return function(a, fn, o){
214 var i = 0, l = a && a.length || 0, result;
215 if(l && typeof a == "string") a = a.split("");
216 if(typeof fn == "string") fn = cache[fn] || buildFn(fn);
219 result = !fn.call(o, a[i], i, a);
226 result = !fn(a[i], i, a);
232 return every; // Boolean
235 // var every = everyOrSome(false), some = everyOrSome(true);
237 // indexOf, lastIndexOf
240 var delta = 1, lOver = 0, uOver = 0;
242 delta = lOver = uOver = -1;
244 return function(a, x, from, last){
245 if(last && delta > 0){
246 // TODO: why do we use a non-standard signature? why do we need "last"?
247 return array.lastIndexOf(a, x, from);
249 var l = a && a.length || 0, end = up ? l + uOver : lOver, i;
251 i = up ? lOver : l + uOver;
259 i = from >= l ? l + uOver : from;
262 if(l && typeof a == "string") a = a.split("");
263 for(; i != end; i += delta){
271 // var indexOf = index(true), lastIndexOf = index(false);
273 function forEach(a, fn, o){
274 var i = 0, l = a && a.length || 0;
275 if(l && typeof a == "string") a = a.split("");
276 if(typeof fn == "string") fn = cache[fn] || buildFn(fn);
279 fn.call(o, a[i], i, a);
288 function map(a, fn, o, Ctr){
289 // TODO: why do we have a non-standard signature here? do we need "Ctr"?
290 var i = 0, l = a && a.length || 0, out = new (Ctr || Array)(l);
291 if(l && typeof a == "string") a = a.split("");
292 if(typeof fn == "string") fn = cache[fn] || buildFn(fn);
295 out[i] = fn.call(o, a[i], i, a);
299 out[i] = fn(a[i], i, a);
305 function filter(a, fn, o){
306 // TODO: do we need "Ctr" here like in map()?
307 var i = 0, l = a && a.length || 0, out = [], value;
308 if(l && typeof a == "string") a = a.split("");
309 if(typeof fn == "string") fn = cache[fn] || buildFn(fn);
313 if(fn.call(o, value, i, a)){
329 every: everyOrSome(false),
330 some: everyOrSome(true),
331 indexOf: index(true),
332 lastIndexOf: index(false),
336 clearCache: clearCache
339 1 && lang.mixin(dojo, array);
341 /*===== return dojo.array; =====*/