Skip to content

Commit

Permalink
[Fix] still check own properties on Dates, RegExps, and Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 10, 2022
1 parent c8e9a71 commit 3295149
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"func-names": 0,
"id-length": [2, { "min": 1, "max": 23 }],
"max-depth": [2, 5],
"max-len": 0,
"max-lines-per-function": [2, { "max": 250 }],
"max-statements": [1, 10],
"max-statements-per-line": [2, { "max": 2 }],
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"foreach": "^2.0.5",
"make-arrow-function": "~1.1.0",
"nyc": "^10.3.2",
"object.assign": "^4.1.2",
"safe-publish-latest": "^2.0.0",
"tape": "^5.3.2"
},
Expand Down
31 changes: 28 additions & 3 deletions test/why.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var functionsHaveNames = require('functions-have-names')();
var inspect = require('object-inspect');
var v = require('es-value-fixtures');
var hasGeneratorSupport = v.generatorFunctions.length > 0;
var assign = require('object.assign');

var symbolIterator = (hasSymbols || hasSymbolShams) && Symbol.iterator;
var symbolToStringTag = (hasSymbols || hasSymbolShams) && Symbol.toStringTag;
Expand Down Expand Up @@ -234,6 +235,14 @@ test('dates', function (t) {
st.end();
});

var zero = new Date(0);
var zeroPlus = assign(new Date(0), { a: 1 });
t.equal(
isEqualWhy(zero, zeroPlus),
'second argument has key "a"; first does not',
inspect(zero) + ' and ' + inspect(zeroPlus) + ' are not equal'
);

t.end();
});

Expand Down Expand Up @@ -294,6 +303,14 @@ test('regexes', function (t) {
st.end();
});

var re = /a/g;
var rePlus = assign(/a/g, { a: 1 });
t.equal(
isEqualWhy(re, rePlus),
'second argument has key "a"; first does not',
inspect(re) + ' and ' + inspect(rePlus) + ' are not equal'
);

t.end();
});

Expand Down Expand Up @@ -519,13 +536,11 @@ test('functions', function (t) {
var g = Object(function g() { /* SOME STUFF */ return 1; });
var anon1 = Object(function () { /* ANONYMOUS! */ return 'anon'; });
var anon2 = Object(function () { /* ANONYMOUS! */ return 'anon'; });
/* jscs: disable */
/* eslint-disable space-before-function-paren */
/* eslint-disable space-before-blocks */
var fnNoSpace = Object(function(){});
/* eslint-enable space-before-blocks */
/* eslint-enable space-before-function-paren */
/* jscs: enable */
var fnWithSpaceBeforeBody = Object(function () {});
var emptyFnWithName = Object(function a() {});
/* eslint-disable no-unused-vars */
Expand Down Expand Up @@ -564,7 +579,7 @@ test('functions', function (t) {
'functions with different names but same implementations are not equal'
);
}
t.equal('', isEqualWhy(f1, f2), 'functions with same names but same implementations are equal');
t.equal(isEqualWhy(f1, f2), '', 'functions with same names but same implementations are equal');
t.equal(
isEqualWhy(f1, f3),
'Function string representations differ',
Expand Down Expand Up @@ -725,6 +740,16 @@ test('functions', function (t) {
st.end();
});

/* eslint-disable no-unused-vars */
var fn = function f(x) { 'y'; };
var fnPlus = assign(function f(x) { 'y'; }, { a: 1 });
/* eslint-enable no-unused-vars */
t.equal(
isEqualWhy(fn, fnPlus),
'second argument has key "a"; first does not',
inspect(fn) + ' and ' + inspect(fnPlus) + ' are not equal'
);

t.end();
});

Expand Down
27 changes: 16 additions & 11 deletions why.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ module.exports = function whyNotEqual(value, other) {
if (!otherIsDate) { return 'first argument is Date, second is not'; }
var valTime = +value;
var otherTime = +other;
if (valTime === otherTime) { return ''; }
return 'Dates have different time values: ' + valTime + ' !== ' + otherTime;
if (valTime !== otherTime) {
return 'Dates have different time values: ' + valTime + ' !== ' + otherTime;
}
}

var valIsRegex = isRegex(value);
Expand All @@ -109,8 +110,9 @@ module.exports = function whyNotEqual(value, other) {
if (!otherIsRegex) { return 'first argument is RegExp, second is not'; }
var regexStringVal = String(value);
var regexStringOther = String(other);
if (regexStringVal === regexStringOther) { return ''; }
return 'regular expressions differ: ' + regexStringVal + ' !== ' + regexStringOther;
if (regexStringVal !== regexStringOther) {
return 'regular expressions differ: ' + regexStringVal + ' !== ' + regexStringOther;
}
}

var valIsArray = isArray(value);
Expand Down Expand Up @@ -170,7 +172,9 @@ module.exports = function whyNotEqual(value, other) {
return 'second argument is an arrow function; first is not';
}

if (isCallable(value) || isCallable(other)) {
var valueIsCallable = isCallable(value);
var otherIsCallable = isCallable(other);
if (valueIsCallable || otherIsCallable) {
if (functionsHaveNames && whyNotEqual(value.name, other.name) !== '') {
return 'Function names differ: "' + value.name + '" !== "' + other.name + '"';
}
Expand All @@ -181,20 +185,21 @@ module.exports = function whyNotEqual(value, other) {
var valueStr = normalizeFnWhitespace(String(value));
var otherStr = normalizeFnWhitespace(String(other));
if (
whyNotEqual(valueStr, otherStr) === ''
|| (
whyNotEqual(valueStr, otherStr) !== ''
&& !(
!valueIsGen
&& !valueIsArrow
&& whyNotEqual(valueStr.replace(/\)\s*\{/, '){'), otherStr.replace(/\)\s*\{/, '){')) === ''
)
) {
return '';
return 'Function string representations differ';
}

return 'Function string representations differ';
}

if (typeof value === 'object' || typeof other === 'object') {
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'; }
Expand Down

0 comments on commit 3295149

Please sign in to comment.