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: multiple symbols and sort fails - Cannot convert a Symbol value to a string #83

Merged
merged 3 commits into from
Nov 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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