Answer wether or not two elements are semantically equal, considering them
equal when they have the same type and all their internal elements are
the same, or when they represent the same concept (two regular expressions
that match the same string, two dates for the same moment, two sets with same
elements in it, and so on).
The function is intended for comparison of basic types, simple non classed
objects, arrays, and built-in basic classed objects such as Set, Map, RegExp,
Date, Buffer and Error.
Most simple cases should return true as expected, such as:
There is one special case, that we support and that might not be expected
in standard TS/JS behavior, which is NaN comparison. Here you might find
that deepEquals(NaN, NaN) is true, even though in JS NaN is not equal
to anything, even itself.
Note that parameters are statically typed when running in TypeScript,
thus not allowing for things such as deepEquals(4, '4.0') to be typed,
unless explicitly casted away. In that case even, the comparison is performed
not considering type coercion, thus, returning false.
Note that deep equality is costly, and should be avoided whenever possible. Yet
is some scenarios, it may be useful to count with such a function. In that sense,
we provide a 'cheap' (in terms of dependency overhead) alternative to most
third-party implementations, that can be used through the whole project.
The implementation is kind of ugly and heavily procedural. The idea behind the
code is to return a result as fast as possible, for performance reasons. Also
note that it might not consider the most edgy cases. If you have trouble with a
specific case, please consider sending a Pull Request or raising an Issue in
this project's repository.
If you want to see all supported and unsupported cases, we recommend you to check
out the test cases.
Answer wether or not two elements are semantically equal, considering them equal when they have the same type and all their internal elements are the same, or when they represent the same concept (two regular expressions that match the same string, two dates for the same moment, two sets with same elements in it, and so on).
The function is intended for comparison of basic types, simple non classed objects, arrays, and built-in basic classed objects such as Set, Map, RegExp, Date, Buffer and Error.
Most simple cases should return true as expected, such as:
deepEquals(1, 1.0)
deepEquals({a: 1, b: {c: 3, d: 4}}, {b: {c: 3, d: 4}, a: 1})
deepEquals([1,2,3], [1, 1+1, 2+1])
deepEquals(new Set([1,2,3]), new Set([3,2,1]))
There is one special case, that we support and that might not be expected in standard TS/JS behavior, which is
NaN
comparison. Here you might find thatdeepEquals(NaN, NaN)
istrue
, even though in JS NaN is not equal to anything, even itself.Note that parameters are statically typed when running in TypeScript, thus not allowing for things such as
deepEquals(4, '4.0')
to be typed, unless explicitly casted away. In that case even, the comparison is performed not considering type coercion, thus, returning false.Note that deep equality is costly, and should be avoided whenever possible. Yet is some scenarios, it may be useful to count with such a function. In that sense, we provide a 'cheap' (in terms of dependency overhead) alternative to most third-party implementations, that can be used through the whole project.
The implementation is kind of ugly and heavily procedural. The idea behind the code is to return a result as fast as possible, for performance reasons. Also note that it might not consider the most edgy cases. If you have trouble with a specific case, please consider sending a Pull Request or raising an Issue in this project's repository.
If you want to see all supported and unsupported cases, we recommend you to check out the test cases.