Skip to content

Commit

Permalink
Fix to compare input for iteratables. (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
MtBlue81 authored Oct 31, 2022
1 parent 58480cc commit aa3badb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/deep-equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ function deepEqualCyclic(actual, expectation, match) {
!isArrayType(actualObj) &&
!isArguments(actualObj);
var isExpectationNonArrayIterable =
isIterable(expectation) &&
!isArrayType(expectation) &&
!isArguments(expectation);
isIterable(expectationObj) &&
!isArrayType(expectationObj) &&
!isArguments(expectationObj);
if (isActualNonArrayIterable || isExpectationNonArrayIterable) {
var actualArray = Array.from(actualObj);
var expectationArray = Array.from(expectation);
var expectationArray = Array.from(expectationObj);
if (actualArray.length !== expectationArray.length) {
return false;
}
Expand Down
44 changes: 44 additions & 0 deletions lib/deep-equal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,4 +924,48 @@ describe("deepEqual", function () {
assert.isFalse(checkDeep);
});
});

describe("nested iterable", function () {
let buildInstance;

before(function () {
class Foo {
constructor(size = 0) {
this.size = size;
}
}
Foo.prototype[Symbol.iterator] = function () {
var count = 1;
var size = this.size;
return {
next: function () {
return count <= size
? { value: count++, done: false }
: { value: undefined, done: true };
},
};
};
buildInstance = function (size) {
return new Foo(size);
};
});

it("return true if same size", function () {
assert.isTrue(
samsam.deepEqual(
{ a: buildInstance(2) },
{ a: buildInstance(2) }
)
);
});

it("return false if different size", function () {
assert.isFalse(
samsam.deepEqual(
{ a: buildInstance(2) },
{ a: buildInstance(3) }
)
);
});
});
});

0 comments on commit aa3badb

Please sign in to comment.