/ajax/libs/dojo/1.7.0/_base/array.js.uncompressed.js

https://gitlab.com/alidz1982/cdnjs · JavaScript · 344 lines · 126 code · 14 blank · 204 comment · 65 complexity · 528c8f9e6e33defa33fe3c4f8045894d MD5 · raw file

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