Skip to content

Commit

Permalink
[Fix] why: compare two numbers properly
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed May 7, 2021
1 parent 60aef5e commit 297b810
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions test/why.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ var hasArrowFunctionSupport = arrowFunctions.length > 0;
var objectEntries = require('object.entries');
var forEach = require('foreach');
var functionsHaveNames = require('functions-have-names')();
var inspect = require('object-inspect');

var symbolIterator = (hasSymbols || hasSymbolShams) && Symbol.iterator;
var symbolToStringTag = (hasSymbols || hasSymbolShams) && Symbol.toStringTag;

var copyFunction = function (fn) {
/* eslint-disable no-new-func */
Expand All @@ -42,6 +44,20 @@ test('primitives', function (t) {

test('NaN', function (t) {
t.equal('', isEqualWhy(NaN, NaN), 'NaNs are equal');

t.test('fakes', { skip: !symbolToStringTag }, function (st) {
var notNaN = { valueOf: function () { return NaN; } };
notNaN[symbolToStringTag] = 'Number';

st.equal(isEqualWhy(NaN, notNaN), 'second argument is not a number; first argument is', 'NaN and ' + inspect(notNaN) + ' are not equal');
st.equal(isEqualWhy(notNaN, NaN), 'first argument is not a number; second argument is', inspect(notNaN) + ' and NaN are not equal');

st.equal(isEqualWhy(NaN, Infinity), 'first argument is NaN; second is not', 'NaN and Infinity are not equal');
st.equal(isEqualWhy(Infinity, NaN), 'second argument is NaN; first is not', 'Infinity and NaN are not equal');

st.end();
});

t.end();
});

Expand Down
2 changes: 1 addition & 1 deletion why.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module.exports = function whyNotEqual(value, other) {
}

var valIsNumber = isNumber(value);
var otherIsNumber = 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'; }
Expand Down

0 comments on commit 297b810

Please sign in to comment.