source: Dev/branches/rest-dojo-ui/client/dojox/lang/functional/array.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: 4.8 KB
Line 
1define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/array", "dojo/_base/window", "./lambda"],
2        function(dojo, lang, arr, win, df){
3
4// This module adds high-level functions and related constructs:
5//      - array-processing functions similar to standard JS functions
6
7// Notes:
8//      - this module provides JS standard methods similar to high-level functions in dojo/_base/array.js:
9//              forEach, map, filter, every, some
10
11// Defined methods:
12//      - take any valid lambda argument as the functional argument
13//      - operate on dense arrays
14//      - take a string as the array argument
15//      - take an iterator objects as the array argument
16
17        var empty = {};
18
19/*=====
20        var df = dojox.lang.functional;
21 =====*/
22        lang.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 || win.global; f = df.lambda(f);
30                        var t = [], v, i, n;
31                        if(lang.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 || win.global; f = df.lambda(f);
58                        var i, n;
59                        if(lang.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 || win.global; f = df.lambda(f);
80                        var t, n, i;
81                        if(lang.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 || win.global; f = df.lambda(f);
105                        var i, n;
106                        if(lang.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 || win.global; f = df.lambda(f);
137                        var i, n;
138                        if(lang.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        return df;
167});
Note: See TracBrowser for help on using the repository browser.