Skip to content

Commit

Permalink
fix: deep symbol comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Feb 25, 2022
1 parent 3b39bf3 commit 2234e30
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ function getEnumerableKeys(target) {
return keys;
}

function getNonEnumerableSymbols(target) {
var keys = Object.getOwnPropertySymbols(target);
return keys;
}

/*!
* Determines if two objects have matching values, given a set of keys. Defers to deepEqual for the equality check of
* each key. If any value of the given key is not equal, the function will return false (early).
Expand Down Expand Up @@ -414,6 +419,14 @@ 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);
if (leftHandSymbols) {
leftHandKeys = leftHandKeys.concat(leftHandSymbols);
}
if (rightHandSymbols) {
rightHandKeys = rightHandKeys.concat(rightHandSymbols);
}
if (leftHandKeys.length && leftHandKeys.length === rightHandKeys.length) {
leftHandKeys.sort();
rightHandKeys.sort();
Expand Down
42 changes: 42 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,48 @@ describe('Generic', function () {

});

describe('Symbols', function () {

it('returns true for same symbols', function () {
var symb = Symbol('a');
var objectA = { [symb]: 'a' };
var objectB = { [symb]: 'a' };
assert(eql(objectA, objectB) === true, 'eql(obj, obj)');
});

it('returns false for different values', function () {
var symb = Symbol('a');
var objectA = { [symb]: 'a' };
var objectB = { [symb]: 'b' };
assert(eql(objectA, objectB) === false, 'eql(obj, obj) === false');
});

it('returns false for different symbols', function () {
var symb = Symbol('a');
var symb2 = Symbol('b');
var objectA = { [symb]: 'a' };
var objectB = { [symb2]: 'a' };
assert(eql(objectA, objectB) === false, 'eql(obj, obj) === false');
});

it('returns true for same nested symbols', function () {
var symb = Symbol('a');
var symb2 = Symbol('b');
var objectA = { [symb]: { [symb2]: 'a' } };
var objectB = { [symb]: { [symb2]: 'a' } };
assert(eql(objectA, objectB) === true, 'eql(obj, obj)');
});

it('returns false for different nested symbols', function () {
var symb = Symbol('a');
var symb2 = Symbol('b');
var objectA = { [symb]: { [symb2]: 'a' } };
var objectB = { [symb]: { [symb]: 'a' } };
assert(eql(objectA, objectB) === false, 'eql(obj, obj) === false');
});
});


describe('errors', function () {

it('returns true for same errors', function () {
Expand Down

0 comments on commit 2234e30

Please sign in to comment.