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: issues #85 #86

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

var type = require('type-detect');
var compareFn = require('default-compare-with-symbol').defaultCompareWithSymbol;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new dependency also introduces some other transitive dependencies which themselves seem to do the stuff we already have (kind-of looks a lot like type-detect), so I'm a little hesitant to introduce this. If anything I'd like to work on reducing the type-detect dependency as I'm not totally convinced it's necessary with modern engines.

To fix #85 issue it seems as though we could use .toString() instead, or perhaps a try/catch.

Copy link

@Tofandel Tofandel Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keithamus I'm hitting the same issue but with actual symbols, not just urls

I don't see any dependencies to default-compare-with-symbol in fact the code is also quite small
https://www.npmjs.com/package/default-compare-with-symbol?activeTab=dependencies

Worst case we can just make the function in the lib, this is exactly what the dep does:

function defaultCompare(a, b) {
    const typeA = a === null ? 'null' : typeof a;
    const typeB = b === null ? 'null' : typeof b;
    if (typeA === 'null' || typeA === 'undefined') {
        switch (typeB) {
            case typeA:
                return 0;
            case 'null':
                return -1;
            default:
                return 1;
        }
    }
    switch (true) {
        case a < b:
            return -1;
        case a > b:
            return 1;
    }

    return 0;
}

function extendedCompare(a, b) {
    const typeB = typeof b;
    if (typeof a === 'symbol') {
        return typeB === 'symbol' ? defaultCompare(String(a), String(b)) : -1;
    } else if (typeB === 'symbol') {
        return 1;
    }

    return defaultCompare(a, b)
}

function FakeMap() {
this._key = 'chai/deep-eql__' + Math.random() + Date.now();
}
Expand Down Expand Up @@ -276,7 +277,7 @@ function entriesEqual(leftHandOperand, rightHandOperand, options) {
rightHandOperand.forEach(function gatherEntries(key, value) {
rightHandItems.push([ key, value ]);
});
return iterableEqual(leftHandItems.sort(), rightHandItems.sort(), options);
return iterableEqual(leftHandItems.sort(compareFn), rightHandItems.sort(compareFn), options);
}

/*!
Expand Down Expand Up @@ -428,8 +429,8 @@ function objectEqual(leftHandOperand, rightHandOperand, options) {
rightHandKeys = rightHandKeys.concat(rightHandSymbols);
}
if (leftHandKeys.length && leftHandKeys.length === rightHandKeys.length) {
leftHandKeys.sort();
rightHandKeys.sort();
leftHandKeys.sort(compareFn);
rightHandKeys.sort(compareFn);
if (iterableEqual(leftHandKeys, rightHandKeys) === false) {
return false;
}
Expand All @@ -439,8 +440,8 @@ function objectEqual(leftHandOperand, rightHandOperand, options) {
var leftHandEntries = getIteratorEntries(leftHandOperand);
var rightHandEntries = getIteratorEntries(rightHandOperand);
if (leftHandEntries.length && leftHandEntries.length === rightHandEntries.length) {
leftHandEntries.sort();
rightHandEntries.sort();
leftHandEntries.sort(compareFn);
rightHandEntries.sort(compareFn);
return iterableEqual(leftHandEntries, rightHandEntries, options);
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
}
},
"dependencies": {
"default-compare-with-symbol": "^1.0.1",
"type-detect": "^4.0.0"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ describe('Generic', function () {
var objectB = { [symb]: { [symb]: 'a' } };
assert(eql(objectA, objectB) === false, 'eql(obj, obj) === false');
});

it('issues#85', function () {
var objectA = new URL('https://github.com/');
var objectB = new URL('https://github.com/');
var objectC = new URL('https://github.com/chaijs/deep-eql');
assert(eql(objectA, objectB) === true, 'eql(obj, obj) === true');
assert(eql(objectA, objectC) === false, 'eql(obj, obj) === false');
});
});


Expand Down