array.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. define("dojo/_base/array", ["./kernel", "../has", "./lang"], function(dojo, has, lang){
  2. // module:
  3. // dojo/_base/array
  4. // summary:
  5. // This module defines the Javascript v1.6 array extensions.
  6. /*=====
  7. dojo.indexOf = function(arr, value, fromIndex, findLast){
  8. // summary:
  9. // locates the first index of the provided value in the
  10. // passed array. If the value is not found, -1 is returned.
  11. // description:
  12. // This method corresponds to the JavaScript 1.6 Array.indexOf method, with one difference: when
  13. // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
  14. // 1.6's indexOf skips the holes in the sparse array.
  15. // For details on this method, see:
  16. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf
  17. // arr: Array
  18. // value: Object
  19. // fromIndex: Integer?
  20. // findLast: Boolean?
  21. // returns: Number
  22. };
  23. dojo.lastIndexOf = function(arr, value, fromIndex){
  24. // summary:
  25. // locates the last index of the provided value in the passed
  26. // array. If the value is not found, -1 is returned.
  27. // description:
  28. // This method corresponds to the JavaScript 1.6 Array.lastIndexOf method, with one difference: when
  29. // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
  30. // 1.6's lastIndexOf skips the holes in the sparse array.
  31. // For details on this method, see:
  32. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/lastIndexOf
  33. // arr: Array,
  34. // value: Object,
  35. // fromIndex: Integer?
  36. // returns: Number
  37. };
  38. dojo.forEach = function(arr, callback, thisObject){
  39. // summary:
  40. // for every item in arr, callback is invoked. Return values are ignored.
  41. // If you want to break out of the loop, consider using dojo.every() or dojo.some().
  42. // forEach does not allow breaking out of the loop over the items in arr.
  43. // arr:
  44. // the array to iterate over. If a string, operates on individual characters.
  45. // callback:
  46. // a function is invoked with three arguments: item, index, and array
  47. // thisObject:
  48. // may be used to scope the call to callback
  49. // description:
  50. // This function corresponds to the JavaScript 1.6 Array.forEach() method, with one difference: when
  51. // run over sparse arrays, this implementation passes the "holes" in the sparse array to
  52. // the callback function with a value of undefined. JavaScript 1.6's forEach skips the holes in the sparse array.
  53. // For more details, see:
  54. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/forEach
  55. // example:
  56. // | // log out all members of the array:
  57. // | dojo.forEach(
  58. // | [ "thinger", "blah", "howdy", 10 ],
  59. // | function(item){
  60. // | console.log(item);
  61. // | }
  62. // | );
  63. // example:
  64. // | // log out the members and their indexes
  65. // | dojo.forEach(
  66. // | [ "thinger", "blah", "howdy", 10 ],
  67. // | function(item, idx, arr){
  68. // | console.log(item, "at index:", idx);
  69. // | }
  70. // | );
  71. // example:
  72. // | // use a scoped object member as the callback
  73. // |
  74. // | var obj = {
  75. // | prefix: "logged via obj.callback:",
  76. // | callback: function(item){
  77. // | console.log(this.prefix, item);
  78. // | }
  79. // | };
  80. // |
  81. // | // specifying the scope function executes the callback in that scope
  82. // | dojo.forEach(
  83. // | [ "thinger", "blah", "howdy", 10 ],
  84. // | obj.callback,
  85. // | obj
  86. // | );
  87. // |
  88. // | // alternately, we can accomplish the same thing with dojo.hitch()
  89. // | dojo.forEach(
  90. // | [ "thinger", "blah", "howdy", 10 ],
  91. // | dojo.hitch(obj, "callback")
  92. // | );
  93. // arr: Array|String
  94. // callback: Function|String
  95. // thisObject: Object?
  96. };
  97. dojo.every = function(arr, callback, thisObject){
  98. // summary:
  99. // Determines whether or not every item in arr satisfies the
  100. // condition implemented by callback.
  101. // arr: Array|String
  102. // the array to iterate on. If a string, operates on individual characters.
  103. // callback: Function|String
  104. // a function is invoked with three arguments: item, index,
  105. // and array and returns true if the condition is met.
  106. // thisObject: Object?
  107. // may be used to scope the call to callback
  108. // returns: Boolean
  109. // description:
  110. // This function corresponds to the JavaScript 1.6 Array.every() method, with one difference: when
  111. // run over sparse arrays, this implementation passes the "holes" in the sparse array to
  112. // the callback function with a value of undefined. JavaScript 1.6's every skips the holes in the sparse array.
  113. // For more details, see:
  114. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/every
  115. // example:
  116. // | // returns false
  117. // | dojo.every([1, 2, 3, 4], function(item){ return item>1; });
  118. // example:
  119. // | // returns true
  120. // | dojo.every([1, 2, 3, 4], function(item){ return item>0; });
  121. };
  122. dojo.some = function(arr, callback, thisObject){
  123. // summary:
  124. // Determines whether or not any item in arr satisfies the
  125. // condition implemented by callback.
  126. // arr: Array|String
  127. // the array to iterate over. If a string, operates on individual characters.
  128. // callback: Function|String
  129. // a function is invoked with three arguments: item, index,
  130. // and array and returns true if the condition is met.
  131. // thisObject: Object?
  132. // may be used to scope the call to callback
  133. // returns: Boolean
  134. // description:
  135. // This function corresponds to the JavaScript 1.6 Array.some() method, with one difference: when
  136. // run over sparse arrays, this implementation passes the "holes" in the sparse array to
  137. // the callback function with a value of undefined. JavaScript 1.6's some skips the holes in the sparse array.
  138. // For more details, see:
  139. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/some
  140. // example:
  141. // | // is true
  142. // | dojo.some([1, 2, 3, 4], function(item){ return item>1; });
  143. // example:
  144. // | // is false
  145. // | dojo.some([1, 2, 3, 4], function(item){ return item<1; });
  146. };
  147. dojo.map = function(arr, callback, thisObject){
  148. // summary:
  149. // applies callback to each element of arr and returns
  150. // an Array with the results
  151. // arr: Array|String
  152. // the array to iterate on. If a string, operates on
  153. // individual characters.
  154. // callback: Function|String
  155. // a function is invoked with three arguments, (item, index,
  156. // array), and returns a value
  157. // thisObject: Object?
  158. // may be used to scope the call to callback
  159. // returns: Array
  160. // description:
  161. // This function corresponds to the JavaScript 1.6 Array.map() method, with one difference: when
  162. // run over sparse arrays, this implementation passes the "holes" in the sparse array to
  163. // the callback function with a value of undefined. JavaScript 1.6's map skips the holes in the sparse array.
  164. // For more details, see:
  165. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
  166. // example:
  167. // | // returns [2, 3, 4, 5]
  168. // | dojo.map([1, 2, 3, 4], function(item){ return item+1 });
  169. };
  170. dojo.filter = function(arr, callback, thisObject){
  171. // summary:
  172. // Returns a new Array with those items from arr that match the
  173. // condition implemented by callback.
  174. // arr: Array
  175. // the array to iterate over.
  176. // callback: Function|String
  177. // a function that is invoked with three arguments (item,
  178. // index, array). The return of this function is expected to
  179. // be a boolean which determines whether the passed-in item
  180. // will be included in the returned array.
  181. // thisObject: Object?
  182. // may be used to scope the call to callback
  183. // returns: Array
  184. // description:
  185. // This function corresponds to the JavaScript 1.6 Array.filter() method, with one difference: when
  186. // run over sparse arrays, this implementation passes the "holes" in the sparse array to
  187. // the callback function with a value of undefined. JavaScript 1.6's filter skips the holes in the sparse array.
  188. // For more details, see:
  189. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
  190. // example:
  191. // | // returns [2, 3, 4]
  192. // | dojo.filter([1, 2, 3, 4], function(item){ return item>1; });
  193. };
  194. =====*/
  195. // our old simple function builder stuff
  196. var cache = {}, u, array; // the export object
  197. function clearCache(){
  198. cache = {};
  199. }
  200. function buildFn(fn){
  201. return cache[fn] = new Function("item", "index", "array", fn); // Function
  202. }
  203. // magic snippet: if(typeof fn == "string") fn = cache[fn] || buildFn(fn);
  204. // every & some
  205. function everyOrSome(some){
  206. var every = !some;
  207. return function(a, fn, o){
  208. var i = 0, l = a && a.length || 0, result;
  209. if(l && typeof a == "string") a = a.split("");
  210. if(typeof fn == "string") fn = cache[fn] || buildFn(fn);
  211. if(o){
  212. for(; i < l; ++i){
  213. result = !fn.call(o, a[i], i, a);
  214. if(some ^ result){
  215. return !result;
  216. }
  217. }
  218. }else{
  219. for(; i < l; ++i){
  220. result = !fn(a[i], i, a);
  221. if(some ^ result){
  222. return !result;
  223. }
  224. }
  225. }
  226. return every; // Boolean
  227. }
  228. }
  229. // var every = everyOrSome(false), some = everyOrSome(true);
  230. // indexOf, lastIndexOf
  231. function index(up){
  232. var delta = 1, lOver = 0, uOver = 0;
  233. if(!up){
  234. delta = lOver = uOver = -1;
  235. }
  236. return function(a, x, from, last){
  237. if(last && delta > 0){
  238. // TODO: why do we use a non-standard signature? why do we need "last"?
  239. return array.lastIndexOf(a, x, from);
  240. }
  241. var l = a && a.length || 0, end = up ? l + uOver : lOver, i;
  242. if(from === u){
  243. i = up ? lOver : l + uOver;
  244. }else{
  245. if(from < 0){
  246. i = l + from;
  247. if(i < 0){
  248. i = lOver;
  249. }
  250. }else{
  251. i = from >= l ? l + uOver : from;
  252. }
  253. }
  254. if(l && typeof a == "string") a = a.split("");
  255. for(; i != end; i += delta){
  256. if(a[i] == x){
  257. return i; // Number
  258. }
  259. }
  260. return -1; // Number
  261. }
  262. }
  263. // var indexOf = index(true), lastIndexOf = index(false);
  264. function forEach(a, fn, o){
  265. var i = 0, l = a && a.length || 0;
  266. if(l && typeof a == "string") a = a.split("");
  267. if(typeof fn == "string") fn = cache[fn] || buildFn(fn);
  268. if(o){
  269. for(; i < l; ++i){
  270. fn.call(o, a[i], i, a);
  271. }
  272. }else{
  273. for(; i < l; ++i){
  274. fn(a[i], i, a);
  275. }
  276. }
  277. }
  278. function map(a, fn, o, Ctr){
  279. // TODO: why do we have a non-standard signature here? do we need "Ctr"?
  280. var i = 0, l = a && a.length || 0, out = new (Ctr || Array)(l);
  281. if(l && typeof a == "string") a = a.split("");
  282. if(typeof fn == "string") fn = cache[fn] || buildFn(fn);
  283. if(o){
  284. for(; i < l; ++i){
  285. out[i] = fn.call(o, a[i], i, a);
  286. }
  287. }else{
  288. for(; i < l; ++i){
  289. out[i] = fn(a[i], i, a);
  290. }
  291. }
  292. return out; // Array
  293. }
  294. function filter(a, fn, o){
  295. // TODO: do we need "Ctr" here like in map()?
  296. var i = 0, l = a && a.length || 0, out = [], value;
  297. if(l && typeof a == "string") a = a.split("");
  298. if(typeof fn == "string") fn = cache[fn] || buildFn(fn);
  299. if(o){
  300. for(; i < l; ++i){
  301. value = a[i];
  302. if(fn.call(o, value, i, a)){
  303. out.push(value);
  304. }
  305. }
  306. }else{
  307. for(; i < l; ++i){
  308. value = a[i];
  309. if(fn(value, i, a)){
  310. out.push(value);
  311. }
  312. }
  313. }
  314. return out; // Array
  315. }
  316. array = {
  317. every: everyOrSome(false),
  318. some: everyOrSome(true),
  319. indexOf: index(true),
  320. lastIndexOf: index(false),
  321. forEach: forEach,
  322. map: map,
  323. filter: filter,
  324. clearCache: clearCache
  325. };
  326. 1 && lang.mixin(dojo, array);
  327. /*===== return dojo.array; =====*/
  328. return array;
  329. });