1 if(!dojo._hasResource["dojo._base.array"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo._base.array"] = true;
3 dojo.require("dojo._base.lang");
4 dojo.provide("dojo._base.array");
7 var _getParts = function(arr, obj, cb){
9 dojo.isString(arr) ? arr.split("") : arr,
11 // FIXME: cache the anonymous functions we create here?
12 dojo.isString(cb) ? new Function("item", "index", "array", cb) : cb
17 indexOf: function( /*Array*/ array,
19 /*Integer?*/ fromIndex,
20 /*Boolean?*/ findLast){
22 // locates the first index of the provided value in the
23 // passed array. If the value is not found, -1 is returned.
25 // For details on this method, see:
26 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf>
28 var step = 1, end = array.length || 0, i = 0;
33 if(fromIndex != undefined){ i = fromIndex; }
34 if((findLast && i > end) || i < end){
35 for(; i != end; i += step){
36 if(array[i] == value){ return i; }
42 lastIndexOf: function(/*Array*/array, /*Object*/value, /*Integer?*/fromIndex){
44 // locates the last index of the provided value in the passed array.
45 // If the value is not found, -1 is returned.
47 // For details on this method, see:
48 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:lastIndexOf>
49 return dojo.indexOf(array, value, fromIndex, true); // Number
52 forEach: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){
54 // for every item in arr, callback is invoked. Return values are ignored.
55 // arr: the array to iterate on. If a string, operates on individual characters.
56 // callback: a function is invoked with three arguments: item, index, and array
57 // thisObject: may be used to scope the call to callback
59 // This function corresponds to the JavaScript 1.6 Array.forEach() method.
60 // In environments that support JavaScript 1.6, this function is a passthrough to the built-in method.
61 // For more details, see:
62 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach>
64 // match the behavior of the built-in forEach WRT empty arrs
65 if(!arr || !arr.length){ return; }
67 // FIXME: there are several ways of handilng thisObject. Is
68 // dojo.global always the default context?
69 var _p = _getParts(arr, thisObject, callback); arr = _p[0];
70 for(var i=0,l=_p[0].length; i<l; i++){
71 _p[2].call(_p[1], arr[i], i, arr);
75 _everyOrSome: function(/*Boolean*/every, /*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){
76 var _p = _getParts(arr, thisObject, callback); arr = _p[0];
77 for(var i = 0, l = arr.length; i < l; i++){
78 var result = !!_p[2].call(_p[1], arr[i], i, arr);
80 return result; // Boolean
83 return every; // Boolean
86 every: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){
88 // Determines whether or not every item in arr satisfies the
89 // condition implemented by callback.
90 // arr: the array to iterate on. If a string, operates on individual characters.
91 // callback: a function is invoked with three arguments: item, index, and array and returns true
92 // if the condition is met.
93 // thisObject: may be used to scope the call to callback
95 // This function corresponds to the JavaScript 1.6 Array.every() method.
96 // In environments that support JavaScript 1.6, this function is a passthrough to the built-in method.
97 // For more details, see:
98 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every>
100 // | dojo.every([1, 2, 3, 4], function(item){ return item>1; });
103 // | dojo.every([1, 2, 3, 4], function(item){ return item>0; });
105 return this._everyOrSome(true, arr, callback, thisObject); // Boolean
108 some: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){
110 // Determines whether or not any item in arr satisfies the
111 // condition implemented by callback.
112 // arr: the array to iterate on. If a string, operates on individual characters.
113 // callback: a function is invoked with three arguments: item, index, and array and returns true
114 // if the condition is met.
115 // thisObject: may be used to scope the call to callback
117 // This function corresponds to the JavaScript 1.6 Array.some() method.
118 // In environments that support JavaScript 1.6, this function is a passthrough to the built-in method.
119 // For more details, see:
120 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:some>
122 // | dojo.some([1, 2, 3, 4], function(item){ return item>1; });
125 // | dojo.some([1, 2, 3, 4], function(item){ return item<1; });
127 return this._everyOrSome(false, arr, callback, thisObject); // Boolean
130 map: function(/*Array|String*/arr, /*Function|String*/callback, /*Function?*/thisObject){
132 // applies callback to each element of arr and returns
133 // an Array with the results
134 // arr: the array to iterate on. If a string, operates on individual characters.
135 // callback: a function is invoked with three arguments: item, index, and array and returns a value
136 // thisObject: may be used to scope the call to callback
138 // This function corresponds to the JavaScript 1.6 Array.map() method.
139 // In environments that support JavaScript 1.6, this function is a passthrough to the built-in method.
140 // For more details, see:
141 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:map>
143 // | dojo.map([1, 2, 3, 4], function(item){ return item+1 });
144 // returns [2, 3, 4, 5]
145 var _p = _getParts(arr, thisObject, callback); arr = _p[0];
146 var outArr = (arguments[3] ? (new arguments[3]()) : []);
147 for(var i=0;i<arr.length;++i){
148 outArr.push(_p[2].call(_p[1], arr[i], i, arr));
150 return outArr; // Array
153 filter: function(/*Array*/arr, /*Function|String*/callback, /*Object?*/thisObject){
155 // Returns a new Array with those items from arr that match the
156 // condition implemented by callback.
157 // arr: the array to iterate on. If a string, operates on individual characters.
158 // callback: a function is invoked with three arguments: item, index, and array and returns true
159 // if the condition is met.
160 // thisObject: may be used to scope the call to callback
162 // This function corresponds to the JavaScript 1.6 Array.filter() method.
163 // In environments that support JavaScript 1.6, this function is a passthrough to the built-in method.
164 // For more details, see:
165 // <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:filter>
167 // | dojo.filter([1, 2, 3, 4], function(item){ return item>1; });
170 var _p = _getParts(arr, thisObject, callback); arr = _p[0];
172 for(var i = 0; i < arr.length; i++){
173 if(_p[2].call(_p[1], arr[i], i, arr)){
177 return outArr; // Array