[483] | 1 | define(["dojo/_base/kernel", "dojo/_base/lang", "./lambda"], function(kernel, lang, 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 | lang.mixin(df, { |
---|
| 14 | // object helpers |
---|
| 15 | keys: function(/*Object*/ obj){ |
---|
| 16 | // summary: |
---|
| 17 | // returns an array of all keys in the object |
---|
| 18 | var t = []; |
---|
| 19 | for(var i in obj){ |
---|
| 20 | if(!(i in empty)){ |
---|
| 21 | t.push(i); |
---|
| 22 | } |
---|
| 23 | } |
---|
| 24 | return t; // Array |
---|
| 25 | }, |
---|
| 26 | values: function(/*Object*/ obj){ |
---|
| 27 | // summary: |
---|
| 28 | // returns an array of all values in the object |
---|
| 29 | var t = []; |
---|
| 30 | for(var i in obj){ |
---|
| 31 | if(!(i in empty)){ |
---|
| 32 | t.push(obj[i]); |
---|
| 33 | } |
---|
| 34 | } |
---|
| 35 | return t; // Array |
---|
| 36 | }, |
---|
| 37 | filterIn: function(/*Object*/ obj, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 38 | // summary: |
---|
| 39 | // creates new object with all attributes that pass the test |
---|
| 40 | // implemented by the provided function. |
---|
| 41 | o = o || kernel.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: |
---|
| 53 | // iterates over all object attributes. |
---|
| 54 | o = o || kernel.global; f = df.lambda(f); |
---|
| 55 | for(var i in obj){ |
---|
| 56 | if(!(i in empty)){ |
---|
| 57 | f.call(o, obj[i], i, obj); |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | return o; // Object |
---|
| 61 | }, |
---|
| 62 | mapIn: function(/*Object*/ obj, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 63 | // summary: |
---|
| 64 | // creates new object with the results of calling |
---|
| 65 | // a provided function on every attribute in this object. |
---|
| 66 | o = o || kernel.global; f = df.lambda(f); |
---|
| 67 | var t = {}, i; |
---|
| 68 | for(i in obj){ |
---|
| 69 | if(!(i in empty)){ |
---|
| 70 | t[i] = f.call(o, obj[i], i, obj); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | return t; // Object |
---|
| 74 | } |
---|
| 75 | }); |
---|
| 76 | |
---|
| 77 | return df; |
---|
| 78 | }); |
---|