array.js 12 KB

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