source: [view]
define("dojo/_base/array", ["dojo/lib/kernel", "dojo/_base/lang"], function(dojo){
//>>excludeStart("webkitMobile", kwArgs.webkitMobile);
(function(){
var _getParts = function(arr, obj, cb){
return [
(typeof arr == "string") ? arr.split("") : arr,
obj || dojo.global,
// FIXME: cache the anonymous functions we create here?
(typeof cb == "string") ? new Function("item", "index", "array", cb) : cb
];
};
var everyOrSome = function(/*Boolean*/every, /*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){
var _p = _getParts(arr, thisObject, callback); arr = _p[0];
for(var i=0,l=arr.length; i var result = !!_p[2].call(_p[1], arr[i], i, arr);
if(every ^ result){
return result; // Boolean
}
}
return every; // Boolean
};
dojo.mixin(dojo, {
indexOf: function( /*Array*/ array,
/*Object*/ value,
/*Integer?*/ fromIndex,
/*Boolean?*/ findLast){
// summary:
// locates the first index of the provided value in the
// passed array. If the value is not found, -1 is returned.
// description:
// This method corresponds to the JavaScript 1.6 Array.indexOf method, with one difference: when
// run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
// 1.6's indexOf skips the holes in the sparse array.
// For details on this method, see:
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf
var step = 1, end = array.length || 0, i = 0;
if(findLast){
i = end - 1;
step = end = -1;
}
if(fromIndex != undefined){ i = fromIndex; }
if((findLast && i > end) || i < end){
for(; i != end; i += step){
if(array[i] == value){ return i; }
}
}
return -1; // Number
},
lastIndexOf: function(/*Array*/array, /*Object*/value, /*Integer?*/fromIndex){
// summary:
// locates the last index of the provided value in the passed
// array. If the value is not found, -1 is returned.
// description:
// This method corresponds to the JavaScript 1.6 Array.lastIndexOf method, with one difference: when
// run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
// 1.6's lastIndexOf skips the holes in the sparse array.
// For details on this method, see:
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/lastIndexOf
return dojo.indexOf(array, value, fromIndex, true); // Number
},
forEach: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){
// summary:
// for every item in arr, callback is invoked. Return values are ignored.
// If you want to break out of the loop, consider using dojo.every() or dojo.some().
// forEach does not allow breaking out of the loop over the items in arr.
// arr:
// the array to iterate over. If a string, operates on individual characters.
// callback:
// a function is invoked with three arguments: item, index, and array
// thisObject:
// may be used to scope the call to callback
// description:
// This function corresponds to the JavaScript 1.6 Array.forEach() method, with one difference: when
// run over sparse arrays, this implemenation passes the "holes" in the sparse array to
// the callback function with a value of undefined. JavaScript 1.6's forEach skips the holes in the sparse array.
// For more details, see:
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/forEach
// example:
// | // log out all members of the array:
// | dojo.forEach(
// | [ "thinger", "blah", "howdy", 10 ],
// | function(item){
// | console.log(item);
// | }
// | );
// example:
// | // log out the members and their indexes
// | dojo.forEach(
// | [ "thinger", "blah", "howdy", 10 ],
// | function(item, idx, arr){
// | console.log(item, "at index:", idx);
// | }
// | );
// example:
// | // use a scoped object member as the callback
// |
// | var obj = {
// | prefix: "logged via obj.callback:",
// | callback: function(item){
// | console.log(this.prefix, item);
// | }
// | };
// |
// | // specifying the scope function executes the callback in that scope
// | dojo.forEach(
// | [ "thinger", "blah", "howdy", 10 ],
// | obj.callback,
// | obj
// | );
// |
// | // alternately, we can accomplish the same thing with dojo.hitch()
// | dojo.forEach(
// | [ "thinger", "blah", "howdy", 10 ],
// | dojo.hitch(obj, "callback")
// | );
// match the behavior of the built-in forEach WRT empty arrs
if(!arr || !arr.length){ return; }
// FIXME: there are several ways of handilng thisObject. Is
// dojo.global always the default context?
var _p = _getParts(arr, thisObject, callback); arr = _p[0];
for(var i=0,l=arr.length; i _p[2].call(_p[1], arr[i], i, arr);
}