-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathequals.js
81 lines (69 loc) · 2.56 KB
/
equals.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
define([
"dojo/_base/array",
"dojo/_base/lang",
"dojo/Stateful",
"./StatefulArray"
], function(array, lang, Stateful, StatefulArray){
var equalsOptions = {
// summary:
// Options used for dojox/mvc/equals().
getType: function(/*Anything*/ v){
// summary:
// Returns the type of the given value.
// v: Anything
// The value.
return lang.isArray(v) ? "array" : lang.isFunction((v || {}).getTime) ? "date" : v != null && ({}.toString.call(v) == "[object Object]" || lang.isFunction((v || {}).set) && lang.isFunction((v || {}).watch)) ? "object" : "value";
},
equalsArray: function(/*Anything[]*/ dst, /*Anything[]*/ src){
// summary:
// Returns if the given two stateful arrays are equal.
// dst: Anything[]
// The array to compare with.
// src: Anything[]
// The array to compare with.
for(var i = 0, l = Math.max(dst.length, src.length); i < l; i++){
if(!equals(dst[i], src[i])){ return false; }
}
return true;
},
equalsDate: function(/*Date*/ dst, /*Date*/ src){
return dst.getTime() == src.getTime();
},
equalsObject: function(/*Object*/ dst, /*Object*/ src){
// summary:
// Returns if the given two stateful objects are equal.
// dst: Object
// The object to compare with.
// src: Object
// The object to compare with.
var list = lang.mixin({}, dst, src);
for(var s in list){
if(!(s in Stateful.prototype) && s != "_watchCallbacks" && !equals(dst[s], src[s])){ return false; }
}
return true;
},
equalsValue: function(/*Anything*/ dst, /*Anything*/ src){
// summary:
// Returns if the given two values are equal.
return dst === src; // Boolean
}
};
var equals = function(/*Anything*/ dst, /*Anything*/ src, /*dojox/mvc/equalsOptions*/ options){
// summary:
// Compares two dojo/Stateful objects, by diving into the leaves.
// description:
// Recursively iterates and compares stateful values.
// dst: Anything
// The stateful value to compare with.
// src: Anything
// The stateful value to compare with.
// options: dojox/mvc/equalsOptions
// The object that defines how two stateful values are compared.
// returns: Boolean
// True if dst equals to src, false otherwise.
var opts = options || equals, types = [opts.getType(dst), opts.getType(src)];
return types[0] != types[1] ? false : opts["equals" + types[0].replace(/^[a-z]/, function(c){ return c.toUpperCase(); })](dst, src); // Boolean
};
// lang.setObject() thing is for back-compat, remove it in 2.0
return lang.setObject("dojox.mvc.equals", lang.mixin(equals, equalsOptions));
});