-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
why.js
300 lines (268 loc) · 11.5 KB
/
why.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
'use strict';
var ObjectPrototype = Object.prototype;
var toStr = ObjectPrototype.toString;
var booleanValue = Boolean.prototype.valueOf;
var hasOwn = require('hasown');
var isArray = require('isarray');
var isArrowFunction = require('is-arrow-function');
var isBoolean = require('is-boolean-object');
var isDate = require('is-date-object');
var isGenerator = require('is-generator-function');
var isNumber = require('is-number-object');
var isRegex = require('is-regex');
var isString = require('is-string');
var isSymbol = require('is-symbol');
var isCallable = require('is-callable');
var isBigInt = require('is-bigint');
var getIterator = require('es-get-iterator');
var toPrimitive = require('es-to-primitive/es2015');
var whichCollection = require('which-collection');
var whichBoxedPrimitive = require('which-boxed-primitive');
var getPrototypeOf = require('object.getprototypeof/polyfill')();
var hasSymbols = require('has-symbols/shams')();
var hasBigInts = require('has-bigints')();
var objectType = function (v) { return whichCollection(v) || whichBoxedPrimitive(v) || typeof v; };
var isProto = Object.prototype.isPrototypeOf;
var functionsHaveNames = require('functions-have-names')();
var symbolValue = hasSymbols ? Symbol.prototype.valueOf : null;
var bigIntValue = hasBigInts ? BigInt.prototype.valueOf : null;
var normalizeFnWhitespace = function normalizeWhitespace(fnStr) {
// this is needed in IE 9, at least, which has inconsistencies here.
return fnStr.replace(/^function ?\(/, 'function (').replace('){', ') {');
};
var testToPrim = function testToPrimitive(value, other, hint, hintName) {
var valPrimitive = NaN;
var valPrimitiveThrows = false;
try {
valPrimitive = toPrimitive(value, hint);
} catch (error) {
valPrimitiveThrows = true;
}
var otherPrimitive = NaN;
var otherPrimitiveThrows = false;
try {
otherPrimitive = toPrimitive(other, hint);
} catch (error) {
otherPrimitiveThrows = true;
}
if (valPrimitiveThrows || otherPrimitiveThrows) {
if (!valPrimitiveThrows) { return 'second argument toPrimitive (hint ' + hintName + ') throws; first does not'; }
if (!otherPrimitiveThrows) { return 'first argument toPrimitive (hint ' + hintName + ') throws; second does not'; }
} else if (valPrimitive !== otherPrimitive) {
return 'first argument toPrimitive does not match second argument toPrimitive (hint ' + hintName + ')';
}
return '';
};
module.exports = function whyNotEqual(value, other) {
if (value === other) { return ''; }
if (value == null || other == null) {
return value === other ? '' : String(value) + ' !== ' + String(other);
}
var valToStr = toStr.call(value);
var otherToStr = toStr.call(other);
if (valToStr !== otherToStr) {
return 'toStringTag is not the same: ' + valToStr + ' !== ' + otherToStr;
}
var valIsBool = isBoolean(value);
var otherIsBool = isBoolean(other);
if (valIsBool || otherIsBool) {
if (!valIsBool) { return 'first argument is not a boolean; second argument is'; }
if (!otherIsBool) { return 'second argument is not a boolean; first argument is'; }
var valBoolVal = booleanValue.call(value);
var otherBoolVal = booleanValue.call(other);
if (valBoolVal === otherBoolVal) { return ''; }
return 'primitive value of boolean arguments do not match: ' + valBoolVal + ' !== ' + otherBoolVal;
}
var valIsNumber = isNumber(value);
var otherIsNumber = isNumber(other);
if (valIsNumber || otherIsNumber) {
if (!valIsNumber) { return 'first argument is not a number; second argument is'; }
if (!otherIsNumber) { return 'second argument is not a number; first argument is'; }
var valNum = Number(value);
var otherNum = Number(other);
if (valNum === otherNum) { return ''; }
var valIsNaN = isNaN(value);
var otherIsNaN = isNaN(other);
if (valIsNaN && !otherIsNaN) {
return 'first argument is NaN; second is not';
} else if (!valIsNaN && otherIsNaN) {
return 'second argument is NaN; first is not';
} else if (valIsNaN && otherIsNaN) {
return '';
}
return 'numbers are different: ' + value + ' !== ' + other;
}
var valIsString = isString(value);
var otherIsString = isString(other);
if (valIsString || otherIsString) {
if (!valIsString) { return 'second argument is string; first is not'; }
if (!otherIsString) { return 'first argument is string; second is not'; }
var stringVal = String(value);
var otherVal = String(other);
if (stringVal === otherVal) { return ''; }
return 'string values are different: "' + stringVal + '" !== "' + otherVal + '"';
}
var valIsDate = isDate(value);
var otherIsDate = isDate(other);
if (valIsDate || otherIsDate) {
if (!valIsDate) { return 'second argument is Date, first is not'; }
if (!otherIsDate) { return 'first argument is Date, second is not'; }
var valTime = +value;
var otherTime = +other;
if (valTime !== otherTime) {
return 'Dates have different time values: ' + valTime + ' !== ' + otherTime;
}
}
var valIsRegex = isRegex(value);
var otherIsRegex = isRegex(other);
if (valIsRegex || otherIsRegex) {
if (!valIsRegex) { return 'second argument is RegExp, first is not'; }
if (!otherIsRegex) { return 'first argument is RegExp, second is not'; }
var regexStringVal = String(value);
var regexStringOther = String(other);
if (regexStringVal !== regexStringOther) {
return 'regular expressions differ: ' + regexStringVal + ' !== ' + regexStringOther;
}
}
var valIsArray = isArray(value);
var otherIsArray = isArray(other);
if (valIsArray || otherIsArray) {
if (!valIsArray) { return 'second argument is an Array, first is not'; }
if (!otherIsArray) { return 'first argument is an Array, second is not'; }
if (value.length !== other.length) {
return 'arrays have different length: ' + value.length + ' !== ' + other.length;
}
var index = value.length - 1;
var equal = '';
var valHasIndex, otherHasIndex;
while (equal === '' && index >= 0) {
valHasIndex = hasOwn(value, index);
otherHasIndex = hasOwn(other, index);
if (!valHasIndex && otherHasIndex) { return 'second argument has index ' + index + '; first does not'; }
if (valHasIndex && !otherHasIndex) { return 'first argument has index ' + index + '; second does not'; }
equal = whyNotEqual(value[index], other[index]);
index -= 1;
}
return equal;
}
var valueIsSym = isSymbol(value);
var otherIsSym = isSymbol(other);
if (valueIsSym !== otherIsSym) {
if (valueIsSym) { return 'first argument is Symbol; second is not'; }
return 'second argument is Symbol; first is not';
}
if (valueIsSym && otherIsSym) {
return symbolValue.call(value) === symbolValue.call(other) ? '' : 'first Symbol value !== second Symbol value';
}
var valueIsBigInt = isBigInt(value);
var otherIsBigInt = isBigInt(other);
if (valueIsBigInt !== otherIsBigInt) {
if (valueIsBigInt) { return 'first argument is BigInt; second is not'; }
return 'second argument is BigInt; first is not';
}
if (valueIsBigInt && otherIsBigInt) {
return bigIntValue.call(value) === bigIntValue.call(other) ? '' : 'first BigInt value !== second BigInt value';
}
var valueIsGen = isGenerator(value);
var otherIsGen = isGenerator(other);
if (valueIsGen !== otherIsGen) {
if (valueIsGen) { return 'first argument is a Generator function; second is not'; }
return 'second argument is a Generator function; first is not';
}
var valueIsArrow = isArrowFunction(value);
var otherIsArrow = isArrowFunction(other);
if (valueIsArrow !== otherIsArrow) {
if (valueIsArrow) { return 'first argument is an arrow function; second is not'; }
return 'second argument is an arrow function; first is not';
}
var valueIsCallable = isCallable(value);
var otherIsCallable = isCallable(other);
if (valueIsCallable || otherIsCallable) {
if (valueIsCallable !== otherIsCallable) {
return valueIsCallable ? 'first argument is callable; second is not' : 'second argument is callable; first is not';
}
if (functionsHaveNames && whyNotEqual(value.name, other.name) !== '') {
return 'Function names differ: "' + value.name + '" !== "' + other.name + '"';
}
if (whyNotEqual(value.length, other.length) !== '') {
return 'Function lengths differ: ' + value.length + ' !== ' + other.length;
}
var valueStr = normalizeFnWhitespace(String(value));
var otherStr = normalizeFnWhitespace(String(other));
if (
whyNotEqual(valueStr, otherStr) !== ''
&& !(
!valueIsGen
&& !valueIsArrow
&& whyNotEqual(valueStr.replace(/\)\s*\{/, '){'), otherStr.replace(/\)\s*\{/, '){')) === ''
)
) {
return 'Function string representations differ';
}
}
var valueIsObj = valIsDate || valIsRegex || valIsArray || valueIsGen || valueIsArrow || valueIsCallable || Object(value) === value;
var otherIsObj = otherIsDate || otherIsRegex || otherIsArray || otherIsGen || otherIsArrow || otherIsCallable || Object(other) === other;
if (valueIsObj || otherIsObj) {
if (typeof value !== typeof other) { return 'arguments have a different typeof: ' + typeof value + ' !== ' + typeof other; }
if (isProto.call(value, other)) { return 'first argument is the [[Prototype]] of the second'; }
if (isProto.call(other, value)) { return 'second argument is the [[Prototype]] of the first'; }
if (getPrototypeOf(value) !== getPrototypeOf(other)) { return 'arguments have a different [[Prototype]]'; }
var valueIsFn = typeof value === 'function';
var otherIsFn = typeof other === 'function';
if (!valueIsFn || !otherIsFn) {
var result = testToPrim(value, other, String, 'String')
|| testToPrim(value, other, Number, 'Number')
|| testToPrim(value, other, void undefined, 'default');
if (result) {
return result;
}
}
var valueIterator = getIterator(value);
var otherIterator = getIterator(other);
if (!!valueIterator !== !!otherIterator) {
if (valueIterator) { return 'first argument is iterable; second is not'; }
return 'second argument is iterable; first is not';
}
if (valueIterator && otherIterator) { // both should be truthy or falsy at this point
var valueNext, otherNext, nextWhy;
do {
valueNext = valueIterator.next();
otherNext = otherIterator.next();
if (!valueNext.done && !otherNext.done) {
nextWhy = whyNotEqual(valueNext, otherNext);
if (nextWhy !== '') {
return 'iteration results are not equal: ' + nextWhy;
}
}
} while (!valueNext.done && !otherNext.done);
if (valueNext.done && !otherNext.done) { return 'first ' + objectType(value) + ' argument finished iterating before second ' + objectType(other); }
if (!valueNext.done && otherNext.done) { return 'second ' + objectType(other) + ' argument finished iterating before first ' + objectType(value); }
return '';
}
var key, valueKeyIsRecursive, otherKeyIsRecursive, keyWhy;
for (key in value) {
if (hasOwn(value, key)) {
if (!hasOwn(other, key)) { return 'first argument has key "' + key + '"; second does not'; }
valueKeyIsRecursive = !!value[key] && value[key][key] === value;
otherKeyIsRecursive = !!other[key] && other[key][key] === other;
if (valueKeyIsRecursive !== otherKeyIsRecursive) {
if (valueKeyIsRecursive) { return 'first argument has a circular reference at key "' + key + '"; second does not'; }
return 'second argument has a circular reference at key "' + key + '"; first does not';
}
if (!valueKeyIsRecursive && !otherKeyIsRecursive) {
keyWhy = whyNotEqual(value[key], other[key]);
if (keyWhy !== '') {
return 'value at key "' + key + '" differs: ' + keyWhy;
}
}
}
}
for (key in other) {
if (hasOwn(other, key) && !hasOwn(value, key)) {
return 'second argument has key "' + key + '"; first does not';
}
}
return '';
}
return false;
};