Skip to content

Commit

Permalink
feat: only compare enumerable symbols (#91)
Browse files Browse the repository at this point in the history
Fixes #90.
  • Loading branch information
lucaswerkmeister authored Dec 4, 2022
1 parent 474b76d commit 0e417c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,15 @@ function getEnumerableKeys(target) {
return keys;
}

function getNonEnumerableSymbols(target) {
var keys = Object.getOwnPropertySymbols(target);
function getEnumerableSymbols(target) {
var keys = [];
var allKeys = Object.getOwnPropertySymbols(target);
for (var i = 0; i < allKeys.length; i += 1) {
var key = allKeys[i];
if (Object.getOwnPropertyDescriptor(target, key).enumerable) {
keys.push(key);
}
}
return keys;
}

Expand Down Expand Up @@ -432,8 +439,8 @@ function keysEqual(leftHandOperand, rightHandOperand, keys, options) {
function objectEqual(leftHandOperand, rightHandOperand, options) {
var leftHandKeys = getEnumerableKeys(leftHandOperand);
var rightHandKeys = getEnumerableKeys(rightHandOperand);
var leftHandSymbols = getNonEnumerableSymbols(leftHandOperand);
var rightHandSymbols = getNonEnumerableSymbols(rightHandOperand);
var leftHandSymbols = getEnumerableSymbols(leftHandOperand);
var rightHandSymbols = getEnumerableSymbols(rightHandOperand);
leftHandKeys = leftHandKeys.concat(leftHandSymbols);
rightHandKeys = rightHandKeys.concat(rightHandSymbols);

Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,16 @@ describe('Generic', function () {
objectB = { [symb3]: 'a', [symb2]: 'b' };
assert(eql(objectA, objectB) === true, 'eql(obj, obj)');
});

it('ignores non-enumerable symbols', function () {
var symb = Symbol('a');
var symb2 = Symbol('b');
var objectA = { [symb]: 'a' };
Object.defineProperty(objectA, symb2, { value: 'b', enumerable: false });
var objectB = { [symb]: 'a' };
Object.defineProperty(objectB, symb2, { value: 'c', enumerable: false });
assert(eql(objectA, objectB) === true, 'eql(obj, obj)');
});
});


Expand Down

0 comments on commit 0e417c7

Please sign in to comment.