Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature request - allow '==' equality with an option #47

Open
xurei opened this issue Sep 5, 2017 · 2 comments
Open

Feature request - allow '==' equality with an option #47

xurei opened this issue Sep 5, 2017 · 2 comments

Comments

@xurei
Copy link

xurei commented Sep 5, 2017

In some cases, it would make sense to use the weak equality == instead of the strong one ===.

Take this example, where two URL parsers give different results :

{
  pathvars: {
    id: 42
  }
}

vs

{
  pathvars: {
    id: "42"
  }
}

I have no idea how complicated it would be to achieve internally. From the API point of view, I would add another function or a flag in the current one's arguments to enable the weak comparison.

@mindplay-dk
Copy link

I was able to do it using the undocumented options.comparator, like this:

function isPrimitive(value) {
    return value === null || typeof value !== 'object';
}

function loose(a, b) {
    return isPrimitive(a) && isPrimitive(b) ? a == b : null;
}

Seems to work - for example:

console.log(deepEqual(3, '3', { comparator: loose })); // true

console.log(deepEqual({foo:3}, {foo:'3'}, { comparator: loose })); // true

console.log(deepEqual([3], ['3'], { comparator: loose })); // true

The isPrimitive function is already present internally in the library, so adding this feature should be very simple - the main question is what would you expect the API to look like, how would this work with other comparators, etc.?

@xurei
Copy link
Author

xurei commented Apr 24, 2018

I guess the most generic way to add this would be to expose the comparison function in the options.

In other words, documenting it seems enough for my use case.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants