Skip to content

Commit

Permalink
fix: multiple symbols and sort fails - Cannot convert a Symbol value …
Browse files Browse the repository at this point in the history
…to a string
  • Loading branch information
snewcomer committed Feb 28, 2022
1 parent 0d392a3 commit 3c88fb4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,7 @@ function objectEqual(leftHandOperand, rightHandOperand, options) {
rightHandKeys = rightHandKeys.concat(rightHandSymbols);
}
if (leftHandKeys.length && leftHandKeys.length === rightHandKeys.length) {
leftHandKeys.sort();
rightHandKeys.sort();
if (iterableEqual(leftHandKeys, rightHandKeys) === false) {
if (iterableEqual(mapSymbols(leftHandKeys).sort(), mapSymbols(rightHandKeys).sort()) === false) {
return false;
}
return keysEqual(leftHandOperand, rightHandOperand, leftHandKeys, options);
Expand Down Expand Up @@ -466,3 +464,13 @@ function objectEqual(leftHandOperand, rightHandOperand, options) {
function isPrimitive(value) {
return value === null || typeof value !== 'object';
}

function mapSymbols(arr) {
return arr.map(function mapSymbol(entry) {
if (typeof entry === 'symbol') {
return entry.description;
}

return entry;
});
}
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,22 @@ describe('Generic', function () {
var objectB = { [symb]: { [symb]: 'a' } };
assert(eql(objectA, objectB) === false, 'eql(obj, obj) === false');
});

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

objectA = { [symb]: 'a', [symb2]: 'b' };
objectB = { [symb2]: 'b', [symb]: 'a' };
assert(eql(objectA, objectB) === true, 'eql(obj, obj)');

objectA = { [symb]: 'a', [symb2]: 'b' };
objectB = { [symb2]: 'a', [symb]: 'b' };
assert(eql(objectA, objectB) === false, 'eql(obj, obj) === false');
});
});


Expand Down

0 comments on commit 3c88fb4

Please sign in to comment.