array.js 4.8 KB

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