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

https://gitlab.com/Mirros/cdnjs · JavaScript · 343 lines · 122 code · 21 blank · 200 comment · 65 complexity · c46e34f9483de120c24c6f8e881193b1 MD5 · raw file

  1. define("dojo/_base/array", ["./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 one difference: when
  135. // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
  136. // 1.6's indexOf skips the holes in the sparse array.
  137. // For details on this method, see:
  138. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/indexOf
  139. // arr: Array
  140. // value: Object
  141. // fromIndex: Integer?
  142. // findLast: Boolean?
  143. // returns: Number
  144. },
  145. =====*/
  146. lastIndexOf: index(false),
  147. /*=====
  148. lastIndexOf: function(arr, value, fromIndex){
  149. // summary:
  150. // locates the last index of the provided value in the passed
  151. // array. If the value is not found, -1 is returned.
  152. // description:
  153. // This method corresponds to the JavaScript 1.6 Array.lastIndexOf method, with one difference: when
  154. // run over sparse arrays, the Dojo function invokes the callback for every index whereas JavaScript
  155. // 1.6's lastIndexOf skips the holes in the sparse array.
  156. // For details on this method, see:
  157. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/lastIndexOf
  158. // arr: Array,
  159. // value: Object,
  160. // fromIndex: Integer?
  161. // returns: Number
  162. },
  163. =====*/
  164. forEach: function(arr, callback, thisObject){
  165. // summary:
  166. // for every item in arr, callback is invoked. Return values are ignored.
  167. // If you want to break out of the loop, consider using array.every() or array.some().
  168. // forEach does not allow breaking out of the loop over the items in arr.
  169. // arr:
  170. // the array to iterate over. If a string, operates on individual characters.
  171. // callback:
  172. // a function is invoked with three arguments: item, index, and array
  173. // thisObject:
  174. // may be used to scope the call to callback
  175. // description:
  176. // This function corresponds to the JavaScript 1.6 Array.forEach() method, with one difference: when
  177. // run over sparse arrays, this implementation passes the "holes" in the sparse array to
  178. // the callback function with a value of undefined. JavaScript 1.6's forEach skips the holes in the sparse array.
  179. // For more details, see:
  180. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/forEach
  181. // example:
  182. // | // log out all members of the array:
  183. // | array.forEach(
  184. // | [ "thinger", "blah", "howdy", 10 ],
  185. // | function(item){
  186. // | console.log(item);
  187. // | }
  188. // | );
  189. // example:
  190. // | // log out the members and their indexes
  191. // | array.forEach(
  192. // | [ "thinger", "blah", "howdy", 10 ],
  193. // | function(item, idx, arr){
  194. // | console.log(item, "at index:", idx);
  195. // | }
  196. // | );
  197. // example:
  198. // | // use a scoped object member as the callback
  199. // |
  200. // | var obj = {
  201. // | prefix: "logged via obj.callback:",
  202. // | callback: function(item){
  203. // | console.log(this.prefix, item);
  204. // | }
  205. // | };
  206. // |
  207. // | // specifying the scope function executes the callback in that scope
  208. // | array.forEach(
  209. // | [ "thinger", "blah", "howdy", 10 ],
  210. // | obj.callback,
  211. // | obj
  212. // | );
  213. // |
  214. // | // alternately, we can accomplish the same thing with lang.hitch()
  215. // | array.forEach(
  216. // | [ "thinger", "blah", "howdy", 10 ],
  217. // | lang.hitch(obj, "callback")
  218. // | );
  219. // arr: Array|String
  220. // callback: Function|String
  221. // thisObject: Object?
  222. var i = 0, l = arr && arr.length || 0;
  223. if(l && typeof arr == "string") arr = arr.split("");
  224. if(typeof callback == "string") callback = cache[callback] || buildFn(callback);
  225. if(thisObject){
  226. for(; i < l; ++i){
  227. callback.call(thisObject, arr[i], i, arr);
  228. }
  229. }else{
  230. for(; i < l; ++i){
  231. callback(arr[i], i, arr);
  232. }
  233. }
  234. },
  235. map: function(arr, callback, thisObject, Ctr){
  236. // summary:
  237. // applies callback to each element of arr and returns
  238. // an Array with the results
  239. // arr: Array|String
  240. // the array to iterate on. If a string, operates on
  241. // individual characters.
  242. // callback: Function|String
  243. // a function is invoked with three arguments, (item, index,
  244. // array), and returns a value
  245. // thisObject: Object?
  246. // may be used to scope the call to callback
  247. // returns: Array
  248. // description:
  249. // This function corresponds to the JavaScript 1.6 Array.map() method, with one difference: when
  250. // run over sparse arrays, this implementation passes the "holes" in the sparse array to
  251. // the callback function with a value of undefined. JavaScript 1.6's map skips the holes in the sparse array.
  252. // For more details, see:
  253. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/map
  254. // example:
  255. // | // returns [2, 3, 4, 5]
  256. // | array.map([1, 2, 3, 4], function(item){ return item+1 });
  257. // TODO: why do we have a non-standard signature here? do we need "Ctr"?
  258. var i = 0, l = arr && arr.length || 0, out = new (Ctr || Array)(l);
  259. if(l && typeof arr == "string") arr = arr.split("");
  260. if(typeof callback == "string") callback = cache[callback] || buildFn(callback);
  261. if(thisObject){
  262. for(; i < l; ++i){
  263. out[i] = callback.call(thisObject, arr[i], i, arr);
  264. }
  265. }else{
  266. for(; i < l; ++i){
  267. out[i] = callback(arr[i], i, arr);
  268. }
  269. }
  270. return out; // Array
  271. },
  272. filter: function(arr, callback, thisObject){
  273. // summary:
  274. // Returns a new Array with those items from arr that match the
  275. // condition implemented by callback.
  276. // arr: Array
  277. // the array to iterate over.
  278. // callback: Function|String
  279. // a function that is invoked with three arguments (item,
  280. // index, array). The return of this function is expected to
  281. // be a boolean which determines whether the passed-in item
  282. // will be included in the returned array.
  283. // thisObject: Object?
  284. // may be used to scope the call to callback
  285. // returns: Array
  286. // description:
  287. // This function corresponds to the JavaScript 1.6 Array.filter() method, with one difference: when
  288. // run over sparse arrays, this implementation passes the "holes" in the sparse array to
  289. // the callback function with a value of undefined. JavaScript 1.6's filter skips the holes in the sparse array.
  290. // For more details, see:
  291. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
  292. // example:
  293. // | // returns [2, 3, 4]
  294. // | array.filter([1, 2, 3, 4], function(item){ return item>1; });
  295. // TODO: do we need "Ctr" here like in map()?
  296. var i = 0, l = arr && arr.length || 0, out = [], value;
  297. if(l && typeof arr == "string") arr = arr.split("");
  298. if(typeof callback == "string") callback = cache[callback] || buildFn(callback);
  299. if(thisObject){
  300. for(; i < l; ++i){
  301. value = arr[i];
  302. if(callback.call(thisObject, value, i, arr)){
  303. out.push(value);
  304. }
  305. }
  306. }else{
  307. for(; i < l; ++i){
  308. value = arr[i];
  309. if(callback(value, i, arr)){
  310. out.push(value);
  311. }
  312. }
  313. }
  314. return out; // Array
  315. },
  316. clearCache: function(){
  317. cache = {};
  318. }
  319. };
  320. 1 && lang.mixin(dojo, array);
  321. return array;
  322. });