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