]>
Commit | Line | Data |
---|---|---|
2f01fe57 | 1 | /* |
81bea17a | 2 | Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved. |
2f01fe57 AD |
3 | Available via Academic Free License >= 2.1 OR the modified BSD license. |
4 | see: http://dojotoolkit.org/license for details | |
5 | */ | |
6 | ||
7 | ||
a089699c AD |
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; | |
2f01fe57 | 10 | dojo.provide("dojo._base.array"); |
81bea17a AD |
11 | dojo.require("dojo._base.lang"); |
12 | ||
a089699c | 13 | |
2f01fe57 | 14 | (function(){ |
a089699c | 15 | var _getParts = function(arr, obj, cb){ |
81bea17a AD |
16 | return [ |
17 | (typeof arr == "string") ? arr.split("") : arr, | |
a089699c AD |
18 | obj || dojo.global, |
19 | // FIXME: cache the anonymous functions we create here? | |
20 | (typeof cb == "string") ? new Function("item", "index", "array", cb) : cb | |
21 | ]; | |
22 | }; | |
23 | ||
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); | |
28 | if(every ^ result){ | |
29 | return result; // Boolean | |
30 | } | |
31 | } | |
32 | return every; // Boolean | |
33 | }; | |
34 | ||
35 | dojo.mixin(dojo, { | |
81bea17a | 36 | indexOf: function( /*Array*/ array, |
a089699c AD |
37 | /*Object*/ value, |
38 | /*Integer?*/ fromIndex, | |
39 | /*Boolean?*/ findLast){ | |
40 | // summary: | |
41 | // locates the first index of the provided value in the | |
42 | // passed array. If the value is not found, -1 is returned. | |
43 | // description: | |
44 | // This method corresponds to the JavaScript 1.6 Array.indexOf method, with one difference: when | |
81bea17a | 45 | // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript |
a089699c AD |
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 | |
49 | ||
50 | var step = 1, end = array.length || 0, i = 0; | |
51 | if(findLast){ | |
52 | i = end - 1; | |
53 | step = end = -1; | |
54 | } | |
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; } | |
59 | } | |
60 | } | |
61 | return -1; // Number | |
62 | }, | |
63 | ||
64 | lastIndexOf: function(/*Array*/array, /*Object*/value, /*Integer?*/fromIndex){ | |
65 | // summary: | |
66 | // locates the last index of the provided value in the passed | |
67 | // array. If the value is not found, -1 is returned. | |
68 | // description: | |
69 | // This method corresponds to the JavaScript 1.6 Array.lastIndexOf method, with one difference: when | |
81bea17a | 70 | // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript |
a089699c AD |
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 | |
75 | }, | |
76 | ||
77 | forEach: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){ | |
78 | // summary: | |
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. | |
82 | // arr: | |
83 | // the array to iterate over. If a string, operates on individual characters. | |
84 | // callback: | |
85 | // a function is invoked with three arguments: item, index, and array | |
86 | // thisObject: | |
87 | // may be used to scope the call to callback | |
88 | // description: | |
81bea17a | 89 | // This function corresponds to the JavaScript 1.6 Array.forEach() method, with one difference: when |
a089699c AD |
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 | |
94 | // example: | |
95 | // | // log out all members of the array: | |
96 | // | dojo.forEach( | |
97 | // | [ "thinger", "blah", "howdy", 10 ], | |
98 | // | function(item){ | |
99 | // | console.log(item); | |
100 | // | } | |
101 | // | ); | |
102 | // example: | |
103 | // | // log out the members and their indexes | |
104 | // | dojo.forEach( | |
105 | // | [ "thinger", "blah", "howdy", 10 ], | |
106 | // | function(item, idx, arr){ | |
107 | // | console.log(item, "at index:", idx); | |
108 | // | } | |
109 | // | ); | |
110 | // example: | |
111 | // | // use a scoped object member as the callback | |
81bea17a | 112 | // | |
a089699c | 113 | // | var obj = { |
81bea17a | 114 | // | prefix: "logged via obj.callback:", |
a089699c AD |
115 | // | callback: function(item){ |
116 | // | console.log(this.prefix, item); | |
117 | // | } | |
118 | // | }; | |
81bea17a | 119 | // | |
a089699c AD |
120 | // | // specifying the scope function executes the callback in that scope |
121 | // | dojo.forEach( | |
122 | // | [ "thinger", "blah", "howdy", 10 ], | |
123 | // | obj.callback, | |
124 | // | obj | |
125 | // | ); | |
81bea17a | 126 | // | |
a089699c AD |
127 | // | // alternately, we can accomplish the same thing with dojo.hitch() |
128 | // | dojo.forEach( | |
129 | // | [ "thinger", "blah", "howdy", 10 ], | |
130 | // | dojo.hitch(obj, "callback") | |
131 | // | ); | |
132 | ||
133 | // match the behavior of the built-in forEach WRT empty arrs | |
134 | if(!arr || !arr.length){ return; } | |
135 | ||
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]; | |
81bea17a | 139 | for(var i=0,l=arr.length; i<l; ++i){ |
a089699c AD |
140 | _p[2].call(_p[1], arr[i], i, arr); |
141 | } | |
142 | }, | |
143 | ||
144 | every: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){ | |
145 | // summary: | |
146 | // Determines whether or not every item in arr satisfies the | |
147 | // condition implemented by callback. | |
148 | // arr: | |
149 | // the array to iterate on. If a string, operates on individual characters. | |
150 | // callback: | |
151 | // a function is invoked with three arguments: item, index, | |
152 | // and array and returns true if the condition is met. | |
153 | // thisObject: | |
154 | // may be used to scope the call to callback | |
155 | // description: | |
81bea17a | 156 | // This function corresponds to the JavaScript 1.6 Array.every() method, with one difference: when |
a089699c AD |
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 | |
161 | // example: | |
162 | // | // returns false | |
163 | // | dojo.every([1, 2, 3, 4], function(item){ return item>1; }); | |
164 | // example: | |
81bea17a | 165 | // | // returns true |
a089699c AD |
166 | // | dojo.every([1, 2, 3, 4], function(item){ return item>0; }); |
167 | return everyOrSome(true, arr, callback, thisObject); // Boolean | |
168 | }, | |
169 | ||
170 | some: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){ | |
171 | // summary: | |
172 | // Determines whether or not any item in arr satisfies the | |
173 | // condition implemented by callback. | |
174 | // arr: | |
175 | // the array to iterate over. If a string, operates on individual characters. | |
176 | // callback: | |
177 | // a function is invoked with three arguments: item, index, | |
178 | // and array and returns true if the condition is met. | |
179 | // thisObject: | |
180 | // may be used to scope the call to callback | |
181 | // description: | |
81bea17a | 182 | // This function corresponds to the JavaScript 1.6 Array.some() method, with one difference: when |
a089699c AD |
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 | |
187 | // example: | |
188 | // | // is true | |
189 | // | dojo.some([1, 2, 3, 4], function(item){ return item>1; }); | |
190 | // example: | |
191 | // | // is false | |
192 | // | dojo.some([1, 2, 3, 4], function(item){ return item<1; }); | |
193 | return everyOrSome(false, arr, callback, thisObject); // Boolean | |
194 | }, | |
195 | ||
196 | map: function(/*Array|String*/arr, /*Function|String*/callback, /*Function?*/thisObject){ | |
197 | // summary: | |
198 | // applies callback to each element of arr and returns | |
199 | // an Array with the results | |
200 | // arr: | |
201 | // the array to iterate on. If a string, operates on | |
202 | // individual characters. | |
203 | // callback: | |
204 | // a function is invoked with three arguments, (item, index, | |
205 | // array), and returns a value | |
206 | // thisObject: | |
207 | // may be used to scope the call to callback | |
208 | // description: | |
81bea17a | 209 | // This function corresponds to the JavaScript 1.6 Array.map() method, with one difference: when |
a089699c AD |
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 | |
214 | // example: | |
215 | // | // returns [2, 3, 4, 5] | |
216 | // | dojo.map([1, 2, 3, 4], function(item){ return item+1 }); | |
217 | ||
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)); | |
222 | } | |
223 | return outArr; // Array | |
224 | }, | |
225 | ||
226 | filter: function(/*Array*/arr, /*Function|String*/callback, /*Object?*/thisObject){ | |
227 | // summary: | |
228 | // Returns a new Array with those items from arr that match the | |
229 | // condition implemented by callback. | |
230 | // arr: | |
231 | // the array to iterate over. | |
232 | // callback: | |
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. | |
237 | // thisObject: | |
238 | // may be used to scope the call to callback | |
239 | // description: | |
81bea17a | 240 | // This function corresponds to the JavaScript 1.6 Array.filter() method, with one difference: when |
a089699c | 241 | // run over sparse arrays, this implemenation passes the "holes" in the sparse array to |
81bea17a | 242 | // the callback function with a value of undefined. JavaScript 1.6's filter skips the holes in the sparse array. |
a089699c AD |
243 | // For more details, see: |
244 | // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter | |
245 | // example: | |
246 | // | // returns [2, 3, 4] | |
247 | // | dojo.filter([1, 2, 3, 4], function(item){ return item>1; }); | |
248 | ||
249 | var _p = _getParts(arr, thisObject, callback); arr = _p[0]; | |
250 | var outArr = []; | |
251 | for(var i=0,l=arr.length; i<l; ++i){ | |
252 | if(_p[2].call(_p[1], arr[i], i, arr)){ | |
253 | outArr.push(arr[i]); | |
254 | } | |
255 | } | |
256 | return outArr; // Array | |
257 | } | |
258 | }); | |
2f01fe57 | 259 | })(); |
a089699c AD |
260 | /* |
261 | */ | |
262 | ||
2f01fe57 | 263 | } |