Skip to content
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

fix: check array's custom properties #38

Open
wants to merge 2 commits into
base: main
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ cache:
directories:
- node_modules
node_js:
- 0.12 # to be removed 2016-12-31
- 4 # to be removed 2018-04-01
- 6 # to be removed 2019-04-01
- 7 # to be removed 2017-06-30
- lts/* # safety net; don't remove
- node # safety net; don't remove
before_install:
Expand Down
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,20 @@ function iterableEqual(leftHandOperand, rightHandOperand, options) {
return false;
}
}

var leftHandProps = Object.keys(leftHandOperand);
var rightHandProps = Object.keys(rightHandOperand);
if (leftHandProps.length !== rightHandProps.length) {
return false;
}

for (var prop in rightHandProps) {
Copy link
Contributor

@shvaikalesh shvaikalesh Jun 2, 2017

Choose a reason for hiding this comment

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

Maybe we can do

return rightHandProps.every(function(prop) {
  return deepEqual(leftHandOperand[prop], rightHandOperand[prop], options);
});

to avoid for in

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll try this and run the benchmarks to see if it's worth it, but I do agree it would be a lot more clean.

Choose a reason for hiding this comment

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

Using for ... in the way it is used here would actually open up another bug as it would also test for inherited properties.
The fasted way to check this properly is to use Object.keys with a plain for loop.

var propName = rightHandProps[prop];
if (deepEqual(leftHandOperand[propName], rightHandOperand[propName], options) === false) {
return false;
}
}

return true;
}

Expand Down
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,24 @@ describe('Generic', function () {
assert(eql(new Array(1), new Array(100)) === false, 'eql(new Array(1), new Array(100)) === false');
});

it('returns true for arrays with the same custom properties', function () {
var one = [ 1, 2, 'str' ];
var two = [ 1, 2, 'str' ];
one.foo = 'bar';
two.foo = 'bar';

assert(eql(one, two), 'eql([ 0: 1, 1: 2, 2: "str", foo: "bar" ], [ 0: 1, 1: 2, 2: "str", foo: "bar" ])');
});

it('returns false for arrays with different custom properties', function () {
var one = [ 1, 2, 'str' ];
var two = [ 1, 2, 'str' ];
one.foo = 'bar';
two.foo = 'baz';

assert(eql(one, two) === false,
'eql([ 0: 1, 1: 2, 2: "str", foo: "bar" ], [ 0: 1, 1: 2, 2: "str", foo: "baz" ]) === false');
});
});

describe('objects', function () {
Expand Down