reversed.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. define("dojox/lang/functional/reversed", ["dojo/_base/lang", "dojo/_base/window" ,"./lambda"],
  2. function(lang, win, df){
  3. // This module adds high-level functions and related constructs:
  4. // - reversed versions of array-processing functions similar to standard JS functions
  5. // Notes:
  6. // - this module provides reversed versions of standard array-processing functions:
  7. // forEachRev, mapRev, filterRev
  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. /*=====
  13. var df = dojox.lang.functional;
  14. =====*/
  15. lang.mixin(df, {
  16. // JS 1.6 standard array functions, which can take a lambda as a parameter.
  17. // Consider using dojo._base.array functions, if you don't need the lambda support.
  18. filterRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  19. // summary: creates a new array with all elements that pass the test
  20. // implemented by the provided function.
  21. if(typeof a == "string"){ a = a.split(""); }
  22. o = o || win.global; f = df.lambda(f);
  23. var t = [], v, i = a.length - 1;
  24. for(; i >= 0; --i){
  25. v = a[i];
  26. if(f.call(o, v, i, a)){ t.push(v); }
  27. }
  28. return t; // Array
  29. },
  30. forEachRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  31. // summary: executes a provided function once per array element.
  32. if(typeof a == "string"){ a = a.split(""); }
  33. o = o || win.global; f = df.lambda(f);
  34. for(var i = a.length - 1; i >= 0; f.call(o, a[i], i, a), --i);
  35. },
  36. mapRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  37. // summary: creates a new array with the results of calling
  38. // a provided function on every element in this array.
  39. if(typeof a == "string"){ a = a.split(""); }
  40. o = o || win.global; f = df.lambda(f);
  41. var n = a.length, t = new Array(n), i = n - 1, j = 0;
  42. for(; i >= 0; t[j++] = f.call(o, a[i], i, a), --i);
  43. return t; // Array
  44. },
  45. everyRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  46. // summary: tests whether all elements in the array pass the test
  47. // implemented by the provided function.
  48. if(typeof a == "string"){ a = a.split(""); }
  49. o = o || win.global; f = df.lambda(f);
  50. for(var i = a.length - 1; i >= 0; --i){
  51. if(!f.call(o, a[i], i, a)){
  52. return false; // Boolean
  53. }
  54. }
  55. return true; // Boolean
  56. },
  57. someRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
  58. // summary: tests whether some element in the array passes the test
  59. // implemented by the provided function.
  60. if(typeof a == "string"){ a = a.split(""); }
  61. o = o || win.global; f = df.lambda(f);
  62. for(var i = a.length - 1; i >= 0; --i){
  63. if(f.call(o, a[i], i, a)){
  64. return true; // Boolean
  65. }
  66. }
  67. return false; // Boolean
  68. }
  69. });
  70. return df;
  71. });