Skip to content

Commit

Permalink
[Refactor] increase coverage; remove redundant branches
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed May 8, 2021
1 parent 19af37e commit c6b6672
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"number": false,
"string": true
}],
"no-restricted-syntax": [2, "BreakStatement", "ContinueStatement", "DebuggerStatement", "LabeledStatement", "WithStatement"]
"no-restricted-syntax": [2, "BreakStatement", "ContinueStatement", "DebuggerStatement", "LabeledStatement", "WithStatement"],
"operator-linebreak": [2, "before"],
},

"overrides": [
Expand Down
13 changes: 13 additions & 0 deletions test/why.js
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,19 @@ test('functions', function (t) {
st.equal(isEqualWhy(fn, copyFunction(fn)), '', inspect(fn) + ' is equal to copyFunction(fn)');
});

var arrow1 = Function('return (a) => {}');
var arrow2 = Function('return (b) => {}');
st.equal(
isEqualWhy(arrow1(), arrow2()),
'Function string representations differ',
'same name/length arrow functions with different source are not equal'
);
st.equal(
isEqualWhy(arrow1(), arrow1()),
'',
'same name/length arrow functions with same source are equal'
);

st.end();
});

Expand Down
16 changes: 11 additions & 5 deletions why.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,18 @@ module.exports = function whyNotEqual(value, other) {

var valueStr = normalizeFnWhitespace(String(value));
var otherStr = normalizeFnWhitespace(String(other));
if (whyNotEqual(valueStr, otherStr) === '') { return ''; }

if (!valueIsGen && !valueIsArrow) {
return whyNotEqual(valueStr.replace(/\)\s*\{/, '){'), otherStr.replace(/\)\s*\{/, '){')) === '' ? '' : 'Function string representations differ';
if (
whyNotEqual(valueStr, otherStr) === ''
|| (
!valueIsGen
&& !valueIsArrow
&& whyNotEqual(valueStr.replace(/\)\s*\{/, '){'), otherStr.replace(/\)\s*\{/, '){')) === ''
)
) {
return '';
}
return whyNotEqual(valueStr, otherStr) === '' ? '' : 'Function string representations differ';

return 'Function string representations differ';
}

if (typeof value === 'object' || typeof other === 'object') {
Expand Down

0 comments on commit c6b6672

Please sign in to comment.