source: Dev/branches/rest-dojo-ui/client/dojox/lang/utils.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).

File size: 3.3 KB
Line 
1define(["..", "dojo/_base/lang"],
2  function(dojox, lang){
3        var du = lang.getObject("lang.utils", true, dojox);
4       
5        var empty = {}, opts = Object.prototype.toString;
6
7        var clone = function(o){
8                if(o){
9                        switch(opts.call(o)){
10                                case "[object Array]":
11                                        return o.slice(0);
12                                case "[object Object]":
13                                        return lang.delegate(o);
14                        }
15                }
16                return o;
17        }
18       
19        lang.mixin(du, {
20                coerceType: function(target, source){
21                        // summary: Coerces one object to the type of another.
22                        // target: Object: object, which typeof result is used to coerce "source" object.
23                        // source: Object: object, which will be forced to change type.
24                        switch(typeof target){
25                                case "number":  return Number(eval("(" + source + ")"));
26                                case "string":  return String(source);
27                                case "boolean": return Boolean(eval("(" + source + ")"));
28                        }
29                        return eval("(" + source + ")");
30                },
31               
32                updateWithObject: function(target, source, conv){
33                        // summary: Updates an existing object in place with properties from an "source" object.
34                        // target: Object: the "target" object to be updated
35                        // source: Object: the "source" object, whose properties will be used to source the existed object.
36                        // conv: Boolean?: force conversion to the original type
37                        if(!source){ return target; }
38                        for(var x in target){
39                                if(x in source && !(x in empty)){
40                                        var t = target[x];
41                                        if(t && typeof t == "object"){
42                                                du.updateWithObject(t, source[x], conv);
43                                        }else{
44                                                target[x] = conv ? du.coerceType(t, source[x]) : clone(source[x]);
45                                        }
46                                }
47                        }
48                        return target;  // Object
49                },
50       
51                updateWithPattern: function(target, source, pattern, conv){
52                        // summary: Updates an existing object in place with properties from an "source" object.
53                        // target: Object: the "target" object to be updated
54                        // source: Object: the "source" object, whose properties will be used to source the existed object.
55                        // pattern: Object: object, whose properties will be used to pull values from the "source"
56                        // conv: Boolean?: force conversion to the original type
57                        if(!source || !pattern){ return target; }
58                        for(var x in pattern){
59                                if(x in source && !(x in empty)){
60                                        target[x] = conv ? du.coerceType(pattern[x], source[x]) : clone(source[x]);
61                                }
62                        }
63                        return target;  // Object
64                },
65               
66                merge: function(object, mixin){
67                        // summary: Merge two objects structurally, mixin properties will override object's properties.
68                        // object: Object: original object.
69                        // mixin: Object: additional object, which properties will override object's properties.
70                        if(mixin){
71                                var otype = opts.call(object), mtype = opts.call(mixin), t, i, l, m;
72                                switch(mtype){
73                                        case "[object Array]":
74                                                if(mtype == otype){
75                                                        t = new Array(Math.max(object.length, mixin.length));
76                                                        for(i = 0, l = t.length; i < l; ++i){
77                                                                t[i] = du.merge(object[i], mixin[i]);
78                                                        }
79                                                        return t;
80                                                }
81                                                return mixin.slice(0);
82                                        case "[object Object]":
83                                                if(mtype == otype && object){
84                                                        t = lang.delegate(object);
85                                                        for(i in mixin){
86                                                                if(i in object){
87                                                                        l = object[i];
88                                                                        m = mixin[i];
89                                                                        if(m !== l){
90                                                                                t[i] = du.merge(l, m);
91                                                                        }
92                                                                }else{
93                                                                        t[i] = lang.clone(mixin[i]);
94                                                                }
95                                                        }
96                                                        return t;
97                                                }
98                                                return lang.clone(mixin);
99                                }
100                        }
101                        return mixin;
102                }
103        });
104       
105        return du;
106});
Note: See TracBrowser for help on using the repository browser.