]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/_base/array.js
Comment class stub
[eow] / static / dojo-release-1.1.1 / dojo / _base / array.js
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");
5
6 (function(){
7         var _getParts = function(arr, obj, cb){
8                 return [ 
9                         dojo.isString(arr) ? arr.split("") : arr, 
10                         obj || dojo.global,
11                         // FIXME: cache the anonymous functions we create here?
12                         dojo.isString(cb) ? new Function("item", "index", "array", cb) : cb
13                 ];
14         };
15
16         dojo.mixin(dojo, {
17                 indexOf: function(      /*Array*/               array, 
18                                                         /*Object*/              value,
19                                                         /*Integer?*/    fromIndex,
20                                                         /*Boolean?*/    findLast){
21                         // summary:
22                         //              locates the first index of the provided value in the
23                         //              passed array. If the value is not found, -1 is returned.
24                         // description:
25                         //              For details on this method, see:
26                         //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf>
27
28                         var step = 1, end = array.length || 0, i = 0;
29                         if(findLast){
30                                 i = end - 1;
31                                 step = end = -1;
32                         }
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; }
37                                 }
38                         }
39                         return -1;      // Number
40                 },
41
42                 lastIndexOf: function(/*Array*/array, /*Object*/value, /*Integer?*/fromIndex){
43                         // summary:
44                         //              locates the last index of the provided value in the passed array. 
45                         //              If the value is not found, -1 is returned.
46                         // description:
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
50                 },
51
52                 forEach: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){
53                         // summary:
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
58                         // description:
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>
63
64                         // match the behavior of the built-in forEach WRT empty arrs
65                         if(!arr || !arr.length){ return; }
66
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);
72                         }
73                 },
74
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);
79                                 if(every ^ result){
80                                         return result; // Boolean
81                                 }
82                         }
83                         return every; // Boolean
84                 },
85
86                 every: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){
87                         // summary:
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
94                         // description:
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>
99                         // example:
100                         //      |       dojo.every([1, 2, 3, 4], function(item){ return item>1; });
101                         //              returns false
102                         // example:
103                         //      |       dojo.every([1, 2, 3, 4], function(item){ return item>0; });
104                         //              returns true 
105                         return this._everyOrSome(true, arr, callback, thisObject); // Boolean
106                 },
107
108                 some: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){
109                         // summary:
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
116                         // description:
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>
121                         // example:
122                         //      |       dojo.some([1, 2, 3, 4], function(item){ return item>1; });
123                         //              returns true
124                         // example:
125                         //      |       dojo.some([1, 2, 3, 4], function(item){ return item<1; });
126                         //              returns false
127                         return this._everyOrSome(false, arr, callback, thisObject); // Boolean
128                 },
129
130                 map: function(/*Array|String*/arr, /*Function|String*/callback, /*Function?*/thisObject){
131                         // summary:
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
137                         // description:
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>
142                         // example:
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));
149                         }
150                         return outArr; // Array
151                 },
152
153                 filter: function(/*Array*/arr, /*Function|String*/callback, /*Object?*/thisObject){
154                         // summary:
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
161                         // description:
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>
166                         // example:
167                         //      |       dojo.filter([1, 2, 3, 4], function(item){ return item>1; });
168                         //              returns [2, 3, 4]
169
170                         var _p = _getParts(arr, thisObject, callback); arr = _p[0];
171                         var outArr = [];
172                         for(var i = 0; i < arr.length; i++){
173                                 if(_p[2].call(_p[1], arr[i], i, arr)){
174                                         outArr.push(arr[i]);
175                                 }
176                         }
177                         return outArr; // Array
178                 }
179         });
180 })();
181
182 }