reversed.js 3.2 KB

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