Skip to content

Commit

Permalink
[Fix] Remove usage of push and for loop
Browse files Browse the repository at this point in the history
  • Loading branch information
arasmussen committed Sep 30, 2023
1 parent 062ae5c commit b26dec7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
1 change: 0 additions & 1 deletion test/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var objectEntries = require('object.entries');
var forEach = require('for-each');
var functionsHaveNames = require('functions-have-names')();
var inspect = require('object-inspect');
var toPrimitive = require('es-to-primitive');
var v = require('es-value-fixtures');
var hasGeneratorSupport = v.generatorFunctions.length > 0;

Expand Down
53 changes: 36 additions & 17 deletions why.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,36 +206,55 @@ module.exports = function whyNotEqual(value, other) {
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 primitiveHints = [];
var valueIsFn = typeof value === 'function';
var otherIsFn = typeof other === 'function';

if (!valueIsFn || !otherIsFn) {
primitiveHints.push(Number);
primitiveHints.push(String);
}
var valStringPrimitive = null;
var valStringPrimitiveErr = null;
try {
valStringPrimitive = toPrimitive(value, String);
} catch (error) {
valStringPrimitiveErr = error;
}

var otherStringPrimitive = null;
var otherStringPrimitiveErr = null;
try {
otherStringPrimitive = toPrimitive(other, String);
} catch (error) {
otherStringPrimitiveErr = error;
}

if (valStringPrimitiveErr || otherStringPrimitiveErr) {
if (!valStringPrimitiveErr) { return 'second argument toPrimitive throws; first does not'; }
if (!otherStringPrimitiveErr) { return 'first argument toPrimitive throws; second does not'; }
}
if (valStringPrimitive !== otherStringPrimitive) {
return 'first argument toPrimitive does not match second argument toPrimitive';
}

for (var primitiveHint in primitiveHints) {
var valPrimitive = null;
var valPrimitiveErr = null;
var valNumberPrimitive = null;
var valNumberPrimitiveErr = null;
try {
valPrimitive = toPrimitive(value, primitiveHint);
valNumberPrimitive = toPrimitive(value, Number);
} catch (error) {
valPrimitiveErr = error;
valNumberPrimitiveErr = error;
}

var otherPrimitive = null;
var otherPrimitiveErr = null;
var otherNumberPrimitive = null;
var otherNumberPrimitiveErr = null;
try {
otherPrimitive = toPrimitive(other, primitiveHint);
otherNumberPrimitive = toPrimitive(other, Number);
} catch (error) {
otherPrimitiveErr = error;
otherNumberPrimitiveErr = error;
}

if (valPrimitiveErr || otherPrimitiveErr) {
if (!valPrimitiveErr) { return 'second argument toPrimitive throws; first does not'; }
if (!otherPrimitiveErr) { return 'first argument toPrimitive throws; second does not'; }
if (valNumberPrimitiveErr || otherNumberPrimitiveErr) {
if (!valNumberPrimitiveErr) { return 'second argument toPrimitive throws; first does not'; }
if (!otherNumberPrimitiveErr) { return 'first argument toPrimitive throws; second does not'; }
}
if (valPrimitive !== otherPrimitive) {
if (valNumberPrimitive !== otherNumberPrimitive) {
return 'first argument toPrimitive does not match second argument toPrimitive';
}
}
Expand Down

0 comments on commit b26dec7

Please sign in to comment.