Skip to content

Commit

Permalink
Consider functions "equal" if type, name, arity, and body are equal.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 15, 2014
1 parent 57c719d commit 6622d56
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ module.exports = function isEqual(value, other) {
var otherIsArrow = isArrowFunction(other);
if (valueIsArrow !== otherIsArrow) { return false; }

return isEqual(String(value), String(other));
var valueStr = String(value);
var otherStr = String(other);
if (isEqual(valueStr, otherStr)) { return true; }

if (!valueIsGen && !valueIsArrow) {
return isEqual(valueStr.replace(/\)\s*\{/, '){'), otherStr.replace(/\)\s*\{/, '){'));
}
return isEqual(valueStr, otherStr);;
}

if (type === objType) {
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ test('functions', function (t) {
/* jscs: disable */
var fnNoSpace = function(){};
/* jscs: enable */
var fnWithSpaceBeforeBody = function () {};
var emptyFnWithName = function a() {};
var emptyFnOneArg = function (a) {};

/* for code coverage */
f1();
Expand All @@ -149,6 +152,10 @@ test('functions', function (t) {
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');
t.notOk(isEqual(emptyFnOneArg, fnNoSpace), 'functions with same name/body, diff arity, are not equal');

t.test('generators', { skip: !hasGeneratorSupport }, function (st) {
var genFnStar = Function('return function* () {};')();
var genFnSpaceStar = Function('return function *() {};')();
Expand Down

0 comments on commit 6622d56

Please sign in to comment.