1 | define(["..", "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: |
---|
22 | // Coerces one object to the type of another. |
---|
23 | // target: Object |
---|
24 | // object, which typeof result is used to coerce "source" object. |
---|
25 | // source: Object |
---|
26 | // object, which will be forced to change type. |
---|
27 | switch(typeof target){ |
---|
28 | case "number": return Number(eval("(" + source + ")")); |
---|
29 | case "string": return String(source); |
---|
30 | case "boolean": return Boolean(eval("(" + source + ")")); |
---|
31 | } |
---|
32 | return eval("(" + source + ")"); |
---|
33 | }, |
---|
34 | |
---|
35 | updateWithObject: function(target, source, conv){ |
---|
36 | // summary: |
---|
37 | // Updates an existing object in place with properties from an "source" object. |
---|
38 | // target: Object |
---|
39 | // the "target" object to be updated |
---|
40 | // source: Object |
---|
41 | // the "source" object, whose properties will be used to source the existed object. |
---|
42 | // conv: Boolean? |
---|
43 | // force conversion to the original type |
---|
44 | if(!source){ return target; } |
---|
45 | for(var x in target){ |
---|
46 | if(x in source && !(x in empty)){ |
---|
47 | var t = target[x]; |
---|
48 | if(t && typeof t == "object"){ |
---|
49 | du.updateWithObject(t, source[x], conv); |
---|
50 | }else{ |
---|
51 | target[x] = conv ? du.coerceType(t, source[x]) : clone(source[x]); |
---|
52 | } |
---|
53 | } |
---|
54 | } |
---|
55 | return target; // Object |
---|
56 | }, |
---|
57 | |
---|
58 | updateWithPattern: function(target, source, pattern, conv){ |
---|
59 | // summary: |
---|
60 | // Updates an existing object in place with properties from an "source" object. |
---|
61 | // target: Object |
---|
62 | // the "target" object to be updated |
---|
63 | // source: Object |
---|
64 | // the "source" object, whose properties will be used to source the existed object. |
---|
65 | // pattern: Object |
---|
66 | // object, whose properties will be used to pull values from the "source" |
---|
67 | // conv: Boolean? |
---|
68 | // force conversion to the original type |
---|
69 | if(!source || !pattern){ return target; } |
---|
70 | for(var x in pattern){ |
---|
71 | if(x in source && !(x in empty)){ |
---|
72 | target[x] = conv ? du.coerceType(pattern[x], source[x]) : clone(source[x]); |
---|
73 | } |
---|
74 | } |
---|
75 | return target; // Object |
---|
76 | }, |
---|
77 | |
---|
78 | merge: function(object, mixin){ |
---|
79 | // summary: |
---|
80 | // Merge two objects structurally, mixin properties will override object's properties. |
---|
81 | // object: Object |
---|
82 | // original object. |
---|
83 | // mixin: Object |
---|
84 | // additional object, which properties will override object's properties. |
---|
85 | if(mixin){ |
---|
86 | var otype = opts.call(object), mtype = opts.call(mixin), t, i, l, m; |
---|
87 | switch(mtype){ |
---|
88 | case "[object Array]": |
---|
89 | if(mtype == otype){ |
---|
90 | t = new Array(Math.max(object.length, mixin.length)); |
---|
91 | for(i = 0, l = t.length; i < l; ++i){ |
---|
92 | t[i] = du.merge(object[i], mixin[i]); |
---|
93 | } |
---|
94 | return t; |
---|
95 | } |
---|
96 | return mixin.slice(0); |
---|
97 | case "[object Object]": |
---|
98 | if(mtype == otype && object){ |
---|
99 | t = lang.delegate(object); |
---|
100 | for(i in mixin){ |
---|
101 | if(i in object){ |
---|
102 | l = object[i]; |
---|
103 | m = mixin[i]; |
---|
104 | if(m !== l){ |
---|
105 | t[i] = du.merge(l, m); |
---|
106 | } |
---|
107 | }else{ |
---|
108 | t[i] = lang.clone(mixin[i]); |
---|
109 | } |
---|
110 | } |
---|
111 | return t; |
---|
112 | } |
---|
113 | return lang.clone(mixin); |
---|
114 | } |
---|
115 | } |
---|
116 | return mixin; |
---|
117 | } |
---|
118 | }); |
---|
119 | |
---|
120 | return du; |
---|
121 | }); |
---|