-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
320 lines (253 loc) · 8.02 KB
/
index.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import t from "should-type";
import format from "./format";
var hasOwnProperty = Object.prototype.hasOwnProperty;
function EqualityFail(a, b, reason, path) {
this.a = a;
this.b = b;
this.reason = reason;
this.path = path;
}
function typeToString(tp) {
return tp.type + (tp.cls ? "(" + tp.cls + (tp.sub ? " " + tp.sub : "") + ")" : "");
}
var PLUS_0_AND_MINUS_0 = "+0 is not equal to -0";
var DIFFERENT_TYPES = "A has type %s and B has type %s";
var EQUALITY = "A is not equal to B";
var EQUALITY_PROTOTYPE = "A and B have different prototypes";
var WRAPPED_VALUE = "A wrapped value is not equal to B wrapped value";
var FUNCTION_SOURCES = "function A is not equal to B by source code value (via .toString call)";
var MISSING_KEY = "%s has no key %s";
var SET_MAP_MISSING_KEY = "Set/Map missing key %s";
var DEFAULT_OPTIONS = {
checkProtoEql: true,
checkSubType: true,
plusZeroAndMinusZeroEqual: true,
collectAllFails: false
};
function setBooleanDefault(property, obj, opts, defaults) {
obj[property] = typeof opts[property] !== "boolean" ? defaults[property] : opts[property];
}
var METHOD_PREFIX = "_check_";
function EQ(opts, a, b, path) {
opts = opts || {};
setBooleanDefault("checkProtoEql", this, opts, DEFAULT_OPTIONS);
setBooleanDefault("plusZeroAndMinusZeroEqual", this, opts, DEFAULT_OPTIONS);
setBooleanDefault("checkSubType", this, opts, DEFAULT_OPTIONS);
setBooleanDefault("collectAllFails", this, opts, DEFAULT_OPTIONS);
this.a = a;
this.b = b;
this._meet = opts._meet || [];
this.fails = opts.fails || [];
this.path = path || [];
}
function ShortcutError(fail) {
this.name = "ShortcutError";
this.message = "fail fast";
this.fail = fail;
}
ShortcutError.prototype = Object.create(Error.prototype);
EQ.checkStrictEquality = function(a, b) {
this.collectFail(a !== b, EQUALITY);
};
EQ.add = function add(type, cls, sub, f) {
var args = Array.prototype.slice.call(arguments);
f = args.pop();
EQ.prototype[METHOD_PREFIX + args.join("_")] = f;
};
EQ.prototype = {
check: function() {
try {
this.check0();
} catch (e) {
if (e instanceof ShortcutError) {
return [e.fail];
}
throw e;
}
return this.fails;
},
check0: function() {
var a = this.a;
var b = this.b;
// equal a and b exit early
if (a === b) {
// check for +0 !== -0;
return this.collectFail(a === 0 && 1 / a !== 1 / b && !this.plusZeroAndMinusZeroEqual, PLUS_0_AND_MINUS_0);
}
var typeA = t(a);
var typeB = t(b);
// if objects has different types they are not equal
if (typeA.type !== typeB.type || typeA.cls !== typeB.cls || typeA.sub !== typeB.sub) {
return this.collectFail(true, format(DIFFERENT_TYPES, typeToString(typeA), typeToString(typeB)));
}
// as types the same checks type specific things
var name1 = typeA.type,
name2 = typeA.type;
if (typeA.cls) {
name1 += "_" + typeA.cls;
name2 += "_" + typeA.cls;
}
if (typeA.sub) {
name2 += "_" + typeA.sub;
}
var f =
this[METHOD_PREFIX + name2] ||
this[METHOD_PREFIX + name1] ||
this[METHOD_PREFIX + typeA.type] ||
this.defaultCheck;
f.call(this, this.a, this.b);
},
collectFail: function(comparison, reason, showReason) {
if (comparison) {
var res = new EqualityFail(this.a, this.b, reason, this.path);
res.showReason = !!showReason;
this.fails.push(res);
if (!this.collectAllFails) {
throw new ShortcutError(res);
}
}
},
checkPlainObjectsEquality: function(a, b) {
// compare deep objects and arrays
// stacks contain references only
//
var meet = this._meet;
var m = this._meet.length;
while (m--) {
var st = meet[m];
if (st[0] === a && st[1] === b) {
return;
}
}
// add `a` and `b` to the stack of traversed objects
meet.push([a, b]);
// TODO maybe something else like getOwnPropertyNames
var key;
for (key in b) {
if (hasOwnProperty.call(b, key)) {
if (hasOwnProperty.call(a, key)) {
this.checkPropertyEquality(key);
} else {
this.collectFail(true, format(MISSING_KEY, "A", key));
}
}
}
// ensure both objects have the same number of properties
for (key in a) {
if (hasOwnProperty.call(a, key)) {
this.collectFail(!hasOwnProperty.call(b, key), format(MISSING_KEY, "B", key));
}
}
meet.pop();
if (this.checkProtoEql) {
//TODO should i check prototypes for === or use eq?
this.collectFail(Object.getPrototypeOf(a) !== Object.getPrototypeOf(b), EQUALITY_PROTOTYPE, true);
}
},
checkPropertyEquality: function(propertyName) {
var _eq = new EQ(this, this.a[propertyName], this.b[propertyName], this.path.concat([propertyName]));
_eq.check0();
},
defaultCheck: EQ.checkStrictEquality
};
EQ.add(t.NUMBER, function(a, b) {
this.collectFail((a !== a && b === b) || (b !== b && a === a) || (a !== b && a === a && b === b), EQUALITY);
});
[t.SYMBOL, t.BOOLEAN, t.STRING].forEach(function(tp) {
EQ.add(tp, EQ.checkStrictEquality);
});
EQ.add(t.FUNCTION, function(a, b) {
// functions are compared by their source code
this.collectFail(a.toString() !== b.toString(), FUNCTION_SOURCES);
// check user properties
this.checkPlainObjectsEquality(a, b);
});
EQ.add(t.OBJECT, t.REGEXP, function(a, b) {
// check regexp flags
var flags = ["source", "global", "multiline", "lastIndex", "ignoreCase", "sticky", "unicode"];
while (flags.length) {
this.checkPropertyEquality(flags.shift());
}
// check user properties
this.checkPlainObjectsEquality(a, b);
});
EQ.add(t.OBJECT, t.DATE, function(a, b) {
//check by timestamp only (using .valueOf)
this.collectFail(+a !== +b, EQUALITY);
// check user properties
this.checkPlainObjectsEquality(a, b);
});
[t.NUMBER, t.BOOLEAN, t.STRING].forEach(function(tp) {
EQ.add(t.OBJECT, tp, function(a, b) {
//primitive type wrappers
this.collectFail(a.valueOf() !== b.valueOf(), WRAPPED_VALUE);
// check user properties
this.checkPlainObjectsEquality(a, b);
});
});
EQ.add(t.OBJECT, function(a, b) {
this.checkPlainObjectsEquality(a, b);
});
[t.ARRAY, t.ARGUMENTS, t.TYPED_ARRAY].forEach(function(tp) {
EQ.add(t.OBJECT, tp, function(a, b) {
this.checkPropertyEquality("length");
this.checkPlainObjectsEquality(a, b);
});
});
EQ.add(t.OBJECT, t.ARRAY_BUFFER, function(a, b) {
this.checkPropertyEquality("byteLength");
this.checkPlainObjectsEquality(a, b);
});
EQ.add(t.OBJECT, t.ERROR, function(a, b) {
this.checkPropertyEquality("name");
this.checkPropertyEquality("message");
this.checkPlainObjectsEquality(a, b);
});
EQ.add(t.OBJECT, t.BUFFER, function(a) {
this.checkPropertyEquality("length");
var l = a.length;
while (l--) {
this.checkPropertyEquality(l);
}
//we do not check for user properties because
//node Buffer have some strange hidden properties
});
function checkMapByKeys(a, b) {
var iteratorA = a.keys();
for (var nextA = iteratorA.next(); !nextA.done; nextA = iteratorA.next()) {
var key = nextA.value;
var hasKey = b.has(key);
this.collectFail(!hasKey, format(SET_MAP_MISSING_KEY, key));
if (hasKey) {
var valueB = b.get(key);
var valueA = a.get(key);
eq(valueA, valueB, this);
}
}
}
function checkSetByKeys(a, b) {
var iteratorA = a.keys();
for (var nextA = iteratorA.next(); !nextA.done; nextA = iteratorA.next()) {
var key = nextA.value;
var hasKey = b.has(key);
this.collectFail(!hasKey, format(SET_MAP_MISSING_KEY, key));
}
}
EQ.add(t.OBJECT, t.MAP, function(a, b) {
this._meet.push([a, b]);
checkMapByKeys.call(this, a, b);
checkMapByKeys.call(this, b, a);
this._meet.pop();
this.checkPlainObjectsEquality(a, b);
});
EQ.add(t.OBJECT, t.SET, function(a, b) {
this._meet.push([a, b]);
checkSetByKeys.call(this, a, b);
checkSetByKeys.call(this, b, a);
this._meet.pop();
this.checkPlainObjectsEquality(a, b);
});
export default function eq(a, b, opts) {
return new EQ(opts, a, b).check();
}
eq.EQ = EQ;