source: Dev/branches/rest-dojo-ui/client/dojox/lang/functional/reversed.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

  • Property svn:executable set to *
File size: 2.8 KB
Line 
1define(["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
6// Notes:
7//      - this module provides reversed versions of standard array-processing functions:
8//              forEachRev, mapRev, filterRev
9
10// Defined methods:
11//      - take any valid lambda argument as the functional argument
12//      - operate on dense arrays
13//      - take a string as the array argument
14
15/*=====
16        var df = dojox.lang.functional;
17 =====*/
18        lang.mixin(df, {
19                // JS 1.6 standard array functions, which can take a lambda as a parameter.
20                // Consider using dojo._base.array functions, if you don't need the lambda support.
21                filterRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
22                        // summary: creates a new array with all elements that pass the test
23                        //      implemented by the provided function.
24                        if(typeof a == "string"){ a = a.split(""); }
25                        o = o || win.global; f = df.lambda(f);
26                        var t = [], v, i = a.length - 1;
27                        for(; i >= 0; --i){
28                                v = a[i];
29                                if(f.call(o, v, i, a)){ t.push(v); }
30                        }
31                        return t;       // Array
32                },
33                forEachRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
34                        // summary: executes a provided function once per array element.
35                        if(typeof a == "string"){ a = a.split(""); }
36                        o = o || win.global; f = df.lambda(f);
37                        for(var i = a.length - 1; i >= 0; f.call(o, a[i], i, a), --i);
38                },
39                mapRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
40                        // summary: creates a new array with the results of calling
41                        //      a provided function on every element in this array.
42                        if(typeof a == "string"){ a = a.split(""); }
43                        o = o || win.global; f = df.lambda(f);
44                        var n = a.length, t = new Array(n), i = n - 1, j = 0;
45                        for(; i >= 0; t[j++] = f.call(o, a[i], i, a), --i);
46                        return t;       // Array
47                },
48                everyRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
49                        // summary: tests whether all elements in the array pass the test
50                        //      implemented by the provided function.
51                        if(typeof a == "string"){ a = a.split(""); }
52                        o = o || win.global; f = df.lambda(f);
53                        for(var i = a.length - 1; i >= 0; --i){
54                                if(!f.call(o, a[i], i, a)){
55                                        return false;   // Boolean
56                                }
57                        }
58                        return true;    // Boolean
59                },
60                someRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
61                        // summary: tests whether some element in the array passes the test
62                        //      implemented by the provided function.
63                        if(typeof a == "string"){ a = a.split(""); }
64                        o = o || win.global; f = df.lambda(f);
65                        for(var i = a.length - 1; i >= 0; --i){
66                                if(f.call(o, a[i], i, a)){
67                                        return true;    // Boolean
68                                }
69                        }
70                        return false;   // Boolean
71                }
72        });
73       
74        return df;
75});
Note: See TracBrowser for help on using the repository browser.