Skip to content

feat: add comparison of functions by content #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ function equal(a, b) {
if (Array.isArray(a)) {
length = a.length;
if (length != b.length) return false;
for (i = length; i-- !== 0;)
if (!equal(a[i], b[i])) return false;
for (i = length; i-- !== 0; ) if (!equal(a[i], b[i])) return false;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, it looks like my IDE prettier settings did a bit of reformatting here. If there's a particular code style that's meant to be followed, would be good to get a prettier config into the repo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the source code comments indicate, we track upstream source code from https://github.com/epoberezkin/fast-deep-equal and then place our updates inside. Community PRs usually just attempt to update our specific modifications here or update the upstream library first.

return true;
}

Expand All @@ -44,31 +43,27 @@ function equal(a, b) {
//
// **Note**: `i` access switches to `i.value`.
var it;
if (hasMap && (a instanceof Map) && (b instanceof Map)) {
if (hasMap && a instanceof Map && b instanceof Map) {
if (a.size !== b.size) return false;
it = a.entries();
while (!(i = it.next()).done)
if (!b.has(i.value[0])) return false;
while (!(i = it.next()).done) if (!b.has(i.value[0])) return false;
it = a.entries();
while (!(i = it.next()).done)
if (!equal(i.value[1], b.get(i.value[0]))) return false;
while (!(i = it.next()).done) if (!equal(i.value[1], b.get(i.value[0]))) return false;
return true;
}

if (hasSet && (a instanceof Set) && (b instanceof Set)) {
if (hasSet && a instanceof Set && b instanceof Set) {
if (a.size !== b.size) return false;
it = a.entries();
while (!(i = it.next()).done)
if (!b.has(i.value[0])) return false;
while (!(i = it.next()).done) if (!b.has(i.value[0])) return false;
return true;
}
// END: Modifications

if (hasArrayBuffer && ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
length = a.length;
if (length != b.length) return false;
for (i = length; i-- !== 0;)
if (a[i] !== b[i]) return false;
for (i = length; i-- !== 0; ) if (a[i] !== b[i]) return false;
return true;
}

Expand All @@ -80,16 +75,15 @@ function equal(a, b) {
length = keys.length;
if (length !== Object.keys(b).length) return false;

for (i = length; i-- !== 0;)
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
for (i = length; i-- !== 0; ) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
// END: fast-deep-equal

// START: react-fast-compare
// custom handling for DOM elements
if (hasElementType && a instanceof Element) return false;

// custom handling for React/Preact
for (i = length; i-- !== 0;) {
for (i = length; i-- !== 0; ) {
if ((keys[i] === '_owner' || keys[i] === '__v' || keys[i] === '__o') && a.$$typeof) {
// React-specific: avoid traversing React elements' _owner
// Preact-specific: avoid traversing Preact elements' __v and __o
Expand All @@ -111,6 +105,8 @@ function equal(a, b) {
return true;
}

if (typeof a === 'function' && typeof b === 'function') return a.toString() === b.toString();

return a !== a && b !== b;
}
// end fast-deep-equal
Expand All @@ -119,7 +115,7 @@ module.exports = function isEqual(a, b) {
try {
return equal(a, b);
} catch (error) {
if (((error.message || '').match(/stack|recursion/i))) {
if ((error.message || '').match(/stack|recursion/i)) {
// warn on circular references, don't crash
// browsers give this different errors name and messages:
// chrome/safari: "RangeError", "Maximum call stack size exceeded"
Expand Down
57 changes: 43 additions & 14 deletions test/node/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,39 @@ var generic = require('fast-deep-equal-git/spec/tests.js');
var es6 = require('fast-deep-equal-git/spec/es6tests.js');

const reactElementA = {
'$$typeof': 'react.element',
$$typeof: 'react.element',
type: 'div',
key: null,
ref: null,
props: { x: 1 },
_owner: {},
_store: {}
_store: {},
};
// in reality the _owner object is much more complex (and contains over dozen circular references)
reactElementA._owner.children = [reactElementA];

const reactElementA2 = {
'$$typeof': 'react.element',
$$typeof: 'react.element',
type: 'div',
key: null,
ref: null,
props: { x: 1 },
_owner: {},
_store: {}
_store: {},
};
reactElementA2._owner.children = [reactElementA2];

const reactElementB = {
'$$typeof': 'react.element',
$$typeof: 'react.element',
type: 'div',
key: null,
ref: null,
props: { x: 2 },
_owner: {},
_store: {}
_store: {},
};
reactElementB._owner.children = [reactElementB];


const react = [
{
description: 'React elements',
Expand All @@ -46,27 +45,57 @@ const react = [
description: 'an element compared with itself',
value1: reactElementA,
value2: reactElementA,
equal: true
equal: true,
},
{
description: 'two elements equal by value',
value1: reactElementA,
value2: reactElementA2,
equal: true
equal: true,
},
{
description: 'two elements unequal by value',
value1: reactElementA,
value2: reactElementB,
equal: false
}
]
}
equal: false,
},
],
},
];

const exampleFn = () => 'foo';

const functions = [
{
description: 'Functions',
reactSpecific: false,
tests: [
{
description: 'a function compared with itself',
value1: exampleFn,
value2: exampleFn,
equal: true,
},
{
description: 'two equal inline functions',
value1: () => 'foo',
value2: () => 'foo',
equal: true,
},
{
description: 'two functions with different results',
value1: () => 'foo',
value2: () => 'bar',
equal: false,
},
],
},
];

module.exports = {
generic,
es6,
react,
all: [...generic, ...es6, ...react],
functions,
all: [...generic, ...es6, ...react, ...functions],
};