dd093eab691bdba189061e32eb326bf4f3ac8451.svn-base 6.6 KB

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