Feature request - allow '==' equality with an option
xurei opened this issue · comments
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.
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.?
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.