-
Notifications
You must be signed in to change notification settings - Fork 5
/
matches.js
204 lines (176 loc) · 4.25 KB
/
matches.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var set = require("can-query-logic/src/set");
var canReflect = require("can-reflect");
var dataFromUrl = require("./data-from-url");
var QueryLogic = require("can-query-logic");
function deepEqual(a, b) {
if(a === b) {
return true;
} else if(Array.isArray(a) && Array.isArray(b)) {
if(a.length !== b.length) {
return false;
} else {
return a.every(function(aVal, i){
return deepEqual(aVal, b[i]);
});
}
} else if(a && b && canReflect.isPlainObject(a) && canReflect.isPlainObject(b)) {
var aKeys = Object.keys(a),
bKeys = Object.keys(b);
if(aKeys.length === bKeys.length) {
for(var prop in a) {
if(!b.hasOwnProperty(prop)) {
return false;
}
if(!deepEqual(a[prop], b[prop])) {
return false;
}
}
return true;
} else {
return false;
}
} else {
return false;
}
}
function deepMatches(a, b) {
if(a === b) {
return true;
} else if(Array.isArray(a) && Array.isArray(b)) {
return a.every(function(aVal, i){
return deepMatches(aVal, b[i]);
});
} else if(a && b && canReflect.isPlainObject(a) && canReflect.isPlainObject(b)) {
for(var prop in a) {
if(!b.hasOwnProperty(prop)) {
return false;
}
if(!deepMatches(a[prop], b[prop])) {
return false;
}
}
return true;
} else {
return false
}
}
function removeFixtureAndXHR(query) {
if(query.fixture || query.xhr || query.data) {
var clone = canReflect.serialize(query);
delete clone.fixture;
delete clone.xhr;
delete clone.data;
return clone;
} else {
return query;
}
}
function identityIntersection(v1, v2) {
return v1.value === v2.value ? v1 : set.EMPTY;
}
function identityDifference(v1, v2){
return v1.value === v2.value ? set.EMPTY : v1;
}
function identityUnion(v1, v2) {
return v1.value === v2.value ? v1 : set.UNDEFINABLE;
}
var identityComparitor = {
intersection: identityIntersection,
difference: identityDifference,
union: identityUnion
};
function makeComparatorType(compare) {
var Type = function(){};
var SetType = function(value) {
this.value = value;
};
SetType.prototype.isMember = function(value, root, keys){
return compare(this.value, value, root, keys);
};
canReflect.assignSymbols(Type,{
"can.SetType": SetType
});
set.defineComparison(SetType,SetType, identityComparitor);
set.defineComparison(set.UNIVERSAL,SetType,{
difference: function(){
return set.UNDEFINABLE;
}
});
return Type;
}
function quickEqual(queryA, queryB){
var dataA = queryA.data,
dataB = queryB.data;
if(dataA && dataB) {
if(!deepMatches(dataA, dataB)) {
return false;
}
}
var q1 = new QueryLogic.KeysAnd(removeFixtureAndXHR(queryA)),
q2 = new QueryLogic.KeysAnd(removeFixtureAndXHR(queryB));
return set.isEqual( q1, q2 );
}
function quickSubset(queryA, queryB){
return set.isSubset( new QueryLogic.KeysAnd(queryA), new QueryLogic.KeysAnd(queryB) );
}
// Define types
var types = {};
canReflect.eachKey({
IsEmptyOrNull: function(a, b){
if( a == null && canReflect.size(b) === 0 ) {
return true;
} else if( b == null && canReflect.size(a) === 0 ) {
return true;
} else {
return quickEqual(a, b);
}
},
isEmptyOrSubset: function(a, b) {
if( a == null && canReflect.size(b) === 0 ) {
return true;
} else if( b == null && canReflect.size(a) === 0 ) {
return true;
} else {
return quickSubset(a, b);
}
},
TemplateUrl: function(a, b) {
return !!dataFromUrl(a, b);
},
StringIgnoreCase: function(a, b){
return b && a ? a.toLowerCase() === b.toLowerCase() : b === a;
},
Ignore: function(){
return true;
}
}, function(compare, name){
types[name] = makeComparatorType(compare);
});
var schema = {
identity: ["id"],
keys: {
url: types.TemplateUrl,
fixture: types.Ignore,
xhr: types.Ignore,
type: types.StringIgnoreCase,
method: types.StringIgnoreCase,
helpers: types.Ignore,
headers: types.IsEmptyOrNull,
data: types.IsEmptyOrSubset
}
};
var query = new QueryLogic(schema);
module.exports = {
fixture: quickEqual,
request: function(requestData, fixtureData) {
return query.isMember({filter: fixtureData}, requestData);
},
matches: function(settings, fixture, exact) {
if (exact) {
return this.fixture(settings, fixture);
} else {
return this.request(settings, fixture)
}
},
makeComparatorType: makeComparatorType
};