1 | define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/window", "./lambda"], function(dojo, lang, win, df){ |
---|
2 | |
---|
3 | // This module adds high-level functions and related constructs: |
---|
4 | // - object/dictionary helpers |
---|
5 | |
---|
6 | // Defined methods: |
---|
7 | // - take any valid lambda argument as the functional argument |
---|
8 | // - skip all attributes that are present in the empty object |
---|
9 | // (IE and/or 3rd-party libraries). |
---|
10 | |
---|
11 | var empty = {}; |
---|
12 | |
---|
13 | /*===== |
---|
14 | var df = dojox.lang.functional; |
---|
15 | =====*/ |
---|
16 | lang.mixin(df, { |
---|
17 | // object helpers |
---|
18 | keys: function(/*Object*/ obj){ |
---|
19 | // summary: returns an array of all keys in the object |
---|
20 | var t = []; |
---|
21 | for(var i in obj){ |
---|
22 | if(!(i in empty)){ |
---|
23 | t.push(i); |
---|
24 | } |
---|
25 | } |
---|
26 | return t; // Array |
---|
27 | }, |
---|
28 | values: function(/*Object*/ obj){ |
---|
29 | // summary: returns an array of all values in the object |
---|
30 | var t = []; |
---|
31 | for(var i in obj){ |
---|
32 | if(!(i in empty)){ |
---|
33 | t.push(obj[i]); |
---|
34 | } |
---|
35 | } |
---|
36 | return t; // Array |
---|
37 | }, |
---|
38 | filterIn: function(/*Object*/ obj, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
39 | // summary: creates new object with all attributes that pass the test |
---|
40 | // implemented by the provided function. |
---|
41 | o = o || win.global; f = df.lambda(f); |
---|
42 | var t = {}, v, i; |
---|
43 | for(i in obj){ |
---|
44 | if(!(i in empty)){ |
---|
45 | v = obj[i]; |
---|
46 | if(f.call(o, v, i, obj)){ t[i] = v; } |
---|
47 | } |
---|
48 | } |
---|
49 | return t; // Object |
---|
50 | }, |
---|
51 | forIn: function(/*Object*/ obj, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
52 | // summary: iterates over all object attributes. |
---|
53 | o = o || win.global; f = df.lambda(f); |
---|
54 | for(var i in obj){ |
---|
55 | if(!(i in empty)){ |
---|
56 | f.call(o, obj[i], i, obj); |
---|
57 | } |
---|
58 | } |
---|
59 | return o; // Object |
---|
60 | }, |
---|
61 | mapIn: function(/*Object*/ obj, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
62 | // summary: creates new object with the results of calling |
---|
63 | // a provided function on every attribute in this object. |
---|
64 | o = o || win.global; f = df.lambda(f); |
---|
65 | var t = {}, i; |
---|
66 | for(i in obj){ |
---|
67 | if(!(i in empty)){ |
---|
68 | t[i] = f.call(o, obj[i], i, obj); |
---|
69 | } |
---|
70 | } |
---|
71 | return t; // Object |
---|
72 | } |
---|
73 | }); |
---|
74 | |
---|
75 | return df; |
---|
76 | }); |
---|