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: deep symbol comparison #81

Merged
merged 1 commit into from
Feb 28, 2022
Merged
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
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