Skip to content

Commit

Permalink
[Fix] ignore function name when comparing in engines that lack the "n…
Browse files Browse the repository at this point in the history
…ame" property (IE)
  • Loading branch information
ljharb committed Dec 10, 2015
1 parent b8ef004 commit d222ba0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ var isSymbol = require('is-symbol');
var isCallable = require('is-callable');
var entries = require('object.entries');

var foo = function foo() {};
var functionsHaveNames = foo.name === 'foo';

var symbolValue = typeof Symbol === 'function' ? Symbol.prototype.valueOf : null;
var symbolIterator = typeof Symbol === 'function' && isSymbol(Symbol.iterator) ? Symbol.iterator : null;
if (typeof Object.getOwnPropertyNames === 'function' && typeof Map === 'function' && typeof Map.prototype.entries === 'function') {
Expand Down Expand Up @@ -120,7 +123,7 @@ module.exports = function isEqual(value, other) {
if (valueIsArrow !== otherIsArrow) { return false; }

if (isCallable(value) || isCallable(other)) {
if (!isEqual(value.name, other.name)) { return false; }
if (functionsHaveNames && !isEqual(value.name, other.name)) { return false; }
if (!isEqual(value.length, other.length)) { return false; }

var valueStr = String(value);
Expand Down
15 changes: 13 additions & 2 deletions test/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ var hasArrowFunctionSupport = arrowFunctions.length > 0;
var objectEntries = require('object.entries');
var forEach = require('foreach');

var fooFn = function fooFn() {};
var functionsHaveNames = fooFn.name === 'fooFn';

var symbolIterator = typeof Symbol === 'function' && isSymbol(Symbol.iterator) ? Symbol.iterator : null;
if (typeof Object.getOwnPropertyNames === 'function' && typeof Map === 'function' && typeof Map.prototype.entries === 'function') {
forEach(Object.getOwnPropertyNames(Map.prototype), function (name) {
Expand Down Expand Up @@ -165,13 +168,21 @@ test('functions', function (t) {
t.ok(isEqual(anon1, anon1), 'same anon function is equal to itself');
t.notOk(isEqual(anon1, anon1withArg), 'similar anon function with different lengths are not equal');

t.notOk(isEqual(f1, g), 'functions with different names but same implementations are not equal');
if (functionsHaveNames) {
t.notOk(isEqual(f1, g), 'functions with different names but same implementations are not equal');
} else {
t.notOk(isEqual(f1, g), '* function names not supported * functions with different names but same implementations are not equal');
}
t.ok(isEqual(f1, f2), 'functions with same names but same implementations are equal');
t.notOk(isEqual(f1, f3), 'functions with same names but different implementations are not equal');
t.ok(isEqual(anon1, anon2), 'anon functions with same implementations are equal');

t.ok(isEqual(fnNoSpace, fnWithSpaceBeforeBody), 'functions with same arity/name/body are equal despite whitespace between signature and body');
t.notOk(isEqual(emptyFnWithName, fnNoSpace), 'functions with same arity/body, diff name, are not equal');
if (functionsHaveNames) {
t.notOk(isEqual(emptyFnWithName, fnNoSpace), 'functions with same arity/body, diff name, are not equal');
} else {
t.notOk(isEqual(emptyFnWithName, fnNoSpace), '* function names not supported * functions with same arity/body, diff name, are not equal');
}
t.notOk(isEqual(emptyFnOneArg, fnNoSpace), 'functions with same name/body, diff arity, are not equal');

t.test('generators', { skip: !hasGeneratorSupport }, function (st) {
Expand Down

0 comments on commit d222ba0

Please sign in to comment.