2150 |
mathias |
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 i = 0, step = 1, end = array.length;
|
|
|
29 |
if(findLast){
|
|
|
30 |
i = end - 1;
|
|
|
31 |
step = end = -1;
|
|
|
32 |
}
|
|
|
33 |
for(i = fromIndex || i; i != end; i += step){
|
|
|
34 |
if(array[i] == value){ return i; }
|
|
|
35 |
}
|
|
|
36 |
return -1; // Number
|
|
|
37 |
},
|
|
|
38 |
|
|
|
39 |
lastIndexOf: function(/*Array*/array, /*Object*/value, /*Integer?*/fromIndex){
|
|
|
40 |
// summary:
|
|
|
41 |
// locates the last index of the provided value in the passed array.
|
|
|
42 |
// If the value is not found, -1 is returned.
|
|
|
43 |
// description:
|
|
|
44 |
// For details on this method, see:
|
|
|
45 |
// http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:lastIndexOf
|
|
|
46 |
return dojo.indexOf(array, value, fromIndex, true); // Number
|
|
|
47 |
},
|
|
|
48 |
|
|
|
49 |
forEach: function(/*Array*/arr, /*Function*/callback, /*Object?*/obj){
|
|
|
50 |
// summary:
|
|
|
51 |
// for every item in arr, call callback with that item as its
|
|
|
52 |
// only parameter.
|
|
|
53 |
// description:
|
|
|
54 |
// Return values are ignored. This function
|
|
|
55 |
// corresponds (and wraps) the JavaScript 1.6 forEach method. For
|
|
|
56 |
// more details, see:
|
|
|
57 |
// http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach
|
|
|
58 |
|
|
|
59 |
// match the behavior of the built-in forEach WRT empty arrs
|
|
|
60 |
if(!arr || !arr.length){ return; }
|
|
|
61 |
|
|
|
62 |
// FIXME: there are several ways of handilng thisObject. Is
|
|
|
63 |
// dojo.global always the default context?
|
|
|
64 |
var _p = _getParts(arr, obj, callback); arr = _p[0];
|
|
|
65 |
for(var i=0,l=_p[0].length; i<l; i++){
|
|
|
66 |
_p[2].call(_p[1], arr[i], i, arr);
|
|
|
67 |
}
|
|
|
68 |
},
|
|
|
69 |
|
|
|
70 |
_everyOrSome: function(/*Boolean*/every, /*Array*/arr, /*Function*/callback, /*Object?*/obj){
|
|
|
71 |
var _p = _getParts(arr, obj, callback); arr = _p[0];
|
|
|
72 |
for(var i = 0, l = arr.length; i < l; i++){
|
|
|
73 |
var result = !!_p[2].call(_p[1], arr[i], i, arr);
|
|
|
74 |
if(every ^ result){
|
|
|
75 |
return result; // Boolean
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
return every; // Boolean
|
|
|
79 |
},
|
|
|
80 |
|
|
|
81 |
every: function(/*Array*/arr, /*Function*/callback, /*Object?*/thisObject){
|
|
|
82 |
// summary:
|
|
|
83 |
// Determines whether or not every item in the array satisfies the
|
|
|
84 |
// condition implemented by callback.
|
|
|
85 |
// description:
|
|
|
86 |
// The parameter thisObject may be used to
|
|
|
87 |
// scope the call to callback. The function signature is derived
|
|
|
88 |
// from the JavaScript 1.6 Array.every() function. More
|
|
|
89 |
// information on this can be found here:
|
|
|
90 |
// http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every
|
|
|
91 |
// example:
|
|
|
92 |
// | dojo.every([1, 2, 3, 4], function(item){ return item>1; });
|
|
|
93 |
// returns false
|
|
|
94 |
// example:
|
|
|
95 |
// | dojo.every([1, 2, 3, 4], function(item){ return item>0; });
|
|
|
96 |
// returns true
|
|
|
97 |
return this._everyOrSome(true, arr, callback, thisObject); // Boolean
|
|
|
98 |
},
|
|
|
99 |
|
|
|
100 |
some: function(/*Array*/arr, /*Function*/callback, /*Object?*/thisObject){
|
|
|
101 |
// summary:
|
|
|
102 |
// Determines whether or not any item in the array satisfies the
|
|
|
103 |
// condition implemented by callback.
|
|
|
104 |
// description:
|
|
|
105 |
// The parameter thisObject may be used to
|
|
|
106 |
// scope the call to callback. The function signature is derived
|
|
|
107 |
// from the JavaScript 1.6 Array.some() function. More
|
|
|
108 |
// information on this can be found here:
|
|
|
109 |
// http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:some
|
|
|
110 |
// example:
|
|
|
111 |
// | dojo.some([1, 2, 3, 4], function(item){ return item>1; });
|
|
|
112 |
// returns true
|
|
|
113 |
// example:
|
|
|
114 |
// | dojo.some([1, 2, 3, 4], function(item){ return item<1; });
|
|
|
115 |
// returns false
|
|
|
116 |
return this._everyOrSome(false, arr, callback, thisObject); // Boolean
|
|
|
117 |
},
|
|
|
118 |
|
|
|
119 |
map: function(/*Array*/arr, /*Function*/func, /*Function?*/obj){
|
|
|
120 |
// summary:
|
|
|
121 |
// applies a function to each element of an Array and creates
|
|
|
122 |
// an Array with the results
|
|
|
123 |
// description:
|
|
|
124 |
// Returns a new array constituted from the return values of
|
|
|
125 |
// passing each element of arr into unary_func. The obj parameter
|
|
|
126 |
// may be passed to enable the passed function to be called in
|
|
|
127 |
// that scope. In environments that support JavaScript 1.6, this
|
|
|
128 |
// function is a passthrough to the built-in map() function
|
|
|
129 |
// provided by Array instances. For details on this, see:
|
|
|
130 |
// http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:map
|
|
|
131 |
// example:
|
|
|
132 |
// | dojo.map([1, 2, 3, 4], function(item){ return item+1 });
|
|
|
133 |
// returns [2, 3, 4, 5]
|
|
|
134 |
var _p = _getParts(arr, obj, func); arr = _p[0];
|
|
|
135 |
var outArr = ((arguments[3]) ? (new arguments[3]()) : []);
|
|
|
136 |
for(var i=0;i<arr.length;++i){
|
|
|
137 |
outArr.push(_p[2].call(_p[1], arr[i], i, arr));
|
|
|
138 |
}
|
|
|
139 |
return outArr; // Array
|
|
|
140 |
},
|
|
|
141 |
|
|
|
142 |
filter: function(/*Array*/arr, /*Function*/callback, /*Object?*/obj){
|
|
|
143 |
// summary:
|
|
|
144 |
// Returns a new Array with those items from arr that match the
|
|
|
145 |
// condition implemented by callback. ob may be used to
|
|
|
146 |
// scope the call to callback. The function signature is derived
|
|
|
147 |
// from the JavaScript 1.6 Array.filter() function.
|
|
|
148 |
//
|
|
|
149 |
// More information on the JS 1.6 API can be found here:
|
|
|
150 |
// http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:filter
|
|
|
151 |
// example:
|
|
|
152 |
// | dojo.filter([1, 2, 3, 4], function(item){ return item>1; });
|
|
|
153 |
// returns [2, 3, 4]
|
|
|
154 |
|
|
|
155 |
var _p = _getParts(arr, obj, callback); arr = _p[0];
|
|
|
156 |
var outArr = [];
|
|
|
157 |
for(var i = 0; i < arr.length; i++){
|
|
|
158 |
if(_p[2].call(_p[1], arr[i], i, arr)){
|
|
|
159 |
outArr.push(arr[i]);
|
|
|
160 |
}
|
|
|
161 |
}
|
|
|
162 |
return outArr; // Array
|
|
|
163 |
}
|
|
|
164 |
});
|
|
|
165 |
})();
|
|
|
166 |
|
|
|
167 |
}
|