array.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.lang.functional.array"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.functional.array"] = true;
  8. dojo.provide("dojox.lang.functional.array");
  9. dojo.require("dojox.lang.functional.lambda");
  10. // This module adds high-level functions and related constructs:
  11. // - array-processing functions similar to standard JS functions
  12. // Notes:
  13. // - this module provides JS standard methods similar to high-level functions in dojo/_base/array.js:
  14. // forEach, map, filter, every, some
  15. // Defined methods:
  16. // - take any valid lambda argument as the functional argument
  17. // - operate on dense arrays
  18. // - take a string as the array argument
  19. // - take an iterator objects as the array argument
  20. (function(){
  21. var d = dojo, df = dojox.lang.functional, empty = {};
  22. d.mixin(df, {
  23. // JS 1.6 standard array functions, which can take a lambda as a parameter.
  24. // Consider using dojo._base.array functions, if you don't need the lambda support.
  25. filter: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  26. // summary: creates a new array with all elements that pass the test
  27. // implemented by the provided function.
  28. if(typeof a == "string"){ a = a.split(""); }
  29. o = o || d.global; f = df.lambda(f);
  30. var t = [], v, i, n;
  31. if(d.isArray(a)){
  32. // array
  33. for(i = 0, n = a.length; i < n; ++i){
  34. v = a[i];
  35. if(f.call(o, v, i, a)){ t.push(v); }
  36. }
  37. }else if(typeof a.hasNext == "function" && typeof a.next == "function"){
  38. // iterator
  39. for(i = 0; a.hasNext();){
  40. v = a.next();
  41. if(f.call(o, v, i++, a)){ t.push(v); }
  42. }
  43. }else{
  44. // object/dictionary
  45. for(i in a){
  46. if(!(i in empty)){
  47. v = a[i];
  48. if(f.call(o, v, i, a)){ t.push(v); }
  49. }
  50. }
  51. }
  52. return t; // Array
  53. },
  54. forEach: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  55. // summary: executes a provided function once per array element.
  56. if(typeof a == "string"){ a = a.split(""); }
  57. o = o || d.global; f = df.lambda(f);
  58. var i, n;
  59. if(d.isArray(a)){
  60. // array
  61. for(i = 0, n = a.length; i < n; f.call(o, a[i], i, a), ++i);
  62. }else if(typeof a.hasNext == "function" && typeof a.next == "function"){
  63. // iterator
  64. for(i = 0; a.hasNext(); f.call(o, a.next(), i++, a));
  65. }else{
  66. // object/dictionary
  67. for(i in a){
  68. if(!(i in empty)){
  69. f.call(o, a[i], i, a);
  70. }
  71. }
  72. }
  73. return o; // Object
  74. },
  75. map: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  76. // summary: creates a new array with the results of calling
  77. // a provided function on every element in this array.
  78. if(typeof a == "string"){ a = a.split(""); }
  79. o = o || d.global; f = df.lambda(f);
  80. var t, n, i;
  81. if(d.isArray(a)){
  82. // array
  83. t = new Array(n = a.length);
  84. for(i = 0; i < n; t[i] = f.call(o, a[i], i, a), ++i);
  85. }else if(typeof a.hasNext == "function" && typeof a.next == "function"){
  86. // iterator
  87. t = [];
  88. for(i = 0; a.hasNext(); t.push(f.call(o, a.next(), i++, a)));
  89. }else{
  90. // object/dictionary
  91. t = [];
  92. for(i in a){
  93. if(!(i in empty)){
  94. t.push(f.call(o, a[i], i, a));
  95. }
  96. }
  97. }
  98. return t; // Array
  99. },
  100. every: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  101. // summary: tests whether all elements in the array pass the test
  102. // implemented by the provided function.
  103. if(typeof a == "string"){ a = a.split(""); }
  104. o = o || d.global; f = df.lambda(f);
  105. var i, n;
  106. if(d.isArray(a)){
  107. // array
  108. for(i = 0, n = a.length; i < n; ++i){
  109. if(!f.call(o, a[i], i, a)){
  110. return false; // Boolean
  111. }
  112. }
  113. }else if(typeof a.hasNext == "function" && typeof a.next == "function"){
  114. // iterator
  115. for(i = 0; a.hasNext();){
  116. if(!f.call(o, a.next(), i++, a)){
  117. return false; // Boolean
  118. }
  119. }
  120. }else{
  121. // object/dictionary
  122. for(i in a){
  123. if(!(i in empty)){
  124. if(!f.call(o, a[i], i, a)){
  125. return false; // Boolean
  126. }
  127. }
  128. }
  129. }
  130. return true; // Boolean
  131. },
  132. some: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  133. // summary: tests whether some element in the array passes the test
  134. // implemented by the provided function.
  135. if(typeof a == "string"){ a = a.split(""); }
  136. o = o || d.global; f = df.lambda(f);
  137. var i, n;
  138. if(d.isArray(a)){
  139. // array
  140. for(i = 0, n = a.length; i < n; ++i){
  141. if(f.call(o, a[i], i, a)){
  142. return true; // Boolean
  143. }
  144. }
  145. }else if(typeof a.hasNext == "function" && typeof a.next == "function"){
  146. // iterator
  147. for(i = 0; a.hasNext();){
  148. if(f.call(o, a.next(), i++, a)){
  149. return true; // Boolean
  150. }
  151. }
  152. }else{
  153. // object/dictionary
  154. for(i in a){
  155. if(!(i in empty)){
  156. if(f.call(o, a[i], i, a)){
  157. return true; // Boolean
  158. }
  159. }
  160. }
  161. }
  162. return false; // Boolean
  163. }
  164. });
  165. })();
  166. }