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 (#83)

* fix: multiple symbols and sort fails - Cannot convert a Symbol value to a string

* revert: #89

* refactor: no if check
  • Loading branch information
snewcomer committed Nov 5, 2022
1 parent 9586dfb commit a311421
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,16 +430,15 @@ function keysEqual(leftHandOperand, rightHandOperand, keys, options) {
* @return {Boolean} result
*/
function objectEqual(leftHandOperand, rightHandOperand, options) {
var leftHandKeys = getEnumerableKeys(leftHandOperand).sort();
var rightHandKeys = getEnumerableKeys(rightHandOperand).sort();
var leftHandSymbols = getNonEnumerableSymbols(leftHandOperand).sort();
var rightHandSymbols = getNonEnumerableSymbols(rightHandOperand).sort();

var leftHandKeys = getEnumerableKeys(leftHandOperand);
var rightHandKeys = getEnumerableKeys(rightHandOperand);
var leftHandSymbols = getNonEnumerableSymbols(leftHandOperand);
var rightHandSymbols = getNonEnumerableSymbols(rightHandOperand);
leftHandKeys = leftHandKeys.concat(leftHandSymbols);
rightHandKeys = rightHandKeys.concat(rightHandSymbols);

if (leftHandKeys.length && leftHandKeys.length === rightHandKeys.length) {
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 @@ -475,3 +474,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.toString();
}

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

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');

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


Expand Down

0 comments on commit a311421

Please sign in to comment.